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;
/** File Upload Tool class */public class Fileuploadutil {private static String Store_url = "";
private static String Link_url = "";
/** allow upload of format */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 block E.printstacktrace (); }}/** * Upload image method (only upload images) * @param file Springmvc Upload interface *@param status 0 Upload product 1 upload bank card 2 upload Avatar 3 Agency 4 ID 5 Real Estate 6 pieces * @return upload successful return path upload failed return null */public static St
Ring Uploadimage (multipartfile file,integer status) {try {Boolean islegal = false;
/** 1. Make picture format */for (String Type:image_type) {if (File.getoriginalfilename (). EndsWith (type)) {
Islegal = true;
Break
}}/** gets the new path to the file */String FileName = GetFileName (File.getoriginalfilename (), status); /** Upload Files */file NewFile = new file (Store_url + File.separator +
FileName);
File.transferto (NewFile);
/** determine whether the uploaded file content is a picture */bufferedimage image = Imageio.read (newFile);
if (image = null) {Islegal = true;
}else{Islegal = false;
}/** If the uploaded file is illegal delete */ if (!islegal) {newfile.delete ();
} String Serverpath = (Link_url + "/" + fileName). replace ("\ \", "/"); Return Islegal?
Serverpath:null;
} catch (Exception e) {e.printstacktrace ();
} return null; }/** * Upload other files * @param file Springmvc Upload interface * @param path uploaded paths * @param types
Allowed file types to be uploaded * @return */public static String uploadothers (Multipartfile file,integer status,string...types) {
try {Boolean islegal = false;
/** 1. Make picture format */for (String type:types) {if (File.getoriginalfilename (). EndsWith (type)) {
Islegal = true;
Break
}}/** gets the file name */String FileName = GetFileName (File.getoriginalfilename (), status); /** Uploading Files */File NewFile = new file (STORE_url + file.separator +filename);
File.transferto (NewFile);
/** If the uploaded file is illegal, delete the */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). t
Ostring ("MM") + File.separator + new DateTime (nowdate). toString ("DD");
File F = new file (Store_url + file.separator + filefolder);
if (!f.isdirectory () &&!f.exists ()) {f.mkdirs (); /** generates a new file name */String filename = new DateTime (nowdate). toString ("yyyymmddhhmmssssss") +
Randomutils.nextint (+file.substring) (File.lastindexof ("."));
Return Filefolder + File.separator + fileName; }/** * * @param files in the file database * Example: Http://localhost:8081/qiantufax-manage/upload * replaced with:D:/eclipse/qiantufax-manage/src/main/ Webapp/upload Deleting files &nbsP * @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 ()) { //determine if a given file name is a normal file &NBS P
if (Deletefile.delete ()) { -return true;
} else { return false;
} } return false;
}}
Upload.properties
Link_url=http://localhost:8081/qiantufax-manage/upload
Img_url=d:/eclipse/qiantufax-manage/src/main/webapp /upload