当前位置:网站首页>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
边栏推荐
- JMeter-接口自动化测试读取用例,执行并结果回写
- Network security sqlmap and DVWA explosion
- 解决:信息中插入avi格式的视频时,提示“unsupported video format”
- Specific method example of V20 frequency converter manual automatic switching (local remote switching)
- Binary tree
- GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
- 系统架构设计师备考经验分享:论文出题方向
- 2021-01-12
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- 七月第一周
猜你喜欢
I wish you all the best and the year of the tiger
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
Brush question 3
Wechat forum exchange applet system graduation design completion (4) opening report
七月第一周
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
二叉树(Binary Tree)
Wechat forum exchange applet system graduation design (2) applet function
STL标准模板库(Standard Template Library)一周学习总结
随机推荐
Network security - install CentOS
Install Fedora under RedHat
PMP项目管理考试过关口诀-1
Unity dynamically merges mesh textures
网络安全-对操作系统进行信息查询
Byte hexadecimal binary understanding
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
网格(Grid)
Why does the market need low code?
聊聊支付流程的设计与实现逻辑
为什么市场需要低代码?
./ setup. Insufficient sh permission
Network security - phishing
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
I wish you all the best and the year of the tiger
智慧社区和智慧城市之间有什么异同
Innovation today | five key elements for enterprises to promote innovation
十四、数据库的导出和导入的两种方法
648. 单词替换
十三、系统优化