gpt4 book ai didi

java - 微软图 : Updating a document with a Put request Java

转载 作者:行者123 更新时间:2023-12-01 10:34:00 25 4
gpt4 key购买 nike

基于 MS 图形文档,我看到我可以更新一个 driveItem(文件)并将其放在特定的共享点驱动器中。该应用程序作为守护程序运行(无需用户登录)。

为此,我使用这个入口点:

PUT /drives/{drive-id}/items/{item-id}/content

我尝试使用主类进行编码并传递现有参数。要更新文档,我调用方法更新文档:
UpdateDocumentResponseModel updatedDocument = fileGraphs.updateDocument(token, DRIVELIBID, DOCUMENTID, INPUTPATH, DOCUPDATE);

被调用的方法旨在构建 URL 并为 PUT 请求准备数据:
public UpdateDocumentResponseModel updateDocument(String accessToken,
String driveLibId,
String documentId,
String inpuPath,
String docName) throws MalformedURLException {
String fullPath = inpuPath + docName;
URL url = new URL("https://graph.microsoft.com/v1.0/drives/" + driveLibId + "/items/" + documentId + "/content");
return requestsBuilder.updateDocument(accessToken, url, fullPath);
}

现在在这个阶段我必须提出请求:
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();

try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Accept","application/json");
httpPut.setHeader("Content-Type","plain/text");
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");

// read the file and convert to stream
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", new File(fullPath),
ContentType.APPLICATION_OCTET_STREAM, "file.ext");
HttpEntity multipart = builder.build();
httpPut.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPut);


System.out.println("\nSending 'UPDATE' request to URL : " + url);
System.out.println("Response Code : " + response.getStatusLine());

// set the response
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Success");
} catch (IOException e) {
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Failure" + e.getCause());
e.printStackTrace();
}

return returnValue;

}

我的问题是,当我发回 docx 文件时,该文件未正确上传。该文件已上传(好东西),但在 sharepoint 在线门户中无法读取,必须下载。
我的第二个问题是我可以接受任何类型的文件:doc、docx、ppt、xls、xlsx、txt、图像...

我确实认为我会遇到其他问题。是否有一个库可以帮助我处理文件扩展名并正确转换文件。我的问题是我不必专门处理 MS Office 文件,而是处理任何类型的文件。

我的问题显然在这里:
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", new File(fullPath),
ContentType.APPLICATION_OCTET_STREAM, "file.ext");
HttpEntity multipart = builder.build();
httpPut.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPut);

谢谢 !

最佳答案

我终于通过使用 ByteArrayInputStream 解决了这个问题...

我替换了:

// read the file and convert to stream
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", new File(fullPath),
ContentType.APPLICATION_OCTET_STREAM, "file.ext");
HttpEntity multipart = builder.build();
httpPut.setEntity(multipart);

有了这个:
byte[] fileContent = FileUtils.readFileToByteArray(new File(fullPath));
httpPut.setEntity(new InputStreamEntity(new ByteArrayInputStream(fileContent), fileContent.length));

最后我的方法是这样的:
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();

try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Content-Type", "text/plain");
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");

byte[] fileContent = FileUtils.readFileToByteArray(new File(fullPath));
httpPut.setEntity(new InputStreamEntity(new ByteArrayInputStream(fileContent), fileContent.length));

// httpPut.setEntity(new StringEntity(String.valueOf(in), StandardCharsets.UTF_8));
CloseableHttpResponse response = client.execute(httpPut);

System.out.println("\nSending 'PUT' request to URL : " + url);
System.out.println("Response Code : " + response.getStatusLine());

// set the response
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Success");
} catch (IOException e) {
returnValue.setDocumentName(fullPath);
returnValue.setUpdatedAt(new Date());
returnValue.setUpdateStatus("Failure" + e.getCause());
e.printStackTrace();
}
return returnValue;
}

关于java - 微软图 : Updating a document with a Put request Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60541711/

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