当前位置:网站首页>Minio文件下载问题——inputstream:closed
Minio文件下载问题——inputstream:closed
2022-07-06 09:17:00 【阿杆.】
前言
最近在搭建一个简单的博客系统,由于上传下载图片是个比较大的需求,然后我又 比较穷 想挑战一下自己,就使用 Minio 来搭建的 oss 服务。官网:https://min.io/。
为了节省大家的时间,我先写个简短的概述,以便大家判断要不要继续往下看:
在使用minio进行文件下载的时候,代码一直报错 java.io.IOException: closed ,最终发现原来是因为 try () {} 语法问题所导致的。
起因
其下载 object 的方法,大概是传入文件名然后返回一个InputStream,我要做的是把获取到的流,通过controller返回到前端。
我的思路是把stream直接返回到前端,而不在本地进行下载。那当然需要封装一个工具类 MinioUtils ,然后去调用工具类中的方法,先获取到 inputStream ,然后再在 controller 层去做转换。
经过
尝试过很多方法,也参考了很多博客,一直弄不出来,一直报错 java.io.IOException: closed ,我直接人傻了,关键是我代码里也没调用 stream.close(); 啊!
我就想着,会不会是因为不确定字节流的长度,所以提前结束了下载,主要是我对http协议只停留在了解的地步,并不太懂response里面的各种参数,也不确定我写的代码里,会不会有某个方法自动帮我计算好这个长度。于是我就用 stream.available(); 去获取它的长度,并且根据此来定义一个byte数组,但是这样也不对,当我调用这个方法的时候,代码就已经报 closed 的错误了。。。
我以为可能是 minio 的问题,曾一度想放弃minio,但是又想着我都弄了这么久了,就放弃有点不划算。
原因
花了两天时间,终于找了问题所在。其实并非是接口的问题,而是Java的 try () {} 语法问题。
public void downloadFile(String fileName) throws Exception {
try (InputStream inputStream = minioClient.getObject(minioConfig.getBucketName(), storePath))
{
return inputStream;
} catch (Exception e) {
logger.error("文件下载异常");
e.printStackTrace();
}
}
// 上面的代码看似没有调用 closed(); ,但实际上在return完之后就已经调用了
// 这是因为使用的 try () {} 语法,在退出 语句块{} 时,就会自动调用closed方法释放资源
解决方法
由于这个原因,导致我的代码一直报错,找到原因之后,问题自然就解决了,下面提供两种解决方法:
// 将输入流传入进来处理
public void downloadFile(String fileName, ServletOutputStream outputStream) throws Exception {
try (InputStream inputStream = minioClient.getObject(GetObjectArgs
.builder()
.bucket(bucketName)
.object(fileName)
.build());
) {
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
logg.error("文件下载异常");
e.printStackTrace();
}
}
// 或者把语法换成普通的try{}catch{}
public InputStream download(String fileName) throws Exception {
try {
return minioClient.getObject(GetObjectArgs
.builder()
.bucket(bucketName)
.object(fileName)
.build());
} catch (Exception e) {
e.printStackTrace();
throw new Exception("文件下载失败");
}
}
后记
解决之后,再回头看看原来看的博客和官方文档,好像他们大都是这样写的,其实并没有很大的问题,只是因为我想做一个这样的封装,所以导致我的代码思路即使跟他们一样,但是执行出来却得到了不同的结果。
边栏推荐
- I2C bus timing explanation
- Gallery's image browsing and component learning
- Basic knowledge of lithium battery
- Selective sorting and bubble sorting [C language]
- Basic operations of databases and tables ----- modifying data tables
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
- arduino获取随机数
- ESP学习问题记录
- Inline detailed explanation [C language]
- 锂电池基础知识
猜你喜欢

js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

几个关于指针的声明【C语言】

AMBA、AHB、APB、AXI的理解

E-commerce data analysis -- User Behavior Analysis

Générateur d'identification distribué basé sur redis

JS regular expression basic knowledge learning
![[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree](/img/6e/0802a92511ac50a652afa1678ad28c.jpg)
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree

uCOS-III 的特点、任务状态、启动

RT thread API reference manual

Arduino uno R3 register writing method (1) -- pin level state change
随机推荐
uCOS-III 的特点、任务状态、启动
.elf .map .list .hex文件
几个关于指针的声明【C语言】
Who says that PT online schema change does not lock the table, or deadlock
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
List and set
列表的使用
【ESP32学习-2】esp32地址映射
[Red Treasure Book Notes simplified version] Chapter 12 BOM
Kaggle competition two Sigma connect: rental listing inquiries
Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
[template] KMP string matching
Whistle+switchyomega configure web proxy
inline详细讲解【C语言】
Pytoch temperature prediction
基于Redis的分布式ID生成器
Time slice polling scheduling of RT thread threads
Selective sorting and bubble sorting [C language]
高通&MTK&麒麟 手机平台USB3.0方案对比
Pytorch four commonly used optimizer tests