Java 使用outputstream将文件上载到Google驱动器

Java 使用outputstream将文件上载到Google驱动器,java,google-drive-api,Java,Google Drive Api,我正试图使用outputstream将一个文件上传到谷歌硬盘。通过下载,我可以通过以下方式获得InputStream: public void downloadStarted() throws Exception { HttpResponse resp = drive.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute(); serve

我正试图使用
outputstream
将一个文件上传到谷歌硬盘。通过下载,我可以通过以下方式获得
InputStream

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

但是我真的需要
输出流
,不知道如何获得它。有什么帮助吗?

我认为不可能用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()
放在
线程
中,这样它就不会阻塞。

我认为不可能用Google drive HTTP API直接编写
OutputStream
接受
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()
放在
线程中,这样它就不会阻塞。

由于性能不好,我不喜欢管道

怎么样

但这有点棘手-P

api强制我们使用
AbstractInputStreamContent
,但在我的代码中 从不调用
InputStream



google api::google api服务驱动器:v3-rev12-1.21.0

由于性能差,我不喜欢管道

怎么样

但这有点棘手-P

api强制我们使用
AbstractInputStreamContent
,但在我的代码中 从不调用
InputStream


谷歌api::谷歌api服务驱动器:v3-rev12-1.21.0