微软图。用一个Put请求更新一个文档Java

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

根据MS图文档,我看到我可以更新一个driveItem(文件),并把它放在一个特定的Sharepoint驱动器。该应用程序是作为一个守护程序运行(没有用户登录)。

对于这一点,我使用这个切入点。

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

我尝试使用主类和传递现有的参数来编码。要更新一个文档,我调用一个方法更新文档。

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,图像......

我想我还会遇到其他问题。是否有一个lib可以帮助我处理文件扩展名并正确转换文件。我的问题是,我不需要专门处理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);

谢谢!

java microsoft-graph sharepoint-online
1个回答
3
投票

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

我把.NET文件替换为

// 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;
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.