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