zoukankan      html  css  js  c++  java
  • 文件上传工具类

    FileUtil.java

    package com.cmbchina.ccd.itpm.utils;
    
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.UUID;
    
    import static org.apache.tomcat.util.http.fileupload.FileUtils.cleanDirectory;
    
    public class FileUtil {
        private FileUtil() {
    
        }
    
        public static void uploadFile(byte[] file, String filePath, String fileName) throws IOException {
            File targetFile = new File(filePath);
            if (!targetFile.exists()) {
                if (!targetFile.mkdirs()) {
                    throw new IOException();
                }
            }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(filePath + fileName);
                out.write(file);
                out.flush();
            } catch (IOException e) {
                throw new IOException();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    
        public static boolean deleteFile(String fileName) {
            File file = new File(fileName);
            // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
            if (file.exists() && file.isFile()) {
                if (file.delete()) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
    
        }
    
        public static String renameToUUID(String fileName) {
            return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
        }
    
        /**
         * 文件删除方法
         *
         * @param fileAddress
         * @return
         */
        public static boolean deleteQuietly(String fileAddress) {
            File file = new File(fileAddress);
            if (file == null) {
                return false;
            } else {
                try {
                    if (file.isDirectory()) {
                        cleanDirectory(file);
                    }
                } catch (Exception var3) {
                    ;
                }
    
                try {
                    return file.delete();
                } catch (Exception var2) {
                    return false;
                }
            }
        }
    
        /**
         * 文件保存方法
         *
         * @param file
         * @param destination
         * @return
         * @throws IllegalStateException
         * @throws IOException
         */
        public static String saveFile(MultipartFile file, String destination) throws IllegalStateException, IOException {
            // 获取上传的文件名称,并结合存放路径,构建新的文件名称
            String filename = file.getOriginalFilename();
            File filepath = new File(destination, filename);
    
            // 判断路径是否存在,不存在则新创建一个
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();
            }
    
            // 将上传文件保存到目标文件目录
            file.transferTo(new File(destination + File.separator + filename));
            String fileMD5String = MD5Util.getFileMD5String(new File(destination + File.separator + filename));
            return fileMD5String;
        }
    
    }
    

      

  • 相关阅读:
    JS document.execCommand实现复制功能(带你出坑)
    jquery动态添加删除一行数据示例
    SpringBoot SpEL表达式注入漏洞-分析与复现
    Fastjson 1.2.22-24 反序列化漏洞分析
    udf提权原理详解
    ZZCMS v8.2 前台Insert注入+任意文件删除
    安恒杯 3月线上个人赛WriteUp
    SQLI LABS Stacked Part(38-53) WriteUp
    【转】Ubuntu16.04安装docker
    安装部署k8s-版本-1.13
  • 原文地址:https://www.cnblogs.com/sueyyyy/p/10542429.html
Copyright © 2011-2022 走看看