使用 Spring core 的 DigestUtils
计算文件的 MD5。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream;
import org.springframework.util.DigestUtils;
public class CommonUtils {
public static String fileMd5(String filePath) { InputStream in = null;
try { in = new FileInputStream(filePath); return DigestUtils.md5DigestAsHex(in); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } }
return ""; } public static void main(String[] args) { System.out.println(fileMd5("/Users/Biao/Desktop/x.rar")); } }
|