使用 commons 或 Javascript 比较两个文件 [英] Comparing two files using commons or Javascript

查看:21
本文介绍了使用 commons 或 Javascript 比较两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查上传的 4 个文件中的任何一个是否相同,此检查可能在 JSP 或 Java-Servlet 端.

I need to check if any of the 4 uploaded files are same, this check might be on the JSP or Java-Servlet side.

我一直在使用

var FileName1 = document.getElementById('fileChooser1').value;
var FileName2 = document.getElementById('fileChooser2').value;
if(FileName1 == FileName2)
{
 alert("same files cannot be uploaded");
}

但是,问题在于这仅处理文件的名称,如果上传内容相同但名称不同的文件,则会失败.

But, the problem is that this only deals with the name of the file and this fails if files with same content but different names are uploaded.

所以,在 apache commons 搜索中,我发现有一个 默认比较器 但我不知道如何使用它,或者有没有其他更好/更简单的方法来检查相同的文件.

So, on apache commons search I found that there is a Default Comparator but I have no idea of how I can use this or is there any other better/simpler way to check for same files.

  1. 我如何使用 默认比较器以及它在什么基础上进行比较?
  2. 在 java 或 javascript 中有没有更好/更简单的解决方案来解决这个问题?
  1. How can I use the Default Comparator and on what basis does it compare?
  2. Is there any better/simpler solution to this problem in java or javascript?

推荐答案

您可以使用 FileUtils.contentEquals 方法来比较 2 个文件的内容.

You can use FileUtils.contentEquals method to compare content of 2 files.

示例

System.out.println(FileUtils.contentEquals(file1, file2));

这篇关于使用 commons 或 Javascript 比较两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆