java md5 check tool

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang.StringUtils;

public class FileHashCompleteCheck {
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args){
			System.out.println(" Please Input CPU Count:");
			BufferedReader   br   =   new  BufferedReader(new  InputStreamReader(System.in)); 
            String str;
			try {
				str = br.readLine();
				int cpuCount = Integer.parseInt(str);
				System.out.println(" Please Input To Check File Path:");
				br   =   new  BufferedReader(new  InputStreamReader(System.in)); 
				str = br.readLine();
				String filePath = str;
		    	String code = new FileHashCompleteCheck().getFileCheckCode(new File(filePath),cpuCount);
		    	System.out.println(" code = " + code);
			}catch(NumberFormatException e){
				System.out.println(" CPU Count Must Number!");
			}catch(FileNotFoundException e){
				System.out.println(" File Not Found!");
			}catch (IOException e) {
				System.out.println(" System Error!");
			} 
	}

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };

	private MessageDigest msgDigest = null;
	private final int blockSize = 1024;

	public FileHashCompleteCheck() {
		try {
			msgDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException nsae) {
			System.out.println(" error: "+nsae.getMessage());
		}
	}

	/**
	 * 比对文件校验码
	 * @param file 要比对的文件
	 * @param checkCode 要比对的文件的校验码
	 * @return 比对结果
	 * @throws IOException
	 */
	public boolean checkFileComplete(File file,String checkCode,int checkRnage)throws IOException {
		return StringUtils.equals(getFileCheckCode(file,checkRnage), checkCode);
	}

	/**
	 * 生成文件校验码
	 * 
	 * @param file 要校验的文件
	 * @param fileCheckRnage 控制校验速度,基本CPU个数
	 * @return 校验码
	 * @throws IOException
	 */
	public String getFileCheckCode(File file,int fileCheckRnage) throws IOException {
		String fileCheckCode = null;
		FileInputStream instream = null;
		if (!file.isDirectory()) {
			try {
				instream = new FileInputStream(file);
				msgDigest.reset();
				int read;
				byte[] buf = new byte[blockSize];
				for (int i = 0; (read = instream.read(buf)) != -1; i++) {
					msgDigest.update(buf, 0, read);
					if (i % (1024 * 1024 * fileCheckRnage) == 0) {
						try {
							Thread.sleep(10);
						} catch (Exception e) {
							System.out.println(" md5 check fail = "+e.getMessage());
							continue;
						}
					}
				}
			} finally {
				if (instream != null) {
					instream.close();
				}
			}
			fileCheckCode = new String(byteArrayToHexString(msgDigest.digest()));
		}
		return fileCheckCode;
	}

	/**
	 * 生成十六进制码
	 * 
	 * @param b
	 * @return String
	 */
	private String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();

		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	/**
	 * 将byte转换成相应的十六进制符
	 * 
	 * @param b
	 * @return String
	 */
	private String byteToHexString(byte b) {
		int n = b;

		if (n < 0) {
			n = 256 + n;
		}
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	/**
	 * 返回要使用的校验方式
	 * 
	 * @return MessageDigest
	 */
	public MessageDigest getMessageDigest() {
		return msgDigest;
	}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值