使用sharepoint api下载文件:文件已损坏

q0qdq0h2  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(476)

我正在开发一个java库,用于使用graphapi在sharepoint上执行基本操作。
我使用soap ui对此入口点进行调用:

https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{item-id}/content

我得到了一个原始的回答:

%PDF-1.6
%âãÏÓ

1751 0 obj
<</Filter/FlateDecode/First 98/Length 322/N 11/Type/ObjStm>>stream

hޜԽJ1†á[ÉL’ó“–m,md±ÁElTü)¼{3“wXYDØ©¾3!ç<)&I^kˆ!ymÁ¤gë¥ÍE ...
endstream
endobj
startxref
2993893
%%EOF

我好像在检索输入流。在httprequest类中,我尝试构建一个返回inputstream的响应对象。我的属性文件inputstream是一个inputstream:

SharePointDownloadResponseModel returnValue = new SharePointDownloadResponseModel();
InputStream inputStream = new ByteArrayInputStream(response.toString().getBytes(StandardCharsets.UTF_8));
returnValue.setFileInputStream(inputStream);
return returnValue;

现在在我的manager类中,我尝试将输入流保存在硬盘中。我处理两个案子。第一种情况,我有一个文件名和一个文件夹来存储文件。我的请求对象:

if(request.getDownloadFolder() != null && request.getFileName() !=null) {
   InputStream initialStream = returnValue.getFileInputStream();
   FileOutputStream fos = new FileOutputStream(request.getDownloadFolder() + "/" + request.getFileName());
   BufferedOutputStream bos = new BufferedOutputStream(fos );

   // Read bytes from URL to the local file
   byte[] buffer = new byte[4096];
   int bytesRead = 0;

   System.out.println("Downloading " + request.getFileName());
   while ((bytesRead = initialStream.read(buffer)) != -1) {
       bos.write(buffer, 0, bytesRead);
   }

   bos.flush();
   // Close destination stream
   bos.close();
   // Close URL stream
   initialStream.close();
}

文档是在应该创建的位置创建的,但文件已损坏,无法打开。我想知道现阶段的问题是什么。

jvlzgdj9

jvlzgdj91#

我终于解决了我的问题。下面是显示我的实现的基本方法:

public class DownloadFile {
    public static void main(String[] args) throws IOException {
        String url = "https://graph.microsoft.com/v1.0/drives/{driveId}/items/{itemId}/content";
        SharePointCredentialRequest sharePointCredentialRequest = new SharePointCredentialRequest(Constants.TENANT_CLIENT_ID,
                Constants.TENANT_CLIENT_SECRET, Constants.TENANT_AUTHORITY);

        String token = Utils.getToken(sharePointCredentialRequest);
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Authorization", "Bearer " + token);

        try (CloseableHttpResponse response = client.execute(httpGet)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println(response.getAllHeaders().length);
                System.out.println(entity.getContentEncoding());
                System.out.println(entity.getContentLength());
                System.out.println(entity.getContentType().getElements().toString());

                try  {
                    // do something useful with the stream
                    InputStream inputStream = IOUtils.toBufferedInputStream(response.getEntity().getContent());
                    File targetFile = new File("C:\\myFolder\\kant.pdf");
                    FileUtils.copyInputStreamToFile(inputStream, targetFile);
                } catch (IOException | UnsupportedOperationException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

相关问题