当前位置:网站首页>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
边栏推荐
- Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
- [untitled] reprint melting ice - track icedid server with a few simple steps
- 【编译原理】词法分析设计实现
- Network security - install CentOS
- ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
- Unity dynamically merges mesh textures
- Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
- The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- Solution: prompt "unsupported video format" when inserting avi format video into the message
猜你喜欢

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

Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?

小程序多种开发方式对比-跨端?低代码?原生?还是云开发?

Wechat forum exchange applet system graduation design completion (1) development outline

微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板

When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK

微信论坛交流小程序系统毕业设计毕设(4)开题报告

USB(十五)2022-04-14

Wechat forum exchange applet system graduation design completion (6) opening defense ppt

Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
随机推荐
2021-01-12
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
Binary tree
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
[microservices SCG] gateway integration Sentinel
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
Mitsubishi PLC SLmP (MC) protocol
ArcGIS: two methods of attribute fusion of the same field of vector elements
十四、数据库的导出和导入的两种方法
Network security - information query of operating system
Coreseek:第二步建索引及測试
FPGA基础篇目录
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
Network security - joint query injection
I wish you all the best and the year of the tiger
定位到最底部[通俗易懂]
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
Dynamics 365 find field filtering
./ setup. Insufficient sh permission