當前位置:首頁 » 編程語言 » 文件md5java

文件md5java

發布時間: 2025-10-10 02:29:08

java 獲取1G文件md5 要多久

我使用了部分取值的方式來提高MD5的計算速度,這樣的時候,時間主要耗費在了IO中。如果是100K(換成500K也並沒有提高執行的速度)取一個字元計算大約10秒以內。但是如果全部讀取可能要60秒或者更多。對於大文件建議使用一些文件相關信息和部分文件內容做MD5.比如用文件長度和一定間隔取一些位元組。

Ⅱ Java,如何獲取文件的MD5值

package cdm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.*;
import org.apache.commons.io.IOUtils;
public class testMD5 {

public static String getMd5ByFile(File file) throws FileNotFoundException {
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}

public static void main(String[] args) throws IOException {

String path="E:\文件.zip";

String v = getMd5ByFile(new File(path));
System.out.println("MD5:"+v.toUpperCase());

FileInputStream fis= new FileInputStream(path);
String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
IOUtils.closeQuietly(fis);
System.out.println("MD5:"+md5);

//System.out.println("MD5:"+DigestUtils.md5Hex("WANGQIUYUN"));
}
}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:645
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:936
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:632
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:821
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:731
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1066
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:299
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:160
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:852
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:763