java实现对文件内容md5加密
程序员文章站
2024-03-19 10:59:46
...
talk is cheap, show you the code
import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class TestMD5 {
public static void main(String[] args) {
String filePath = "/data/test/xxxx.bin";
File file = new File(filePath);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
byte[] secretBytes = MessageDigest.getInstance("md5").digest(filecontent);
System.out.println(DatatypeConverter.printHexBinary(secretBytes)); //将byte数组转为十六进制字符串);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
上一篇: 拓扑排序 用dfs或者bfs