Note that there are some explanatory texts on larger screens.

plurals
  1. POPost data Java with HttpUrlConnection
    text
    copied!<p>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.</p> <p>I've based my code on what I found here: <a href="http://www.experts-exchange.com/Programming/Languages/Java/Q_20370019.html" rel="nofollow">http://www.experts-exchange.com/Programming/Languages/Java/Q_20370019.html</a></p> <p>If any help, I succeeded to create a solution using PHP and cURL, but the final solution must be in Java.</p> <pre><code>$post = array( 'sub' =&gt; 'upload', 'login' =&gt; $user, 'password' =&gt; $pass, 'filecontent' =&gt; '@'. $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); </code></pre> <p>My Java source:</p> <pre><code>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&amp;login=***&amp;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()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload