java文件md5判重
时间: 2023-11-17 20:04:20 浏览: 41
可以使用Java的DigestUtils类中的md5Hex方法来获取文件的md5值,然后将其与其他文件的md5值进行比较,以判断是否为相同文件。以下是一个示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.codec.digest.DigestUtils;
public class FileMd5Checker {
public static void main(String[] args) throws IOException {
File file1 = new File("path/to/file1");
File file2 = new File("path/to/file2");
String md5_1 = getFileMd5(file1);
String md5_2 = getFileMd5(file2);
if (md5_1.equals(md5_2)) {
System.out.println("The two files are the same.");
} else {
System.out.println("The two files are different.");
}
}
private static String getFileMd5(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
String md5 = DigestUtils.md5Hex(fis);
fis.close();
return md5;
}
}
```
在上面的示例代码中,getFileMd5方法接收一个File对象作为参数,返回该文件的md5值。在main方法中,我们可以使用getFileMd5方法获取两个文件的md5值,并将它们进行比较,以判断它们是否相同。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)