Google Drive InputStreamContent需要设置长度 [英] Google Drive InputStreamContent requires length to be set
问题描述
将文件上传到Google Drive的示例通常使用FileContent类,这很容易理解并按预期工作。
一个InputStream被暴露,因为输入可能不一定源自一个文件。我曾尝试使用InputStreamContent类,但是如果未设置长度,则会抛出IllegalArgumentException: 线程中的异常 mainjava.lang.IllegalArgumentException
,位于com.google.common.base.Preconditions.checkArgument(Preconditions.java:72)
位于com.google.api.client.googleapis.media.MediaHttpUploader.getMediaContentLength (MediaHttpUploader.java:315)
,位于com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:333)
,位于com.google.api.client.googleapis.media .MediaHttpUploader.upload(MediaHttpUploader.java:252)
,位于com.google.api.services.drive.Drive $ Files $ Insert.executeUnparsed(Drive.java:309)
,位于com.google.api .services.drive.Drive $ Files $ Insert.execute(Drive.java:331)
当我可能会将InputStream复制到一个字节数组中,然后从中获取大小,但似乎并不是最有效的方法。这是预期的实施? javadoc并没有指出需要设置的值,这似乎是误导性的:
/ **
*如果不知道,则设置内容长度或小于零。
*
*< p>
*默认为{@代码-1}。
*< / p>
*
* @since 1.5
* /
public InputStreamContent setLength(long length){
this.length = length;
返回此;
这是一个已知问题,详情如下:
http:/ /code.google.com/p/google-api-java-client/issues/detail?id=591
我们会在1.12 google-api-java-client release。
Examples of uploading a file to Google Drive typically use the FileContent class, this is easy to understand and works as expected.
In my current case, I only have an InputStream exposed, as the input may not necessarily originate from a file. I have tried to use the InputStreamContent class, however if the length is not set, an IllegalArgumentException is thrown:
Exception in thread "main" java.lang.IllegalArgumentException
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:72)
at com.google.api.client.googleapis.media.MediaHttpUploader.getMediaContentLength(MediaHttpUploader.java:315)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:333)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:252)
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)
While I could copy the InputStream into a byte array perhaps and then obtain the size from that, it doesn't seem to be the most efficient approach. Is this the intended implementation? The javadoc does not indicate that the value needs to be set, which seems to be misleading:
/**
* Sets the content length or less than zero if not known.
*
* <p>
* Defaults to {@code -1}.
* </p>
*
* @since 1.5
*/
public InputStreamContent setLength(long length) {
this.length = length;
return this;
}
This is a known issue, details are here: http://code.google.com/p/google-api-java-client/issues/detail?id=591
We will get this fixed in the 1.12 google-api-java-client release.
这篇关于Google Drive InputStreamContent需要设置长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!