笔记-获取APK的MD5值
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; /** * MD5加密工具类 * */ public final class Md5Utils { private Md5Utils() { throw new UnsupportedOperationException("cannot be instantiated"); } /** * 获取文件的MD5值 * * @param file * @return */ public static String getFileMD5(File file) throws Exception { if (file == null || !file.exists()) { return ""; } FileInputStream fis = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); fis = new FileInputStream(file); byte[] buffer = new byte[8192]; int len; while ((len = fis.read(buffer)) != -1) { digest.update(buffer, 0, len); } return bytes2Hex(digest.digest()); } catch (Exception e) { throw new Exception(e); } finally { if (fis != null) { try { fis.close(); } catch (IOException ignored) { } } } } /** * 一个byte转为2个hex字符 * * @param src byte数组 * @return 16进制大写字符串 */ private static String bytes2Hex(byte[] src) { char[] res = new char[src.length << 1]; final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; for (int i = 0, j = 0; i < src.length; i++) { res[j++] = hexDigits[src[i] >>> 4 & 0x0F]; res[j++] = hexDigits[src[i] & 0x0F]; } return new String(res); } public static long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("文件不存在"); return -1; } return file.length(); } public static void main(String[] args) throws Exception { File file = new File("F:\\iVS_Ftp\\gpsUpgradePkg\\1.7.7_565_202207050247_ZX.apk"); String md5 = Md5Utils.getFileMD5(file); System.out.println(md5); long size = getFileSize("F:\\iVS_Ftp\\gpsUpgradePkg\\1.7.7_565_202207050247_ZX.apk"); System.out.println("文件大小为: " + size); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)