Java : 使用 Graph API 在线更新 Sharepoint 上的 docx 文件
Java : Update a docx file on Sharepoint online with Graph API
我在使用 Java 在线更新 Sharepoint 上的 docx 文件时遇到问题。
首先,我检查了 URL 以构建 PUT 请求(此处:https://docs.microsoft.com/fr-fr/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http)并使用此请求:PUT /drives/{drive-id}/items/{item-id} /内容
我首先使用服务构建 URL :
@Override
public UpdateDocumentResponseModel updateDocument(String accessToken, String libId, String docId, String filePath) throws MalformedURLException {
URL url = new URL("https://graph.microsoft.com/v1.0/drives/" + libId + "/items/" + docId + "/content");
return documentRequestBuilder.updateDocument(accessToken, url, filePath);
}
我构建请求:
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();
Tika tika = new Tika();
String mimeType = tika.detect(fullPath);
System.out.println("Test mime type: " + mimeType);
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Content-Type", mimeType);
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
// read the file and convert to stream
// Get an input stream for the file
InputStream in = new FileInputStream(fullPath);
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;
}
我注意到该文件已在 Sharepoint 中更新。
我建立响应:
通过 Id 和 DriveId 更新文档
{
"documentName" : "C:\Users\me\Documents\uploadFolder\myDoc.docx",
"updateStatus" : "Success",
"updatedAt" : 1590147553641
}
很遗憾,文件无法打开。
我终于通过使用 ByteArrayInputStream 解决了这个问题...
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 在线更新 Sharepoint 上的 docx 文件时遇到问题。
首先,我检查了 URL 以构建 PUT 请求(此处:https://docs.microsoft.com/fr-fr/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http)并使用此请求:PUT /drives/{drive-id}/items/{item-id} /内容
我首先使用服务构建 URL :
@Override
public UpdateDocumentResponseModel updateDocument(String accessToken, String libId, String docId, String filePath) throws MalformedURLException {
URL url = new URL("https://graph.microsoft.com/v1.0/drives/" + libId + "/items/" + docId + "/content");
return documentRequestBuilder.updateDocument(accessToken, url, filePath);
}
我构建请求:
public UpdateDocumentResponseModel updateDocument(String accessToken, URL url, String fullPath) {
UpdateDocumentResponseModel returnValue = new UpdateDocumentResponseModel();
Tika tika = new Tika();
String mimeType = tika.detect(fullPath);
System.out.println("Test mime type: " + mimeType);
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut httpPut = new HttpPut(String.valueOf(url));
httpPut.setHeader("Authorization", "Bearer " + accessToken);
httpPut.setHeader("Content-Type", mimeType);
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
// read the file and convert to stream
// Get an input stream for the file
InputStream in = new FileInputStream(fullPath);
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;
}
我注意到该文件已在 Sharepoint 中更新。 我建立响应:
通过 Id 和 DriveId 更新文档
{
"documentName" : "C:\Users\me\Documents\uploadFolder\myDoc.docx",
"updateStatus" : "Success",
"updatedAt" : 1590147553641
}
很遗憾,文件无法打开。
我终于通过使用 ByteArrayInputStream 解决了这个问题...
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;
}