当前位置:网站首页>Conversion between commonsmultipartfile and file
Conversion between commonsmultipartfile and file
2022-07-07 23:16:00 【codepig16】
One 、 CommonsMultipartFile To File( Curve of national salvation )
Here is the way to save the country with curves : First use CommonsMultipartFile Of getInputStream() Method to get a InputStream Object of type , then InputStream Type into File
// take InputStream Type into File type
private static void inputStreamToFile(InputStream ins, File file) {
// Convert the output stream into a file
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while((bytesRead = ins.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}catch(Exception e) {
throw new RuntimeException(" call inputStreamToFile Cause abnormal :"+e.getMessage());
}finally {
try {
if(os != null) {
os.close();
}
if(ins != null) {
ins.close();
}
}catch(IOException e) {
throw new RuntimeException("inputStreamToFile close io An exception occurred while :"+e.getMessage());
}
}
}
// Use inputStreamToFile Method
// When creating a file , Add any empty path , Create the file , You can delete it later
File xxFile = new File(" A non repeating path ");
try {
xxFile.createNewFile();
} catch (IOException e) {
// Handling exception information
}
try {
inputStreamToFile(xxCommonsMultipartFile.getInputStream(), xxFile);
} catch (IOException e) {
// Handling exception information
}
//xxFile Is the transformed File file
Two 、File To CommonsMultipartFile
// hold File Turn into CommonsMultipartFile
public FileItem createFileItem(File file, String fieldName) {
//DiskFileItemFactory(): Construct a configured instance of this class
// The first parameter threshold( threshold ): In bytes . Below this threshold item Will be stored in memory , Above this threshold item Will be stored as a file
// The second parameter data repository: The directory where the files will be created . Used to configure when creating file items , The temporary folder used when the file item is greater than the threshold , The system default temporary file path is adopted by default
FileItemFactory factory = new DiskFileItemFactory(16, null);
//fieldName: The name of the form field ; The second parameter ContentType; The third parameter isFormField; The fourth one : file name
FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
FileInputStream fis = null;
OutputStream os = null;
try {
fis = new FileInputStream(file);
os = item.getOutputStream();
while((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);// from buffer Get data from and write
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null) {
os.close();
}
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return item;
}
// Use
FileItem fileItem = createFileItem(xxFile, " Form field name ");
CommonsMultipartFile xxCMF = new CommonsMultipartFile(fileItem);
//xxCMF Is the transformed CommonsMultipartFile file
边栏推荐
- Talk about DART's null safety feature
- Grid
- Circumvention Technology: Registry
- 聊聊 Dart 的空安全 (null safety) 特性
- Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- U盘拷贝东西时,报错卷错误,请运行chkdsk
- JMeter interface automated test read case, execute and write back result
- 30讲 线性代数 第五讲 特征值与特征向量
- Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
猜你喜欢

微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT

海内外技术人们“看”音视频技术的未来

V20变频器手自动切换(就地远程切换)的具体方法示例

I wish you all the best and the year of the tiger

Introduction to redis and jedis and redis things

消息队列与快递柜之间妙不可言的关系

The wonderful relationship between message queue and express cabinet

2021ICPC上海 H.Life is a Game Kruskal重构树

聊聊支付流程的设计与实现逻辑

Brush question 3
随机推荐
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
One question per day - pat grade B 1002 questions
系统架构设计师备考经验分享:论文出题方向
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
Bea-3xxxxx error code
Advantages and disadvantages of rest ful API
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
云原生数据仓库AnalyticDB MySQL版用户手册
聊聊支付流程的设计与实现逻辑
Introduction to anomaly detection
Txt file virus
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
力扣解法汇总648-单词替换
Matlab 信号处理【问答随笔·2】
微信论坛交流小程序系统毕业设计毕设(1)开发概要
Install Fedora under RedHat
消息队列与快递柜之间妙不可言的关系
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
Brush question 4