当前位置:网站首页>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
边栏推荐
- Redhat下安装fedora
- 微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
- Binary tree
- oc 可变參数传递
- Statistical method for anomaly detection
- Wechat forum exchange applet system graduation design completion (4) opening report
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
- 2021ICPC上海 H.Life is a Game Kruskal重构树
- I wish you all the best and the year of the tiger
- 二叉树(Binary Tree)
猜你喜欢
[microservices SCG] gateway integration Sentinel
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
消息队列与快递柜之间妙不可言的关系
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
微信论坛交流小程序系统毕业设计毕设(3)后台功能
Binary tree
JMeter-接口自动化测试读取用例,执行并结果回写
Brush question 4
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
随机推荐
三菱PLC slmp(mc)协议
[record of question brushing] 3 Longest substring without duplicate characters
Network security sqlmap and DVWA explosion
【刷题记录】3. 无重复字符的最长子串
14、 Two methods of database export and import
Redhat下安装fedora
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
Brush question 3
V20变频器手自动切换(就地远程切换)的具体方法示例
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
Circumvention Technology: Registry
Innovation today | five key elements for enterprises to promote innovation
Advantages and disadvantages of rest ful API
js 获取对象的key和value
OC variable parameter transfer
Line test - graphic reasoning - 1 - Chinese character class
智慧社區和智慧城市之間有什麼异同
Mitsubishi PLC SLmP (MC) protocol
Unity dynamically merges mesh textures