上传工具类

来源:互联网 发布:用ps淘宝详情页怎么做 编辑:程序博客网 时间:2024/03/29 16:05
import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Date;import java.util.Properties;import javax.imageio.ImageIO;import org.apache.commons.lang.math.RandomUtils;import org.joda.time.DateTime;import org.springframework.web.multipart.MultipartFile;/** 文件上传工具类 */public class FileUploadUtil {private static String STORE_URL = "";private static String LINK_URL = "";        /** 允许上传的格式 */    public final static String[] IMAGE_TYPE = new String[] { ".bmp", ".jpg", ".jpeg", ".gif", ".png" };        static{    try { Properties properties = new Properties();properties.load(FileUploadUtil.class.getResourceAsStream("/upload.properties"));STORE_URL = (String) properties.get("PRODUCT_IMG_URL");LINK_URL = (String) properties.get("LINK_URL");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}    }    /**     * 上传图片方法(只能上传图片)     * @param file SpringMvc的上传接口     * @param status 0 上传产品 1 上传银行卡  2 上传头像 3 机构 4 身份证  5房产 6名片     * @return 上传成功 返回路径 上传失败 返回Null     */    public static String uploadImage(MultipartFile file,Integer status){         try {            boolean isLegal = false;            /** 1.进行图片格式 */            for(String type : IMAGE_TYPE){                if(file.getOriginalFilename().endsWith(type)){                   isLegal = true;                   break;                }            }            /** 获取文件新路径 */            String fileName = getFileName(file.getOriginalFilename(),status);                                             /** 上传文件 */            File newFile = new File(STORE_URL + File.separator + fileName);            file.transferTo(newFile);            /** 判断上传的文件内容是否为图片 */            BufferedImage image = ImageIO.read(newFile);            if(image != null){                isLegal = true;            }else{                isLegal = false;            }                        /** 如果上传的文件非法则删除 */            if(!isLegal){                newFile.delete();            }            String serverPath = (LINK_URL + "/" + fileName).replace("\\", "/");            return isLegal ? serverPath  : null;        } catch (Exception e) {            e.printStackTrace();        }        return null;       }        /**     * 上传其他文件     * @param file      SpringMvc的上传接口     * @param path      上传的路径     * @param types     允许上传的文件类型     * @return     */    public static String uploadOthers(MultipartFile file,Integer status,String...types){        try {            boolean isLegal = false;            /** 1.进行图片格式 */            for(String type : types){                if(file.getOriginalFilename().endsWith(type)){                   isLegal = true;                   break;                }            }            /** 获取文件名字 */            String fileName = getFileName(file.getOriginalFilename(),status);                        /** 上传文件 */            File newFile = new File(STORE_URL + File.separator +fileName);                        file.transferTo(newFile);            /** 如果上传的文件非法则删除 */            if(!isLegal){                newFile.delete();            }            String serverPath = (LINK_URL + "/" + fileName).replace("\\", "/");            return isLegal ? serverPath  : null;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }            private static String getFileName(String file,Integer status){        Date nowDate = new Date();        /** yyyy/MM/dd */        String fileFolder ="";        switch (status) {case 0:fileFolder += "product" + File.separator;break;case 1:fileFolder += "bank" + File.separator;break;case 2:fileFolder += "face" + File.separator;break;case 3:fileFolder += "mechanism" + File.separator;break;case 4:fileFolder += "idCard" + File.separator;break;case 5:fileFolder += "house" + File.separator;break;case 6:fileFolder += "business" + File.separator;break;case 7:fileFolder += "myMonthPay" + File.separator;break;}        fileFolder += new DateTime(nowDate).toString("yyyy") + File.separator                 + new DateTime(nowDate).toString("MM") + File.separator  + new DateTime(nowDate).toString("dd");        File f = new File(STORE_URL + File.separator  + fileFolder);        if(!f.isDirectory() && !f.exists()){            f.mkdirs();        }        /** 生成新的文件名 */        String fileName =  new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS")                + RandomUtils.nextInt(100) +file.substring(file.lastIndexOf("."));        return fileFolder + File.separator  + fileName;    }    /**     *      * @param file  数据库中文件的路径        * 例如:http://localhost:8081/qiantufax-manage/upload     * 替换成 :D:/eclipse/qiantufax-manage/src/main/webapp/upload  在删除文件     * @return     */    public static boolean deleteFile(String file){    String oldFile = file.replace(LINK_URL,STORE_URL);    File deletefile = new File(oldFile);    if (deletefile.exists() && deletefile.isFile()) {   //判断给定文件名是否为正常的文件            if (deletefile.delete()) {                   return true;            } else {                return false;            }    }return false;    }        }
upload.properties
LINK_URL=http://localhost:8081/qiantufax-manage/uploadIMG_URL=D:/eclipse/qiantufax-manage/src/main/webapp/upload


原创粉丝点击