2013-08-03 98 views
0

根據Skydrive API(http://msdn.microsoft.com/en-us/library/live/hh826531.aspx#uploading_files),我在發佈請求的主體中將字節內容傳遞給byte []。 文件創建發生在skydrive服務器上,但是當我在skydrive中打開它時,它說「該文件存在損壞或損壞」。使用java將文件上傳到skydrive

體POST請求的

--A300x 內容處置:形狀數據; NAME = 「文件」;文件名= 「菜單的icon.png」 內容類型:應用/八位字節流

[[email protected] 

--A300x--

來自服務器的響應。用於將圖像轉換成字節

<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> 

代碼[]

字節[]圖像= FileUtils.readFileToByteArray(physicalfile);

任何線索?

編輯: 以下是代碼片段我用

我帶有些地區的屬性文件。

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); 

回答

0

在修剪客戶端ID和baseURL中的空格後,使用下面的代碼片段處理它。

String BOUNDARY_STRING="A300x"; 
     String urlString="https://apis.live.net/v5.0/"+folderPath+"/files?state="+getSkydriveClientId()+"&redirect_uri="+baseURL+"&access_token="+getAcessToken(); 
     URL connectURL = new URL(urlString);   
     HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY_STRING); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     conn.setUseCaches(false); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.connect(); 
     DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 
     dos.writeBytes("--" + BOUNDARY_STRING + "\r\n"); 
     dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\"\r\n"); 
     dos.writeBytes("Content-Type: application/octet-stream\r\n"); 
     dos.writeBytes("\r\n"); 
     File fileToUpload=Util.getFile(fileName); 
     FileInputStream fileInputStream = new FileInputStream(fileToUpload); 
     int fileSize = fileInputStream.available(); 
     int maxBufferSize = 8192; 
     int bufferSize = Math.min(fileSize, maxBufferSize); 
     byte[] buffer = new byte[bufferSize]; 
     int bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     while (bytesRead > 0) {   
     dos.write(buffer, 0, bytesRead); 
     int bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, buffer.length); 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 
     fileInputStream.close();   
     dos.writeBytes("\r\n"); 
     dos.writeBytes("--" + BOUNDARY_STRING + "--\r\n"); 
     dos.flush(); 
0

你出現在您的字節數組被調用toString() - 那正是其轉換爲[[email protected]。您沒有向我們展示任何您用來提出請求的代碼,但基本上您需要避免撥打toString()。我不知道API 是否接受 a byte[],但您至少應該嘗試找到一種方法,因爲這適用於您的內容。

編輯:在研究樣本代碼,看來你可以在InputStreamLiveConnectClient.uploadAsync通 - 所以只是包裝你的byte[]ByteArrayInputStream