MultipartFile和File互转
一、M转F
详细方法
public static File multipartFileToFile(String outFilePath, MultipartFile multiFile) { // 获取文件名 if (null == multiFile) { return null; } String fileName = multiFile.getOriginalFilename(); if (null == fileName) { return null; } try { File toFile; InputStream ins; ins = multiFile.getInputStream(); //指定存储路径 toFile = new File(outFilePath.concat(File.separator).concat(multiFile.getOriginalFilename())); inputStreamToFile(ins, toFile); return toFile; } catch (Exception e) { e.printStackTrace(); } return null; } private static void inputStreamToFile(InputStream ins, File file) { try (OutputStream os = new FileOutputStream(file)) { int bytesRead; int bytes = 8192; byte[] buffer = new byte[bytes]; while ((bytesRead = ins.read(buffer, 0, bytes)) != -1) { os.write(buffer, 0, bytesRead); } ins.close(); } catch (Exception e) { e.printStackTrace(); } }
封装方法
File file = new File(path); FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
其他方法
//创建file对象File file = new File(path);//将multipartFile转为filemultipartFile.transferTo(file);return file;
二、F转M
public static MultipartFile fileToMultipartFile(File file) { MultipartFile result = null; if (null != file) { try (FileInputStream input = new FileInputStream(file)) { result = new MockMultipartFile(file.getName().concat("temp"), file.getName(), "text/plain", input); } catch (IOException e) { e.printStackTrace(); } } return result; }
三、其他的一些功能
package gov.cma.ew.qc.common.utils;import cn.hutool.core.text.CharSequenceUtil;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.springframework.web.multipart.MultipartFile;import java.io.*;import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class FileUtil { private FileUtil () { } public static List<String> getDirectory(File file) { File[] fList = file.listFiles(); if (fList == null || fList.length == 0) { return Collections.emptyList(); } List<String> files = new ArrayList<>(); for (File f : fList) { if (!f.isDirectory()) { //这里将列出所有的文件 files.add(f.getAbsolutePath()); } getDirectory(f); } return files; } public static File multipartFileToFile(String outFilePath, MultipartFile multiFile) { // 获取文件名 if (null == multiFile) { return null; } String fileName = multiFile.getOriginalFilename(); if (null == fileName) { return null; } try { File toFile; InputStream ins; ins = multiFile.getInputStream(); //指定存储路径 toFile = new File(outFilePath.concat(File.separator).concat(multiFile.getOriginalFilename())); inputStreamToFile(ins, toFile); return toFile; } catch (Exception e) { e.printStackTrace(); } return null; } private static void inputStreamToFile(InputStream ins, File file) { try (OutputStream os = new FileOutputStream(file)) { int bytesRead; int bytes = 8192; byte[] buffer = new byte[bytes]; while ((bytesRead = ins.read(buffer, 0, bytes)) != -1) { os.write(buffer, 0, bytesRead); } ins.close(); } catch (Exception e) { e.printStackTrace(); } } public static FileItem createFileItem(String filePath, String fileName){ String fieldName = "file"; FileItemFactory factory = new DiskFileItemFactory(16, null); FileItem item = factory.createItem(fieldName, "text/plain", false,fileName); File newFile = new File(filePath); int bytesRead; int b = 8192; byte[] buffer = new byte[b]; try (FileInputStream fis = new FileInputStream(newFile); OutputStream os = item.getOutputStream()) { while ((bytesRead = fis.read(buffer, 0, b)) != -1) { os.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } return item; } public static String getFileContent(String filePath) { if (CharSequenceUtil.isBlank(filePath)) { return null; } File file = new File(filePath); StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { //构造一个BufferedReader类来读取文件 String str; while ((str = br.readLine()) != null) { //使用readLine方法,一次读一行 sb.append(str); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }}
最后奉上hutool 工具类包
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId></dependency>
来源地址:https://blog.csdn.net/AA8310888193aaa/article/details/126856974