2016-03-21 36 views
0

我正尝试使用outputstream将文件上传到Google云端硬盘。随着下载我能够得到InputStream这样:使用输出流将文件上传到Google云端硬盘

public void downloadStarted() throws Exception 
    { 
     HttpResponse resp = drive.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute(); 
     serverInputStream = resp.getContent(); 
    } 

有关上载我有一个工作的这个样本测试:

private static File uploadFile(boolean useDirectUpload) throws IOException 
{ 
    File fileMetadata = new File(); 
    fileMetadata.setTitle(UPLOAD_FILE.getName()); 

    FileContent mediaContent = new FileContent("*/*", UPLOAD_FILE); 

    Drive.Files.Insert insert = drive.files().insert(fileMetadata, mediaContent); 

    MediaHttpUploader uploader = insert.getMediaHttpUploader(); 

    uploader.setDirectUploadEnabled(useDirectUpload); 
    uploader.setProgressListener(new FileUploadProgressListener()); 
    return insert.execute(); 
} 

,但我真的需要outputstream,而且不知道如何得到它。任何帮助?

回答

1

我认为用Google Drive HTTP API直接编写OutputStream是不可能的。 Drive.Files.create()insert()接受AbstractInputStreamContent,所以它必须是InputStream。一种解决方法是这样的:

ByteArrayOutputStream out = new ByteArrayOutputStream(); 
// use out 
File file = drive.files().create(fileMetadata, new ByteArrayContent("", 
    out.toByteArray())).setFields("id").execute(); 

另一个想法可能工作是使用PipedInputStream/PipedOutputStream。将drive.files().create(fileMetadata, pipedInputstream).setFields(id").execute()放在Thread的内部,这样它就不会阻塞。

相关问题