日期:2014-05-20 浏览次数:20812 次
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FileDigestThread extends Thread{
private File input;
public FileDigestThread(File input){
this.input = input;
}
public void run(){
try {
FileInputStream fs = new FileInputStream(input);
MessageDigest mg = MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(fs,mg);
/* while
((b=din.read())!=-1);*/
while(true){
if(din.read()==-1);
break;
}
din.close();
// byte[] digest = din.getMessageDigest().digest();
byte[] digest = mg.digest();
StringBuffer result = new StringBuffer(input.toString());
result.append(": ");
for(int i =0;i<digest.length;i++)
{
result.append(digest[i]+ "");
}
System.out.println("result: "+result);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 0;i<args.length;i++)
{
File f = new File(args[i]);
Thread thread = new FileDigestThread(f);
thread.start();
}
}
}