zoukankan      html  css  js  c++  java
  • JAVA生成文件的md5校验值

    这里使用了lombok打印日志,也可以不用

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import lombok.extern.slf4j.Slf4j;
    /**
     * author yvioo
     */
    @Slf4j
    public class MD5Util {
         /**
          * 生成文件的md5校验值
          * 
          * @param path 文件路径
          * @return 文件md5校验值
          * @throws IOException
         * @throws NoSuchAlgorithmException 
          */
        public static String getFileMD5String(String path){
            InputStream fis = null;
            MessageDigest messagedigest = null;
            try {
                messagedigest = MessageDigest.getInstance("md5");
                File file = new File(path);
                if(!file.exists() || !file.isFile()) {
                    log.error("不存在或不是一个文件");
                    return "";
                }
                
                fis = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int numRead = 0;
                while ((numRead = fis.read(buffer)) > 0) {
                   messagedigest.update(buffer, 0, numRead);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }finally {
                try {
                    if(fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return bufferToHex(messagedigest.digest());
        }
        private static String bufferToHex(byte bytes[]) {
            return bufferToHex(bytes, 0, bytes.length);
        }
        private static String bufferToHex(byte bytes[], int m, int n) {
            StringBuffer stringbuffer = new StringBuffer(2 * n);
            int k = m + n;
            for (int l = m; l < k; l++) {
             appendHexPair(bytes[l], stringbuffer);
            }
            return stringbuffer.toString();
        }
        /**
         * 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
         */
        protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
         char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>> 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同 
         char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换 
         stringbuffer.append(c0);
         stringbuffer.append(c1);
        }
    }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    基于vue-cli快速构建
    '无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称' 或 'vue不是内部或外部命令' 的解决方法
    js / ajax 成功提交后怎么跳转到另外一个页面?
    SpringMVC 拦截器不拦截静态资源的三种处理方式方法
    各种JSON的maven引用
    java版微信公众号支付(H5调微信内置API)
    阿里云MongoDB存储数据
    阿里RocketMq(TCP模式)
    Nginx 简单安装
    Redis-主从复制
  • 原文地址:https://www.cnblogs.com/pxblog/p/14665953.html
Copyright © 2011-2022 走看看