【JavaUtils】Java 文件相关的工具类集合

Java 文件相关的工具类集合

  • 一、File 转 MultipartFile(入参FileInputStream)
  • 二、File 转 MultipartFile(入参File)
  • 三、递归删除文件夹及其内容

一、File 转 MultipartFile(入参FileInputStream)

import lombok.extern.slf4j.Slf4j;
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* File 转 MultipartFile
*
* @param fileInputStream FileInputStream
* @param fileType        文件类型
* @return MultipartFile
 */

public static MultipartFile fileInputStreamToMultipartFile(FileInputStream fileInputStream, String fileType) {
   FileItemFactory factory = new DiskFileItemFactory(16, null);
   String textFieldName = "file";
   FileItem item = factory.createItem(textFieldName, "text/plain", true, fileType);
   try {
       int length = 0;
       byte[] buffer = new byte[1024];
       OutputStream outputStream = item.getOutputStream();
       while ((length = fileInputStream.read(buffer)) > -1) {
           outputStream.write(buffer, 0, length);
       }
       outputStream.close();
       fileInputStream.close();
       return new CommonsMultipartFile(item);
   } catch (IOException e) {
       log.error(e.getMessage(), e);
   }
   return null;
}

二、File 转 MultipartFile(入参File)

import lombok.extern.slf4j.Slf4j;
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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* File 转 MultipartFile
*
* @param file     File
* @param fileType 文件类型
* @return MultipartFile
*/
public static MultipartFile fileToMultipartFile(File file, String fileType) {
   FileInputStream fileInputStream = null;
   try {
       fileInputStream = new FileInputStream(file);
       return fileInputStreamToMultipartFile(fileInputStream, fileType);
   } catch (Exception e) {
       log.error(e.getMessage(), e);
   } finally {
       if (fileInputStream != null) {
           try {
               fileInputStream.close();
           } catch (IOException e) {
               log.error(e.getMessage(), e);
           }
       }
   }
   return null;
}

三、递归删除文件夹及其内容

/**
* 递归删除文件夹及其内容
*
* @param folder 文件
* @return MultipartFile
*/
public static void deleteFolder(File folder) {
   if (folder.isDirectory()) {
       File[] files = folder.listFiles();
       if (files != null) {
           // 递归删除子文件夹和文件
           for (File file : files) {
               deleteFolder(file);
           }
       }
   }
   // 删除文件夹或文件
   if (!folder.delete()) {
       throw new ServiceException("递归删除文件夹及其内容");
   }
}

你可能感兴趣的:(java,开发语言)