@Test public void fileHash() { File file = new File(TestUtil.path + "/text/English.txt"); System.out.println(FileHelper.fileHash(file, "MD5")); System.out.println(FileHelper.fileHash(file, "SHA-1")); System.out.println(FileHelper.fileHash(file, "SHA-256")); }
public static String fileHash(File file, String HashTyle) { try (InputStream fis = new FileInputStream(file)) { MessageDigest md = MessageDigest.getInstance(HashTyle); byte[] buffer = new byte[1024]; int length = -1; while ((length = fis.read(buffer, 0, 1024)) != -1) { md.update(buffer, 0, length); } byte[] md5Bytes = md.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { logger.error("get filehash error" + e.getMessage()); e.printStackTrace(); return ""; } }
FileHelper { public static String fileHash(File file, String HashTyle) { try (InputStream fis = new FileInputStream(file)) { MessageDigest md = MessageDigest.getInstance(HashTyle); byte[] buffer = new byte[1024]; int length = -1; while ((length = fis.read(buffer, 0, 1024)) != -1) { md.update(buffer, 0, length); } byte[] md5Bytes = md.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { logger.error("get filehash error" + e.getMessage()); e.printStackTrace(); return ""; } } }
FileHelper { public static String fileHash(File file, String HashTyle) { try (InputStream fis = new FileInputStream(file)) { MessageDigest md = MessageDigest.getInstance(HashTyle); byte[] buffer = new byte[1024]; int length = -1; while ((length = fis.read(buffer, 0, 1024)) != -1) { md.update(buffer, 0, length); } byte[] md5Bytes = md.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { logger.error("get filehash error" + e.getMessage()); e.printStackTrace(); return ""; } }  }
FileHelper { public static String fileHash(File file, String HashTyle) { try (InputStream fis = new FileInputStream(file)) { MessageDigest md = MessageDigest.getInstance(HashTyle); byte[] buffer = new byte[1024]; int length = -1; while ((length = fis.read(buffer, 0, 1024)) != -1) { md.update(buffer, 0, length); } byte[] md5Bytes = md.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { logger.error("get filehash error" + e.getMessage()); e.printStackTrace(); return ""; } }  static void handlerWithLine(File file, String encoding, ObjectHandler<String> handler); static void processWithLine(File file, String encoding, Collection<E> result, ObjectProcess<String, E> process); static int lineCounts(File file); static List<String> readLines(File file); static List<String> readLines(File file, String encoding); static String readContents(File file); final static boolean createFiles(File file); final static boolean createFiles(File file, boolean isReNew); static boolean write(File file, String str); static boolean write(File file, String str, String encoding); static boolean addWrite(File file, String str); static boolean addWrite(File file, String str, String encoding); static boolean copy(File resource, File target); static boolean copyFile(File file, File targetFile); static boolean cleanFile(File file); static boolean delete(File file); static List<File> listFile(File path); static List<File> listFileSuffix(File dirPath, String postfixs); static List<File> listFileName(File dirPath, String fileName); static List<File> listFileNameIgnoreCase(File dirPath, String fileName); static List<File> listFileFilter(File path, ObjectFilter filter); static List<File> listFileNameReg(File dirPath, Pattern pattern); static List<File> listFileWithHandler(File path, ObjectProcess<File, File> handler); static boolean createFile(String path); static String suffix(File file); static String fileHash(File file, String HashTyle); static String simpleEncoding(File file); static boolean zipDeCompress(File file, String zipFile); static boolean zipUnCompress(File zipFile, String path); }
FileHelper { public static String fileHash(File file, String HashTyle) { try (InputStream fis = new FileInputStream(file)) { MessageDigest md = MessageDigest.getInstance(HashTyle); byte[] buffer = new byte[1024]; int length = -1; while ((length = fis.read(buffer, 0, 1024)) != -1) { md.update(buffer, 0, length); } byte[] md5Bytes = md.digest(); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } catch (Exception e) { logger.error("get filehash error" + e.getMessage()); e.printStackTrace(); return ""; } }  static void handlerWithLine(File file, String encoding, ObjectHandler<String> handler); static void processWithLine(File file, String encoding, Collection<E> result, ObjectProcess<String, E> process); static int lineCounts(File file); static List<String> readLines(File file); static List<String> readLines(File file, String encoding); static String readContents(File file); final static boolean createFiles(File file); final static boolean createFiles(File file, boolean isReNew); static boolean write(File file, String str); static boolean write(File file, String str, String encoding); static boolean addWrite(File file, String str); static boolean addWrite(File file, String str, String encoding); static boolean copy(File resource, File target); static boolean copyFile(File file, File targetFile); static boolean cleanFile(File file); static boolean delete(File file); static List<File> listFile(File path); static List<File> listFileSuffix(File dirPath, String postfixs); static List<File> listFileName(File dirPath, String fileName); static List<File> listFileNameIgnoreCase(File dirPath, String fileName); static List<File> listFileFilter(File path, ObjectFilter filter); static List<File> listFileNameReg(File dirPath, Pattern pattern); static List<File> listFileWithHandler(File path, ObjectProcess<File, File> handler); static boolean createFile(String path); static String suffix(File file); static String fileHash(File file, String HashTyle); static String simpleEncoding(File file); static boolean zipDeCompress(File file, String zipFile); static boolean zipUnCompress(File zipFile, String path);  }