MS Graph Java SDK:如何将大文件上传到 OneDrive?
我正在尝试从我的 Java 应用程序将文件上传到 OneDrive,但我不了解流程,也没有看到 SKD 中用于文件上传的任何文档或方法.
I'm trying to upload file to a OneDrive from my Java application, but i dont understand the flow and dont see any documentation or methods within SKD for file upload.
我发现的唯一流程是:
driveClient.me().drive().root().createUploadSession(uploadProperties).buildRequest().post()
但这会导致 NPE: source
.
如何将内容设置为 InputStream ?
how can i set the content as InputStream ?
您必须使用 ChunkedUploadProvider.下面是上传大文件的官方示例代码:https://docs.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=java
You must use the ChunkedUploadProvider. Here is the official sample code for uploading large files: https://docs.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=java
对我来说,这个示例适用于小于 1.9921875 GB 的文件.不幸的是,较大的文件因上传会话失败太多次"错误而失败.解决方案是使用 File.length() 方法获取文件大小:
For me this sample worked for files with size less than 1.9921875 GB. Unfortunately larger files failed with "Upload session failed too many times" error. The solution is to get the file size using File.length() method:
long streamSize = file.length();
代替
long streamSize = (long)fileStream.available();
如示例中所示.
发生这种情况是因为 fileStream.available() 将可用字节作为整数返回,而整数的最大值(以字节为单位)为 ~2GB.
This happens because fileStream.available() returns the available bytes as integer and integer's max value in bytes is ~2GB.
希望对你有帮助.