当前位置: 代码迷 >> 综合 >> File 与 MultipartFile 相互转换;计算文件夹的最深层数;递归删除某个文件夹;File对象转为InputStream
  详细解决方案

File 与 MultipartFile 相互转换;计算文件夹的最深层数;递归删除某个文件夹;File对象转为InputStream

热度:71   发布时间:2023-11-29 17:19:49.0

File --> InputStream

InputStream in = new FileInputStream(file);

 

MultipartFile对象 转换为 File 对象

/*** @Author: zcm* @DateTime: 2021/10/12 下午3:15* @Params: [org.springframework.web.multipart.MultipartFile]* @Return java.io.File* @Description: 将 MultipartFile对象 转换为 File 对象*/public static File multipartFileToFile(MultipartFile file) throws IOException {File toFile = null;if (file.equals("") || file.getSize() <= 0) {file = null;} else {InputStream ins = null;ins = file.getInputStream();toFile = new File(file.getOriginalFilename());inputStreamToFile(ins, toFile);ins.close();}return toFile;}/*** @Author: zcm* @DateTime: 2021/10/12 下午3:00* @Params: [java.io.InputStream, java.io.File]* @Return void* @Description: 获取流文件*/private static void inputStreamToFile(InputStream ins, File file) {try {OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();} catch (Exception e) {e.printStackTrace();}}

 将File对象 转为 MultipartFile

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 org.springframework.web.multipart.commons.CommonsMultipartFile;import java.io.*;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.Queue;

/*** @Author: zcm* @DateTime: 2021/10/12 下午3:01* @Params: [java.io.File]* @Return org.springframework.web.multipart.MultipartFile* @Description: 将File对象 转为 MultipartFile。* 转换出来后,注意对比一下,原生的MultipartFile 与 转换出来的 MultipartFile 的* 各个属性,因为计算文件幻数的时候,用的是MultipartFile。* MultipartFile不止一个实现类,File对象转MultipartFile时,注意看调试时 MultipartFile的实现类*/public static MultipartFile fileToMultipartFile(File file) {FileItem fileItem = createFileItem(file);MultipartFile multipartFile = new CommonsMultipartFile(fileItem);return multipartFile;}/*** @Author: zcm* @DateTime: 2021/10/12 下午3:12* @Params: [java.io.File]* @Return java.lang.String* @Description: 获取 MultipartFile 对象的 ContentType属性* 就是文件内容的类型*/public static String getContentType(File file) {String s = null;try {s = Files.probeContentType(file.toPath());} catch (IOException e) {e.printStackTrace();}return s;}/*** @Author: zcm* @DateTime: 2021/10/12 下午3:14* @Params: [java.io.File]* @Return org.apache.commons.fileupload.FileItem* @Description: 生成 new CommonsMultipartFile(fileItem) 的 FileItem*/private static FileItem createFileItem(File file) {FileItemFactory factory = new DiskFileItemFactory(10240, null);String contentType = FileUtils.getContentType(file);FileItem item = factory.createItem("file", contentType, true, file.getName());int bytesRead = 0;byte[] buffer = new byte[8192];try {FileInputStream fis = new FileInputStream(file);OutputStream os = item.getOutputStream();while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();fis.close();} catch (IOException e) {e.printStackTrace();}return item;}

计算文件夹最深层数

/*** @Author: zcm* @DateTime: 2021/10/11 下午8:09* @Params: [java.io.File, int]* @Return void* @Description: 计算文件夹的最大深度,如果超过 maximumOfFileTreeDepth 层,就抛出异常*/private static int  maximumOfFileTreeDepth = 5;public static void checkFileTreeDepth(File file, int depth){File[] files = file.listFiles();for(int i = 0; i < files.length; i++){File f = files[i];if (f.isDirectory()){depth++;if (depth < FileUtils.maximumOfFileTreeDepth){checkFileTreeDepth(f, depth);}else{throw new RuntimeException("文件层数太深了!");}depth--;}}return ;}

递归删除某个文件夹

/*** @Author: zcm* @DateTime: 2021/10/11 下午8:14* @Params: [java.lang.String]* @Return void* @Description: 递归删除这个路径下的所有文件对象*/public static void deleteAllFile(String path){File all = new File(path);		//获取其file对象File[] fs = all.listFiles();	//遍历path下的文件和目录,放在File数组中for(File f:fs){					//遍历File[]数组if(!f.isDirectory()){f.delete();}else {deleteAllFile(f.getAbsolutePath());}}all.delete();}

  相关解决方案