zoukankan      html  css  js  c++  java
  • java对文件hash

    package cn.iautos.mall.utils;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.security.MessageDigest;
    
    import org.springframework.util.StringUtils;
    
    public class CheckHashUtils {
        
        
        public static String getHashByFile(String fileName) throws Exception{
            return getMD5Checksum(fileName,null);
        }
        public static String getHashByStream(InputStream fis) throws Exception{
            return getMD5Checksum(null,fis);
        }
        
        
        private static byte[] createChecksum(String filename,InputStream fis) throws Exception {
            if(!StringUtils.isEmpty(filename)){
                fis =  new FileInputStream(filename);
            }
            byte[] buffer = new byte[1024];
            MessageDigest complete = MessageDigest.getInstance("MD5");
            int numRead;
    
            do {
                numRead = fis.read(buffer);
                if (numRead > 0) {
                    complete.update(buffer, 0, numRead);
                }
            } while (numRead != -1);
    
            fis.close();
            return complete.digest();
        }
     
        private static String getMD5Checksum(String filename,InputStream fis) throws Exception {
            byte[] b = createChecksum(filename,fis);
            String result = "";
    
            for (int i=0; i < b.length; i++) {
                result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
            }
            return result;
        }
    
    }
  • 相关阅读:
    POJ_1485_dp
    POJ_1376_bfs
    [noi1994]海盗
    [noi1755]Trie
    [luogu3733]八纵八横
    [noi1774]array
    [noi1773]function
    [noi1754]SA
    [noi1779]D
    [bzoj4873]寿司餐厅
  • 原文地址:https://www.cnblogs.com/xuzhenmin/p/3643058.html
Copyright © 2011-2022 走看看