首页 > 基础资料 博客日记
java MultipartFile和File 最简单的互转
2024-02-25 13:00:05基础资料围观602次
文章java MultipartFile和File 最简单的互转分享给大家,欢迎收藏Java资料网,专注分享技术知识
MultipartFile和File 的区别
MuitipartFile 是 Spring 框架中用来处理文件上传的接口,它封装了文件上传的信息,比如文件名、文件类型等。
File 是Java 标准库中提供的文件操作类,用于描述文件信息,比如文件路径、文件大小等
总的来说,MultipantFile 是用来处理文件上传的,而 File 则是用来描述文件信息的。
MultipartFile 与 File 的 互相转换
1. MultipartFile 转 File
最常见的方式(通过文件流写入):
public File multipartFile2File (MultipartFile multipartFile) {
// 创建临时文件
String path = "export/demo.xlsx";
File file = new File(path);
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
// 获取文件输入流
inputStream = multipartFile.getInputStream();
if (!file.exists()) {
file.createNewFile();
}
// 创建输出流
outputStream = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int len;
// 写入到创建的临时文件
while ((len = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 关流
try {
if (outputStream != null) {
outputStream.close();
}
if (outputStream != null) {
inputStream.close();
}
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return file;
}
最简单的方式:
使用Spring中的FileCpopyUtils类的copy()方法将MultipartFile 转换为File类型
public File multipartFile2File (MultipartFile multipartFile) {
String path = "export/demo.xlsx";
File file = new File(path);
try {
if (!file.exists()) {
file.createNewFile();
}
// 底层也是通过io流写入文件file
FileCopyUtils.copy(multipartFile.getBytes(), file);
} catch (Exception e) {
throw new RuntimeException(e);
}
return file;
}
2. File 转 MultipartFile
使用org.springframework.mock.web.MockMultipartFile 需要导入spring-test.jar
public MultipartFile file2MultipartFile () {
String path = "export/demo.xlsx";
File file = new File(path);
MultipartFile multipartFile;
try {
FileInputStream fileInputStream = new FileInputStream(file);
multipartFile = new MockMultipartFile("copy"+file.getName(),file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
System.out.println(multipartFile.getName()); // 输出demo.xlsx
fileInputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return multipartFile;
}
使用CommonsMultipartFile
public MultipartFile file2MultipartFile () {
String path = "export/demo.xlsx";
File file = new File(path);
MultipartFile multipartFile;
try {
DiskFileItem fileItem2 = (DiskFileItem) new DiskFileItemFactory().createItem("file", ContentType.MULTIPART.getValue(), true, file.getName());
//也可以用IOUtils.copy(inputStream,os);
Files.copy(Paths.get(file.getAbsolutePath()), fileItem2.getOutputStream());
multipartFile = new CommonsMultipartFile(fileItem2);
} catch (Exception e) {
throw new RuntimeException(e);
}
return multipartFile;
}
又是摸鱼的一天,,,,,
文章来源:https://blog.csdn.net/m0_56324585/article/details/131723857
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:

