工具类:org.springframework.util.DigestUtils
作用:计算字节数组、输入流的 md5 摘要
所在模块:spring-core
方法 | 描述 |
String md5DigestAsHex(byte[] bytes) |
返回字节数组的 md5 摘要(计算字符串) |
String md5DigestAsHex(InputStream inputStream) |
返回输入流的 md5 摘要(计算文件) |
demo 代码:
import org.springframework.util.DigestUtils; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class DigestUtilsDemo { public static void main(String[] args) throws IOException { byte[] bytes = "123456".getBytes(StandardCharsets.UTF_8); String md5 = DigestUtils.md5DigestAsHex(bytes); System.out.println(md5); try (InputStream is = new FileInputStream("C:\\Users\\Nihaorz\\Desktop\\web.xml")) { md5 = DigestUtils.md5DigestAsHex(is); System.out.println(md5); } } }
console 打印:
e10adc3949ba59abbe56e057f20f883e 145ba663d9cfbbb8389f61afe42d56d8
标签:java,String,Spring,import,工具,DigestUtils,md5DigestAsHex,md5 From: https://www.cnblogs.com/nihaorz/p/17967298