Microsoft graph:使用 Put 请求更新文档 Java

Microsoft graph : Updating a document with a Put request Java

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

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

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

我尝试使用 main class 并传递现有参数进行编码。要更新文档,我调用方法更新文档:

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;
}