笔记-获取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); } }