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