简体   繁体   English

无法使用 SharePoint 下载 docx 文件

[英]Can not download docx file using SharePoint file download REST API & Java

I got the attached img value as response from the SharePoint server but can not write it on the docx file.我从 SharePoint 服务器获得了附加的 img 值作为响应,但无法将其写入 docx 文件。 The file download API using Postman is giving same response and if I save Postman response to docx file then it is saving perfectly but not from Java side. The file download API using Postman is giving same response and if I save Postman response to docx file then it is saving perfectly but not from Java side. If I write same response to a docx file using Java, the file is corrupted.如果我使用 Java 对 docx 文件写入相同的响应,则该文件已损坏。

I am using this REST API to download the file from Sharepoint:我正在使用这个 REST API 从 Sharepoint 下载文件:

SiteURL/_api/web/getfilebyserverrelativeurl('relativeURL/Shared Documents/test.docx')/$value

Most likely server is returning the document (docx) as binary ( application/octet-stream ).很可能服务器将文档 (docx) 作为二进制 ( application/octet-stream ) 返回。 Your code saves the document's string representation:您的代码保存了文档的字符串表示形式:

        ResponseEntity<String> response = restTemplate.exchange(fileUrl, HttpMethod.GET, request, String.class);
        String responseStrFromSharePoint = response.getBody();

That's why the file could not be decoded by application.这就是为什么应用程序无法解码文件的原因。 Instead save the exact binary (bytes) returned by the server as the following snippet shows:而是保存服务器返回的确切二进制文件(字节),如下面的代码片段所示:

        ResponseEntity<byte[]> response = restTemplate.exchange(fileUrl, HttpMethod.GET, request, byte[].class);
        byte[] responseStrFromSharePoint = response.getBody();

Other parts of the code seems fine.代码的其他部分似乎很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM