上传文件工具类

package com.akb.hfcx.csp.common.util;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class SuperUploadFileUtil {
    
    public static String uploadFile(CommonsMultipartFile file, String path) throws IllegalStateException, IOException {
        String url = "";
        if (file != null && file.getSize() > 0) {
            // 文件上传
            String oldFilename = file.getOriginalFilename();
            String newFilename = UUID.randomUUID().toString() + oldFilename.substring(oldFilename.lastIndexOf("."));
            File headImgPath = new File(path);
            if (!headImgPath.exists()) {
                headImgPath.mkdirs();
            }
            File file2 = new File(path + File.separator + newFilename);
            file.transferTo(file2);
            url = newFilename;
        }
        return url;
    }
    
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10688175.html