1.å¦ä½è®¡ç®InputstreamçMD5
å¦ä½è®¡ç®InputstreamçMD5
é¦å ï¼æç®åçæ¹å¼å°±æ¯æä½ ç两è¡ä»£ç ç»åèµ·æ¥ï¼å ä¿åæ件ï¼å读åæ件æµè®¡ç®MD5ï¼
public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream,头脑王者 源码 File file) throws IOException {
FileUtils.copyInputStreamToFile(inputStream, file);
return DigestUtils.md5Hex(new FileInputStream(file));
}
å½ç¶è¿æ ·åè¦å¯¹åä¸ä¸ªæµè¯»å两次ï¼æ¾å¾ä¸å¤ä½ç¢³ç¯ä¿ã
æ¤æ¶å¯ä»¥çä¸DigestUtilsæºç ï¼è¿½å ¶æ ¹æº¯å ¶æºå¯ä»¥çå°ï¼
public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
while (read > -1) {
digest.update(buffer, 0, read);
read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
}
return digest;
}
ä¹ä¸æ¯å¤é«çº§çææ¯ï¼å°±æ¯ææ´ä¸ªInputStreamææé¿åº¦çåèæ°ç»é个MD5ã
åççFileUtils.copyInputStreamToFileæºç çè¿½æ ¹æº¯æºå®ç°ï¼
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
long count;
int n;
for(count = 0L; -1 != (n = input.read(buffer)); count += (long)n) {
output.write(buffer, 0, n);
}
return count;
}
åæ ·ä¹æ¯è®²InputStreamææçåèæ°ç»ï¼é个åå°ç®æ æ件ä¸ã
é£ä¹ï¼ä¸¤è ç»åèµ·æ¥ä»£ç ä¹å°±å¥½åäºï¼
public static String copyInputStreamToFileAndGetMd5Hex(InputStream inputStream, File file) throws IOException {
MessageDigest digest = DigestUtils.getMd5Digest();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[];
int read = inputStream.read(buffer);
while (read > -1) {
// 计ç®MD5,顺便åå°æ件
digest.update(buffer, 0, read);
outputStream.write(buffer, 0, read);
read = inputStream.read(buffer);
}
} finally {
IOUtils.closeQuietly(outputStream);
}
return Hex.encodeHexString(digest.digest());
}
2024-12-23 06:46
2024-12-23 06:44
2024-12-23 05:47
2024-12-23 05:09
2024-12-23 04:53
2024-12-23 04:37