0

I'm trying to upload a file to Rapidshare through their API with Java. But I'm stuck and I think my problem is that their API don't know the property name of my uploaded file, or something like that. It seems like it uploads and submits perfectly, but no files are in my account afterwards.

I've based my code on what I found here: http://www.experts-exchange.com/Programming/Languages/Java/Q_20370019.html

If any help, I succeeded to create a solution using PHP and cURL, but the final solution must be in Java.

$post = array(
  'sub'         => 'upload',
  'login'       => $user,
  'password'    => $pass,
  'filecontent' => '@'. $filepath
);

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, 'https://rs'. $server .'.rapidshare.com/cgi-bin/rsapi.cgi');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
$info = curl_exec($c);
curl_close($c);

My Java source:

package main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void main(String args[]) throws Exception {
        String url = "http://rs"+ getServerId() +".rapidshare.com/cgi-bin/rsapi.cgi?sub=upload&login=***&password=***";
        String docPath = "/Users/***/Downloads/a.jpg";    // Document 

        HttpURLConnection httpcon = (HttpURLConnection) ((new URL(url).openConnection()));
        httpcon.setDoOutput(true);
        httpcon.setUseCaches(false);
        httpcon.setRequestProperty("Content-Type", "multipart/form-data");
        httpcon.connect();

        System.out.println("Posting " +docPath +"...");
        File file = new File(docPath);
        FileInputStream is = new FileInputStream(file);
        OutputStream os = httpcon.getOutputStream();

        // POST
        // Read bytes until EOF to write
        byte[] buffer = new byte[4096];
        int bytes_read;    // How many bytes in buffer
        while((bytes_read = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytes_read);
        }
        os.close();
        is.close();

        System.out.println("Done...");
    }

    private static int getServerId() throws NumberFormatException, IOException {
        HttpURLConnection httpUrlConnection = (HttpURLConnection) new URL(
                "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver")
                .openConnection();
        httpUrlConnection.connect();
        InputStreamReader in = new InputStreamReader(
                (InputStream) httpUrlConnection.getContent());

        BufferedReader buff = new BufferedReader(in);

        return Integer.parseInt(buff.readLine());
    }
}
Emil Moe
  • 389
  • 5
  • 17

1 Answers1

0

I used the following class for my project. http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

It supports GET and POST requests.

You could use it like

RestClient client = new RestClient(your_url);
client.AddParam("sub", "upload");
//Other parameters here

try { 
    client.Execute(RequestMethod.POST); 
} catch (Exception e) {} 
joelkema
  • 19
  • 3