Java:使用 Graph API 在线更新 Sharepoint 上的 docx 文件 [英] Java : Update a docx file on Sharepoint online with Graph API

查看:54
本文介绍了Java:使用 Graph API 在线更新 Sharepoint 上的 docx 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Java 在线更新 Sharepoint 上的 docx 文件时遇到问题.

I have an issue updating a docx file on Sharepoint online with Java.

首先,我检查了构建 PUT 请求的 URL(此处: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}/content

First I checked the URL to build the PUT request (here : https://docs.microsoft.com/fr-fr/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http) and use this request : PUT /drives/{drive-id}/items/{item-id}/content

我首先使用服务来构建 URL:

I first use service to build the 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 中更新.我构建了响应:

I can note that the file is updated in Sharepoint. I build the response :

通过 Id 和 DriveId 更新文档

Update a document by Id and DriveId

{
  "documentName" : "C:\\Users\\me\\Documents\\uploadFolder\\myDoc.docx",
  "updateStatus" : "Success",
  "updatedAt" : 1590147553641
}

很遗憾,无法打开文件.

Unfortunatelly, the file can't be opened.

推荐答案

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

I finally solved the issue by using 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:使用 Graph API 在线更新 Sharepoint 上的 docx 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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