8

Downloading files to skydrive using java

 3 years ago
source link: https://www.codesd.com/item/downloading-files-to-skydrive-using-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Downloading files to skydrive using java

advertisements

As per Skydrive api(http://msdn.microsoft.com/en-us/library/live/hh826531.aspx#uploading_files), I am passing the content of the image in byte[] in the body of the post request. the file creation happens on the skydrive server, but when i open it in skydrive it says "the file appers to be damaged or corrupted".

body of the post request

--A300x Content-Disposition: form-data; name="file"; filename="menu-icon.png" Content-Type: application/octet-stream

    [B@11f3043

--A300x--

the response from the server.

<html>
  <head>
    <script type="text/javascript">
      window.location = "http://localtesting.com/mywebapp-1.0-SNAPSHOT#state=000000004C0FA618&result=%7b%22id%22%3a%22file.918bb0fbaf82f760.918BB0FBAF82F760!139%22%2c%22name%22%3a%22menu-icon.png%22%2c%22source%22%3a%22https%3a%2f%2f1m61va.tuk.livefilestore.com%2fy2mhncDTiOhUVFZxp08gi3yAFhp7OQ2-UYuQhnC_Obpoo4q5tG6onLuJz2mLJkJh6lUW5l8Cq2KBxvrLRrZ0bk6V7xmfso47cJWAw1fKE8bFJw%2fmenu-icon.png%3fdownload%26psid%3d1%22%7d";
    </script>
  </head>
</html>

code for converting the image into byte[]

byte[] image=FileUtils.readFileToByteArray(physicalfile);

Any clue?

EDIT: Following is the code snippet I use

I'm taking some parts from properties file.

env.properties
part1=--A300x\u000d\u000aContent-Disposition: form-data; name=\"file\"; filename=\"
part2=\"\u000d\u000aContent-Type: application/octet-stream\u000d\u000a\u000d\u000a
part3=\u000d\u000a--A300x--

java file

String part1=bundle.getString("part1");
String part2= fileName+bundle.getString("part2");
String part3=bundle.getString("part3");

byte[] imageByteArray=FileUtils.readFileToByteArray(physicalfile);

PostMethod postMethod= new PostMethod("https://apis.live.net/v5.0/"+folderPath+"/files?state="+getSkydriveClientId()+"&redirect_uri="+baseURL+"&access_token="+getAcessToken());
postMethod.setRequestHeader("Content-Type","multipart/form-data; boundary=A300x");
postMethod.setRequestBody(part1+part2+imageByteArray+part3);

HttpClient httpClient=new HttpClient();
httpClient.executeMethod(postMethod);


You appear to be calling toString() on your byte array - that's what's converting it to [B@11f3043. You haven't shown us any of the code you're using to make the request, but basically you need to avoid calling toString(). I don't know whether the API accepts a byte[], but you should at least try to find a method which takes one, as that would be appropriate for your content.

EDIT: Looking at the sample code, it appears you can pass in an InputStream to LiveConnectClient.uploadAsync - so just wrap your byte[] in a ByteArrayInputStream.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK