删除文件的工具类:↓DeleteFileUtil.java
package com.ssh02.util; import java.io.File; public class DeleteFileUtil { public static boolean deleteFilePath(String path,String[] paths){ File f= null; if(path != null){ f= new File(path); if(f.exists()){ f.delete(); return true; } } if(paths.length >= 1){ for(int i=0; i<paths.length; i++){ f = new File(paths[i]); if(f.exists()){ f.delete(); } } return true; } return false; } }
随机文件名工具:IPTimeRandom.java (这工具不陌生)
package com.ssh02.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class IPTimeRandom { private SimpleDateFormat sdf = null; private String ip = null; public IPTimeRandom(){ } public IPTimeRandom(String ip){ this.ip = ip; } public String getIPTimeRandom(){ StringBuffer buf = new StringBuffer(); if(this.ip != null){ String s[] = this.ip.split("\\:"); for(int i=0; i<s.length; i++) { buf.append(this.addZero(s[i], 4)); } } buf.append(this.getDate()); Random r = new Random(); for(int i=0; i<3; i++) { buf.append(r.nextInt(10)); } return buf.toString(); } public String addZero(String str,int len){ StringBuffer s = new StringBuffer(); s.append(str); while(s.length() < len){ s.insert(0, "0"); } return s.toString(); } public String getDate(){ this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return this.sdf.format(new Date()); } }
Md5加密处理工具:↓Md5Util.java
package com.ssh02.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Md5Util { public static String md5s(String newstr){ String str = null; MessageDigest md = null; try { md = MessageDigest.getInstance("MD5"); md.update(newstr.getBytes()); byte[] b = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for(int j = 0; j<b.length; j++){ i = b[j]; if(i<0)i+=256; if(i<16)buf.append("0"); buf.append(Integer.toHexString(i)); } str = buf.toString().toUpperCase(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return str; } public static void main(String[] args){ System.out.println(Md5Util.md5s("123")); } }
文件上传工具:↓Upload.java
package com.ssh02.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Upload { public static void is(File file, String uploadPath, String newFileName) throws Exception{ FileInputStream is = new FileInputStream(file); FileOutputStream os = new FileOutputStream(uploadPath + "/" + newFileName); byte[] buff = new byte[1024]; int newint = 0; while((newint = is.read(buff))> 0){ os.write(buff, 0, newint); } is.close(); os.close(); } }
连接mysql工具:↓Upload.java
package com.itjiandan.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBManager { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection("jdbc:mysql://localhost/itjiandan?useUnicode=true&characterEncoding=utf-8", "root","root"); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static void close(Connection conn,Statement pre,ResultSet set){ try { if(conn != null)conn.close(); if(pre != null)pre.close(); if(set != null)set.close(); } catch (SQLException e) { e.printStackTrace(); } } }
利用上传插件:commons-fileupload-1.3.jar commons-io-2.4.jar
package com.itjiandan.util; import java.io.File; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.tomcat.util.http.fileupload.FileItem; import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory; import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload; public class FileLoadUtil { public static Map<String,String> fileLoad(HttpServletRequest req, String path){ DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); Map<String,String> myFMap = new HashMap<String, String>(); try { List<FileItem> items = upload.parseRequest(req); Iterator<FileItem> iter = items.iterator(); while(iter.hasNext()){ FileItem item = iter.next(); if(item.isFormField()){ System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("utf-8")); myFMap.put(item.getFieldName(), item.getString("utf-8")); }else{ myFMap.put("imagename", item.getName().split("\\.")[1]); //System.out.println(item.getName().split("\\.")[1]); File newfile = new File(path + item.getName().split("\\.")[1]); item.write(newfile); } } } catch (Exception e) { e.printStackTrace(); } return myFMap; } }