2012-07-14 88 views
2

發佈的圖像,以谷歌驅動器時,我遇到了以下問題IOException異常:當插入文件,以谷歌驅動

java.io.IOException: insufficient data written 
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:2822) 
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:83) 
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:895) 
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:280) 
at com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309) 
at com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331) 

我相信它的與此相關的:http://code.google.com/p/google-api-java-client/issues/detail?id=521

是否有辦法繞過這個?我想知道如果可能插入一個文件,而不使用從谷歌驅動器sdk可恢復的上傳API?

回答

2

我相信我回答了我的問題是如何做一個直接上傳:

Insert insert = this.driveClient.files().insert(body, mediaContent); 
insert.getMediaHttpUploader().setDirectUploadEnabled(true); 
File result = insert.execute(); 

但是,還不能確定寫入錯誤數據不足的原因。

+0

我很困惑,或者這是否不解決這個錯誤嗎? – mmcrae 2015-03-19 03:42:24

0

使用選項setDirectUploadEnabled(假)的insert.execute()開始上傳第一小盤瞬間我得到的最小塊大小之前的意思。你必須完成request.getPart(「fleInputName」),並隨後開始啓動上傳程序。

我解決了這個用這樣的阻塞線程:

public class GetPartThread extends Thread { 

     private HttpServletRequest request; 
     private String inputAttr; 

     private Part part; 



     public GetPartThread(HttpServletRequest request, String inputAttr) { 
      super(); 
      this.request = request; 
      this.inputAttr = inputAttr; 
     } 


     @Override 
     public void run(){ 
       synchronized(this){ 
        try { 
         part = request.getPart(inputAttr); 
         notify(); 

        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
     } 

     public Part getPart() { 
      return part; 
     } 

     public void setPart(Part part) { 
      this.part = part; 
     } 

然後用您的驅動器上傳方法使用這樣的:

GetPartThread getPart = new GetPartThread(request, "templatefile"); 
getPart.start(); 
synchronized(getPart){ 
try{ 
    System.out.println("Waiting for Part upload to complete..."); 
    getPart.wait(); 
}catch(InterruptedException e){ 
    e.printStackTrace(); 
} 
System.out.println("Part upload Finished..."); 

Part part = getPart.getPart(); 

//Here continue with processing your "part" and upload it to drive with the method you set for uploading to drive ... } 

這應該工作。

注意:在使用驅動器可恢復媒體上傳時,不要使用setConvert(真),這將拋出一個異常,因爲上傳到驅動,而不是上傳完成後的第一個塊皈依的開始。