当前位置:网站首页>Minio file download problem - inputstream:closed
Minio file download problem - inputstream:closed
2022-07-06 12:21:00 【A pole】
Preface
Recently, I am building a simple blog system , Because uploading and downloading pictures is a big demand , And then I Relatively poor I want to challenge myself , Just use Minio To build oss service . Official website :https://min.io/.
In order to save everyone's time , Let me start with a brief overview , So that everyone can judge whether to continue to look down :
In the use of minio When downloading files , The code keeps reporting errors java.io.IOException: closed , Finally, I found that it was because try () {} Caused by grammar problems .
cause
Its download object Methods , Probably pass in the file name and then return a InputStream, What I need to do is to get the stream , adopt controller Back to front end .
My idea is to put stream Go straight back to the front end , Instead of downloading locally . Of course, you need to encapsulate a tool class MinioUtils , Then call the method in the tool class , Get it first inputStream , And then in controller Layer to do conversion .
after
Tried many methods , I also refer to many blogs , I can't get it out , Has been an error java.io.IOException: closed , I'm directly stupid , The key is that there is no call in my code stream.close(); ah !
I'm thinking of , Is it because the length of byte stream is uncertain , So I ended the download ahead of schedule , It's mainly my understanding of http The agreement only stays at the level of understanding , I don't quite understand response The various parameters in it , I'm not sure in the code I wrote , Is there any way to automatically calculate the length for me . So I used stream.available(); To get its length , And define a byte Array , But this is also wrong , When I call this method , The code has already reported closed It's wrong ...
I thought it might be minio The problem of , Once wanted to give up minio, But I think I've been doing it for so long , It's a little uneconomical to give up .
reason
It took two days , Finally found the problem . In fact, it's not about the interface , It is Java Of try () {} Grammar problem .
public void downloadFile(String fileName) throws Exception {
try (InputStream inputStream = minioClient.getObject(minioConfig.getBucketName(), storePath))
{
return inputStream;
} catch (Exception e) {
logger.error(" File download exception ");
e.printStackTrace();
}
}
// The above code doesn't seem to call closed(); , But it's actually return After that, it has been called
// This is because of the try () {} grammar , In the exit Sentence block {} when , Will automatically call closed Method to release resources
resolvent
For this reason , My code keeps reporting errors , After finding out why , The problem will be solved naturally , Here are two solutions :
// Pass in the input stream for processing
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(" File download exception ");
e.printStackTrace();
}
}
// Or change the grammar to ordinary 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(" File download failed ");
}
}
Postscript
After that , Look back at the original blog and official documents , It seems that most of them write like this , In fact, there is no big problem , Just because I want to make such a package , So my code thinking is the same as theirs , But the implementation has achieved different results .
边栏推荐
- Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
- Basic operations of databases and tables ----- modifying data tables
- JS變量類型以及常用類型轉換
- Embedded startup process
- 【ESP32学习-1】Arduino ESP32开发环境搭建
- MySQL时间、时区、自动填充0的问题
- JS正则表达式基础知识学习
- 基于Redis的分布式ID生成器
- Time slice polling scheduling of RT thread threads
- JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
猜你喜欢

Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction

基于Redis的分布式ID生成器

Common properties of location

JS regular expression basic knowledge learning

ESP8266连接onenet(旧版MQTT方式)

Characteristics, task status and startup of UCOS III

Esp8266 uses Arduino to connect Alibaba cloud Internet of things

基于Redis的分布式锁 以及 超详细的改进思路

MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解

高通&MTK&麒麟 手機平臺USB3.0方案對比
随机推荐
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
小天才电话手表 Z3工作原理
js 变量作用域和函数的学习笔记
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
Basic knowledge of lithium battery
Learning notes of JS variable scope and function
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
@The difference between Autowired and @resource
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
[esp32 learning-1] construction of Arduino esp32 development environment
Cannot change version of project facet Dynamic Web Module to 2.3.
Arm pc=pc+8 is the most understandable explanation
Important methods of array and string
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
JS数组常用方法的分类、理解和运用
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
ESP learning problem record
(一)R语言入门指南——数据分析的第一步
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
Amba, ahb, APB, Axi Understanding