自己整理的,发在这里做个备份。 /**//* * md5.java * * Created on 2007年5月14日, 下午12:12 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package tools;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** *//** * * @author zhappy */public class md5 ...{ /** *//** Creates a new instance of md5 */ public md5() ...{ } /** *//** * 得到指定文件MD5信息 */ public static String fileMD5(File file) ...{ FileInputStream fis = null; MessageDigest md = null; byte[] buf = new byte[2048]; try ...{ md = MessageDigest.getInstance("MD5"); fis = new FileInputStream(file); int num = fis.read(buf); while (num != (-1)) ...{ md.update(buf,0,num); num = fis.read(buf); } } catch (FileNotFoundException ex) ...{ ex.printStackTrace(); } catch (IOException e) ...{ e.printStackTrace(); } catch (NoSuchAlgorithmException ex) ...{ ex.printStackTrace(); } return bytes2Hex(md.digest()); } /** *//** * 得到strSrc的文件MD5信息 */ public static String strMD5(String strSrc) ...{ MessageDigest md = null; byte[] bt=strSrc.getBytes(); String strDes = null; try ...{ md = MessageDigest.getInstance("MD5"); md.update(bt); strDes = bytes2Hex(md.digest()); //to HexString } catch (NoSuchAlgorithmException e) ...{ e.printStackTrace(); } return strDes; } /** *//** * 二行制转hex字符串 */ public static String bytes2Hex(byte[] bts) ...{ String des=""; String tmp=null; for (int i=0;i<bts.length;i++) ...{ tmp=(Integer.toHexString(bts[i] & 0xFF)); if (tmp.length()==1) ...{ des+="0"; } des+=tmp; } return des; } }