Java。用Graph API在线更新Sharepoint上的一个docx文件。

问题描述 投票:0回答:1

我有一个问题,在Sharepoint在线用Java更新一个docx文件。

首先我检查了建立PUT请求的URL(这里: https:/docs.microsoft.comfr-frgraphapidriveitem-put-content?view=graph-rest-1.0&tabs=http。),并使用这个请求:PUT drives{drive-id}items{item-id}content

我首先使用service来构建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
}

不幸的是,该文件无法打开。

java sharepoint microsoft-graph microsoft-graph-sdks
1个回答
0
投票

我最终通过使用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;
}
© www.soinside.com 2019 - 2024. All rights reserved.