【问题标题】:Downloading file with SharePoint API: file damaged使用 SharePoint API 下载文件:文件损坏
【发布时间】:2020-11-18 09:54:19
【问题描述】:

我正在使用 Graph API 为 SharePoint 上的基本操作开发一个 Java 库。

我使用 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 的响应对象。我的属性 fileInputStream 是一个 InputStream:

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

现在在我的管理器类中,我尝试将输入流保存在硬盘中。我处理2个案例。第一种情况,我有一个 fileName 一个文件夹来存储文件。我的请求对象:

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

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

【问题讨论】:

  • 尝试使用 POSTMAN/MS Graph Explorer 使用相同的 API 调用重现该问题,看看您是否仍然可以重现该问题。
  • 我的问题不是 Postman 问题,无法重现。在 SOAP UI / Postman 我检索原始流作为响应。我的问题是将此流作为对象值传递并将其转换为文件。

标签: java microsoft-graph-api inputstream


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2015-07-14
    • 2015-07-16
    • 2017-12-04
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多