簡體   English   中英

Java:在線更新 Sharepoint 上的 docx 文件,使用圖表 API

[英]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}/content

我首先使用服務來構建 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM