文件的MD5摘要

一个文件的md5摘要分两种情况:文件为文本文件,文件为二进制文件。

1. 文件为文本文件时:

package com.ant.jdk8.encrypt;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author wangll
 * @date 2018/7/17
 */
public class FileMD5Demo {
    public static void main(String[] args) throws IOException {
        String md5Str = fileMD5("E:"+File.separator+"易林文.txt");//E盘下有文本文件:易林文.txt
        System.out.println(md5Str);
    }

    public static String fileMD5(String txtFilePath) throws IOException {
        String str = FileUtils.readFileToString(new File(txtFilePath),"utf-8");
        String md5Str = DigestUtils.md5Hex(str);
        return md5Str;
    }
}

2. 文件为二进制文件时:

猜你喜欢

转载自www.cnblogs.com/i-hard-working/p/9326638.html
今日推荐