且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MS Graph Java SDK:如何将大文件上传到 OneDrive?

更新时间:2023-01-15 15:18:24

您必须使用 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.

希望对你有帮助.