gpt4 book ai didi

java - 无法使用 Java SDK 将文件上传到 OneDrive

转载 作者:太空宇宙 更新时间:2023-11-04 09:37:05 26 4
gpt4 key购买 nike

我无法使用 MS Graph Java SDK 将文件上传到 OneDrive,也找不到任何基于 Java 的示例。

我在这里发现了一个类似的(但未回答)问题:MS Graph Java SDK: how to upload a large file to OneDrive?

但是,我已经设法拼凑了上面提出的问题中显示的更多代码(基于基于 .NET 的示例),但仍然没有成功。

FileSystemInfo fileSystemInfo = new FileSystemInfo();
fileSystemInfo.createdDateTime = new GregorianCalendar();
fileSystemInfo.lastModifiedDateTime = new GregorianCalendar();

DriveItemUploadableProperties properties = new DriveItemUploadableProperties();
properties.name = "test.jpeg";
properties.description = "TEST_DESCRIPTION";
properties.fileSystemInfo = fileSystemInfo;

IDriveItemCreateUploadSessionRequest request = this.graphServiceClient
.me()
.drive()
.root()
.createUploadSession(properties)
.buildRequest();

UploadSession session = request.post();

if(Objects.nonNull(session)) {
int maxSizeChunk = (320 * 1024) * 4;
String macOsPath = "[ADD YOUR VALID FILE PATH]";
File file = new File(macOsPath);
FileInputStream inputStream = new FileInputStream(file);

ChunkedUploadProvider<File> uploadProvider =
new ChunkedUploadProvider(session, this.graphServiceClient,
inputStream, maxSizeChunk, File.class);

IProgressCallback<File> callback = new IProgressCallback<File>() {
@Override
public void progress(long l, long l1) {
log.info("making progress: " + l + ", " + l1);
}

@Override
public void success(File file) {
log.info("Success! " + file.getName());
}

@Override
public void failure(ClientException e) {
log.info("Failure! " + e.getMessage());
}
};
uploadProvider.upload(callback, 0);
}

尝试发布上传 session 请求时发生错误:

UploadSession session = request.post();

返回以下错误,而不是返回上传 session :

{
"error": {
"code": "invalidRequest",
"message": "The request is malformed or incorrect.",
"innerError": {
"request-id": "b9d4026b-0d8f-44b4-9e9e-8d78a0ea5ea3",
"date": "2019-05-30T16:20:11"
}
}
}

最佳答案

我不完全理解需要 DriveItemUploadableProperties 的原因,但您不应设置其任何属性。

尝试使用这个:

UploadSession session = this.graphServiceClient
.me()
.drive()
.root
.itemWithPath("_hamilton.jpg")
.createUploadSession(new DriveItemUploadableProperties())
.buildRequest()
.post();

我个人发现最有用的例子是 SDK 本身的单元测试。在这种情况下,您可以在OneDriveTests.Java中找到它。 .

关于java - 无法使用 Java SDK 将文件上传到 OneDrive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56382362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com