Note that there are some explanatory texts on larger screens.

plurals
  1. POJava skydrive rest file uploading
    text
    copied!<p>I tried uploading a file to skydrive with the rest api in java.</p> <p>Here is my code:</p> <pre><code> public void UploadFile(File upfile) { if (upload_loc == null) { getUploadLocation(); } HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(upload_loc + "?" + "access_token=" + access_token); try { MultipartEntity mpEntity = new MultipartEntity(null,"A300x",null); ContentBody cbFile = new FileBody(upfile, "multipart/form-data"); mpEntity.addPart("file", cbFile); post.setEntity(mpEntity); System.out.println(post.toString()); HttpResponse response = client.execute(post); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line2 = ""; while ((line2 = rd.readLine()) != null) { System.out.println(line2); } } catch (IOException ex) { Logger.getLogger(Onlab.class.getName()).log(Level.SEVERE, null, ex); } client.getConnectionManager().shutdown(); } </code></pre> <p>But when I try to run it, I get this error:</p> <pre><code>{ "error": { "code": "request_body_invalid", "message": "The request entity body for multipart form-data POST isn't valid. The expected format is:\u000d\u000a--[boundary]\u000d\u000aContent-Disposition: form-data; name=\"file\"; filename=\"[FileName]\"\u000d\u000aContent-Type: application/octet-stream\u000d\u000a[CR][LF]\u000d\u000a[file contents]\u000d\u000a--[boundary]--[CR][LF]" } } </code></pre> <p>My biggest problem is that I don't see the request itself. I couldn't find any usable toString method for that. I tried this forced boundary format, but I tried it with empty constructor too.</p> <p>My file is now a txt with some text, and I think the boundary is the main problem or I should be configuring some more parameters. When I see the variables in debugging mode everything looks the same as a guide in the msdn.</p> <p>I'm new in the rest world and if it possible I want to keep this apache lib with the simple to use HttpClient and HttpPost classes.</p> <p>Thanks in advance, and sorry for my english.</p> <p>EDIT: Ok, after a long sleep I decided to try the PUT method instead of POST. The code work fine with minimal changes:</p> <pre><code>public void UploadFile(File upfile) { if (upload_loc == null) { getUploadLocation(); } HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); String fname=upfile.getName(); HttpPut put= new HttpPut(upload_loc +"/"+fname+ "?" + "access_token=" + access_token); try { FileEntity reqEntity=new FileEntity(upfile); put.setEntity(reqEntity); HttpResponse response = client.execute(put); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line2 = ""; while ((line2 = rd.readLine()) != null) { System.out.println(line2); } } catch (IOException ex) { Logger.getLogger(Onlab.class.getName()).log(Level.SEVERE, null, ex); } client.getConnectionManager().shutdown(); } </code></pre> <p>But there is no answer for the first question yet.</p>
 

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