1
votes

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

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

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

I build the request :

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

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

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.

1

1 Answers

1
votes

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