File文件操作工具类

import com.byd.entity.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author s.bankaiqi
 * @since 2023/03/30
 */
@Slf4j
public class FileUtil {

    // 下划线
    public static final String UNDER_LINE = "_";

    /**
     * 上传文件(返回完整的路径信息)
     * @param file 文件
     * @param uploadPath 上传路径
     */
    public static ResultVo uploadFile(MultipartFile file,String uploadPath) {
        // 1.获取一个新的文件名
        String newFileName = getNewFileName(file);
        if (StringUtil.isEmpty(newFileName)) {
            log.error("【上传文件】转换文件名称失败");
            return ResultVo.error("【上传文件】转换文件名称失败");
        }
        // 2.获取文件上传路径
        if (StringUtil.isEmpty(uploadPath)) {
            log.error("【上传文件】获取文件上传路径失败");
            return ResultVo.error("【上传文件】获取文件上传路径失败");
        }
        uploadPath = uploadPath + File.separator +  DateUtil.getCurrentDate();
        // 3.生成上传目录
        File uploadDir = mkdirs(uploadPath);
        if (!uploadDir.exists()) {
            log.error("【上传文件】生成上传目录失败");
            return ResultVo.error("【上传文件】生成上传目录失败");
        }
        // 4.文件全路径
        String fileFullPath = uploadPath + File.separator + newFileName;
        log.info("上传的文件:" + file.getName() + "," + file.getContentType() + ",保存的路径为:" + fileFullPath);
        try {
            // 5.上传文件
            doUploadFile(file, fileFullPath);
        } catch (IOException e) {
            log.error("【上传文件】上传文件报IO异常,异常信息为{}", e.getMessage());
            return ResultVo.error(e.getMessage());
        }
        return ResultVo.success(fileFullPath);
    }

    /**
     * 上传文件(返回文件静态资源路径,)
     * @param file 文件
     * @param uploadPath 上传路径
     */
    public static ResultVo uploadFile2(MultipartFile file,String uploadPath) {
        // 1.获取一个新的文件名
        String newFileName = getNewFileName(file);
        if (StringUtil.isEmpty(newFileName)) {
            log.error("【上传文件】转换文件名称失败");
            return ResultVo.error("【上传文件】转换文件名称失败");
        }
        // 2.获取文件上传路径
        if (StringUtil.isEmpty(uploadPath)) {
            log.error("【上传文件】获取文件上传路径失败");
            return ResultVo.error("【上传文件】获取文件上传路径失败");
        }
        uploadPath = uploadPath + File.separator +  DateUtil.getCurrentDate();
        // 3.生成上传目录
        File uploadDir = mkdirs(uploadPath);
        if (!uploadDir.exists()) {
            log.error("【上传文件】生成上传目录失败");
            return ResultVo.error("【上传文件】生成上传目录失败");
        }
        // 4.文件相对路径
        String fileFullPath = uploadPath + File.separator + newFileName;//全路径
        String fileRelativePath = File.separator +  DateUtil.getCurrentDate() + File.separator + newFileName;//静态资源路径
        log.info("上传的文件:" + file.getName() + "," + file.getContentType() + ",保存的路径为:" + fileFullPath);
        try {
            // 5.上传文件
            doUploadFile(file, fileFullPath);
        } catch (IOException e) {
            log.error("【上传文件】上传文件报IO异常,异常信息为{}", e.getMessage());
            return ResultVo.error(e.getMessage());
        }
        return ResultVo.success(fileRelativePath);
    }


    /**
     * 将上传的文件转换为一个新的文件名
      * @param file 文件
     */
    public static String getNewFileName(MultipartFile file) {
        // 1.获取上传的文件名称(包含后缀。如:test.jpg)
        String originalFilename = file.getOriginalFilename();
        log.info("【上传文件】上传的文件名为{}", originalFilename);
        // 2.以小数点进行分割
        String[] split = originalFilename.split("\\.");
        String newFileName = null;
        if (null == split || split.length == 0) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        if (1 == split.length) {
            // 3.此文件无后缀
            newFileName = builder.append(originalFilename).append(UNDER_LINE).append(System.nanoTime()).toString();
            return newFileName;
        }
        // 4.获取文件的后缀
        String fileSuffix = split[split.length - 1];
        for (int i = 0; i < split.length - 1; i++) {
            builder.append(split[i]);
            if (null != split[i + 1] && "" != split[i + 1]) {
                builder.append(UNDER_LINE);
            }
        }
        newFileName = builder.append(System.nanoTime()).append(".").append(fileSuffix).toString();
        return newFileName;
    }

    /**
     * 生成相应的目录
      * @param path
     */
    public static File mkdirs(String path) {
        File file = new File(path);
        if(!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        return file;
    }


    /**
     * 上传文件
      * @param file
     * @param path
     * @throws IOException
     */
    public static void doUploadFile(MultipartFile file, String path) throws IOException {
        Streams.copy(file.getInputStream(), new FileOutputStream(path), true);
    }


    /**
     * 递归删除目录下的所有文件及子目录下所有文件
      * @param file
     */
    public static boolean deleteFile(File file) {
        if (!file.exists()) {
            return false;
        }
        if (file.isDirectory()) {
            String[] children = file.list();
            //递归删除目录中的子目录下
            for (int i=0; i downloadFile(String filePath, HttpServletResponse response) {
        File file = new File(filePath);
        // 1.参数校验
        if (!file.exists()) {
            log.error("【下载文件】文件路径{}不存在", filePath);
            return ResultVo.error("文件不存在");
        }
        // 2.下载文件
        log.info("【下载文件】下载文件的路径为{}", filePath);
        return FileUtil.downloadFile(file, response);
    }

    /**
     * 下载文件
     * @param file
     * @param response
     * @return
     */
    public static ResultVo downloadFile(File file, HttpServletResponse response) {
        try {
            // 1.设置响应头
            setResponse(file, response);
        } catch (UnsupportedEncodingException e) {
            log.error("文件名{}不支持转换为字符集{}", file.getName(), "UTF-8");
            return ResultVo.error(e.getMessage());
        }
        // 2.下载文件
        return doDownLoadFile(file, response);
    }

    /**
     * 设置响应头
     * @param file
     * @param response
     * @throws UnsupportedEncodingException
     */
    public static void setResponse(File file, HttpServletResponse response) throws UnsupportedEncodingException {
        // 清空response
        response.reset();
        //表示所有的域都可以接受
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许所有的方法
        response.setHeader("Access-Control-Allow-Methods","POST,GET,DELETE,PUT,OPTIONS");
        //请求得到结果的有效期
        response.setHeader("Access-Control-Max-Age","3600");
        response.setHeader("Access-Control-Allow-Headers","x-requested-with,Content-Type,access-control-allow-origin,version-info");
        //该值是一个boolean值,表示允许发送cookie,默认情况下cookie不包含在cores中,设置为true
        response.setHeader("Access-Control-Allow-Credentials","true");

        response.setCharacterEncoding("UTF-8");
        // 返回给客户端类型,任意类型
        response.setContentType("application/octet-stream");
        // Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
        // attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        // 告知浏览器文件的大小
        response.addHeader("Content-Length", String.valueOf(file.length()));
    }

    /**
     * 通过流的形式下载文件
     * @param file
     * @param response
     * @return
     */
    public static ResultVo doDownLoadFile(File file, HttpServletResponse response) {

        // 将输入流中的数据循环写入到响应输出流中,而不是一次性读取到内存,通过响应输出流输出到前端
        try (InputStream in = new FileInputStream(file);
             OutputStream out = response.getOutputStream()) {
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            log.info("【文件下载】文件下载成功");
            return null;
        } catch (FileNotFoundException e){
            log.error("【文件下载】下载文件时,没有找到相应的文件,文件路径为{}", file.getAbsolutePath());
            return ResultVo.error(e.getMessage());
        } catch (IOException e) {
            log.error("【文件下载】下载文件时,出现文件IO异常");
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 校验文件为TXT
     * @param file
     * @return
     */
    public static boolean checkTxtExtension(MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (StringUtil.isEmpty(filename)) {
            log.info("【校验TXT文件后缀】TXT文件名为空");
            return false;
        }
        int index = filename.lastIndexOf(".");
        if (index == -1) {
            log.info("【校验TXT文件后缀】TXT文件名中没有点号");
            return false;
        }
        String extension = filename.substring(index + 1);
        return "txt".equals(extension);
    }


    /**
     * 写入文件
     * @param filePath 文件路径
     * @param textList 文件行内容
     * @throws IOException  异常信息
     */
    public  static void writeTxtFile(String filePath, List textList) {
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            File file = new File(filePath);
            file.createNewFile();
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            for (String text : textList) {
                bufferedWriter.write(text);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

统一返回类

import com.byd.common.constant.ResultCodeEnum;
import lombok.Data;

/**
 * @author s.bankaiqi
 * @since 2023/03/30
 */
@Data
public class ResultVo {

    // 错误码
    private Integer code;

    // 提示信息
    private String msg;

    // 返回的数据
    private T data;

    // 判断是否成功
    public boolean checkSuccess() {
        return ResultCodeEnum.SUCCESS.getCode().equals(this.code);
    }

    public static ResultVo success() {
        return success(null);
    }

    public static ResultVo success(String msg, Object object) {
        ResultVo result = new ResultVo();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(msg);
        result.setData(object);
        return result;
    }

    public static ResultVo success(Object object) {
        ResultVo result = new ResultVo();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg("成功");
        result.setData(object);
        return result;
    }

    public static ResultVo success(Integer code, Object object) {
        return success(code, null, object);
    }

    public static ResultVo success(Integer code, String msg, Object object) {
        ResultVo result = new ResultVo();

        result.setCode(code);
        result.setMsg(msg);
        result.setData(object);
        return result;
    }

    public static ResultVo error(String msg) {
        ResultVo result = new ResultVo();
        result.setCode(ResultCodeEnum.ERROR.getCode());
        result.setMsg(msg);
        return result;
    }

    public static ResultVo error(Integer code, String msg) {
        ResultVo result = new ResultVo();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static ResultVo error(String msg,Object object) {
        ResultVo result = new ResultVo();
        result.setCode(ResultCodeEnum.ERROR.getCode());
        result.setMsg(msg);
        result.setData(object);
        return result;
    }

    public static ResultVo error(Integer code, String msg, Object object) {
        ResultVo result = new ResultVo();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(object);
        return result;
    }
}

返回枚举类

import lombok.Getter;

/**
 * @author s.bankaiqi
 * @since 2023/03/30
 */
@Getter
public enum ResultCodeEnum {

    SUCCESS(200, "成功"),
    CHECK_SUCCESS(203, "导入校验成功"),
    CONFIRM(302, "覆盖确认"),
    CONFIRM_UPDATE(303, "更新确认"),
    PARAM_ERROR(400, "参数错误"),
    ERROR(500, "错误"),
    FILE_NOT_EXIST(501, "文件不存在"),
    CLOSE_FAILD(504, "关闭流失败");

    private Integer code;
    private String message;

    ResultCodeEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

}

你可能感兴趣的:(java,java)