MS Graph Java SDK:如何将大文件上传到 OneDrive? [英] MS Graph Java SDK: how to upload a large file to OneDrive?

查看:138
本文介绍了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.

希望对你有帮助.

这篇关于MS Graph Java SDK:如何将大文件上传到 OneDrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆