如何在Java中的FileInputStream中获得天蓝色的Blob存储内容?

问题描述

我正在使用API​​(春季启动)。在这种情况下,我需要在get方法的Response主体中返回FileInputStream。

预期- 前端调用get-files API时,应在浏览器上打开文件下载提示

问题-我们不能使用blob.downloadToFile方法,因为它将在本地计算机(或托管API的位置)上下载文件,并且我们需要一些可以直接发送文件方法到API调用的前端。 但是,还有另一种方法 blob.download(),该方法返回的OutputStream不能在API调用时返回。

因此,有什么方法可以将OutputStream转换为FileInputStream而不保存到设备上的实际文件中。

示例代码-

public ByteArrayOutputStream downloadBlob() throws URISyntaxException,InvalidKeyException,StorageException,IOException {
        String storageConnectionString = "DefaultEndpointsProtocol=https;" + "AccountName=" + accountName + ";"
                + "AccountKey=" + accountKey;

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        final CloudBlobContainer container = blobClient.getContainerReference("container name");


        CloudBlockBlob blob = container.getBlockBlobReference("image.PNG");
        ByteArrayOutputStream outputStream =new ByteArrayOutputStream();
        blob.download(outputStream);
        System.out.println("file downloaded");

        return outputStream;
    }

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

解决方法

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

package com.example.demo.controllers;

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


@RestController
public class FileDownload {
    @GetMapping(path="/download-file")
    public ResponseEntity<Resource> getFile(){
        String fileName = "Can not find symbol.docx";
        //azure credentials
        String connection_string = "Connection String of storage account on azure";

        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connection_string).buildClient();

        BlobContainerClient containerClient= blobServiceClient.getBlobContainerClient("Container name");
        System.out.println(containerClient.getBlobContainerName());

        BlobClient blob = containerClient.getBlobClient(fileName);

        //creating an object of output stream to recieve the file's content from azure blob.
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        blob.download(outputStream);

        //converting it to the inputStream to return
        final byte[] bytes = outputStream.toByteArray();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        ByteArrayResource resource = new ByteArrayResource(bytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=\"" + fileName + "\"");

        return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM)
                .headers(headers)
                .body(resource);
    }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...