Google drive api 将文件插入Google驱动器时发生IOException
我在向google drive发布图像时遇到以下问题:Google drive api 将文件插入Google驱动器时发生IOException,google-drive-api,Google Drive Api,我在向google drive发布图像时遇到以下问题: 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(NetHttpReq
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)
我认为这与此有关:
有没有办法避免这种情况?我想知道是否可以在不使用google drive sdk的可恢复上传api的情况下插入文件?我想我已经回答了我自己关于如何直接上传的问题:
Insert insert = this.driveClient.files().insert(body, mediaContent);
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
File result = insert.execute();
但是,仍然不能确定数据写入错误不足的原因。使用选项setDirectUploadEnabled(false)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(true)。这将引发异常,因为上载到驱动器的第一个块的转换将启动,而不是在上载完成后启动