如何在java的fileinputstream中获取azure blob存储内容?

pbossiut  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(672)

我正在开发一个api(springboot)。其中我需要在get方法的响应体中返回fileinputstream。
预期-当前端调用get files api时,浏览器上应该打开一个文件下载提示。
问题-我们不能使用blob.downloadtofile方法,因为它将在本地计算机(或api所在的主机)上下载文件,并且我们需要一些可以在api调用时将文件直接发送到前端的东西。不过,还有另一个方法blob.download(),它返回的outputstream在api调用时无法返回。
那么,有没有什么方法可以将outputstream转换为fileinputstream,而不必保存到设备上的实际文件中呢。
示例代码-

  1. public ByteArrayOutputStream downloadBlob() throws URISyntaxException, InvalidKeyException, StorageException, IOException {
  2. String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName + ";"
  3. + "AccountKey=" + accountKey;
  4. CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
  5. CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
  6. final CloudBlobContainer container = blobClient.getContainerReference("container name");
  7. CloudBlockBlob blob = container.getBlockBlobReference("image.PNG");
  8. ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
  9. blob.download(outputStream);
  10. System.out.println("file downloaded");
  11. return outputStream;
  12. }

注意-文件可以是任何类型。

kse8i1jr

kse8i1jr1#

下面是解决方案-在浏览器中点击api后,您的文件将在浏览器中下载-

  1. package com.example.demo.controllers;
  2. import com.azure.storage.blob.BlobClient;
  3. import com.azure.storage.blob.BlobContainerClient;
  4. import com.azure.storage.blob.BlobServiceClient;
  5. import com.azure.storage.blob.BlobServiceClientBuilder;
  6. import org.springframework.core.io.ByteArrayResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.http.HttpHeaders;
  9. import org.springframework.http.MediaType;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.ByteArrayOutputStream;
  15. @RestController
  16. public class FileDownload {
  17. @GetMapping(path="/download-file")
  18. public ResponseEntity<Resource> getFile(){
  19. String fileName = "Can not find symbol.docx";
  20. //azure credentials
  21. String connection_string = "Connection String of storage account on azure";
  22. BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connection_string).buildClient();
  23. BlobContainerClient containerClient= blobServiceClient.getBlobContainerClient("Container name");
  24. System.out.println(containerClient.getBlobContainerName());
  25. BlobClient blob = containerClient.getBlobClient(fileName);
  26. //creating an object of output stream to recieve the file's content from azure blob.
  27. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  28. blob.download(outputStream);
  29. //converting it to the inputStream to return
  30. final byte[] bytes = outputStream.toByteArray();
  31. ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
  32. ByteArrayResource resource = new ByteArrayResource(bytes);
  33. HttpHeaders headers = new HttpHeaders();
  34. headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
  35. return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
  36. .headers(headers)
  37. .body(resource);
  38. }
  39. }
展开查看全部

相关问题