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

    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;
            }
        }
    }
  • 相关阅读:
    Sky中国War3的旗帜
    2008流行趋势发布暨07届学生毕业秀(上海大学主办)
    随便写一下
    六一节——小朋友们快乐
    HEI
    Update my blog to improve my idea.
    由ipod引起的奇遇记
    加勒比海盗
    “老板娘”请客吃饭
    蚂蚁工坊
  • 原文地址:https://www.cnblogs.com/zhengyuanyuan/p/10688175.html
Copyright © 2011-2022 走看看