问题描述
我正在使用 Azure 文件存储将文件上传到 Azure 存储。在运行集成测试时,我下载文件并将其与不匹配的原始文件进行比较。下面分享一个示例代码
public boolean downloadFile(String fileName,File expectedFile) throws Exception {
File file = new File(fileName);
//opens a connection to azure storage through helper method.
this.azureConnection = openConnection(this.azureDir);
CloudFile cloudFile = this.azureConnection.getFileReference(fileName);
if (cloudFile.exists()) {
cloudFile.downloadToFile(file.getAbsolutePath());
} else {
throw new Exception(fileName+" doesn't exist in Azure");
}
return FileUtils.contentEquals(expectedFile,file);
}
上述方法总是返回false。我还尝试实现一种使用扫描仪逐行读取并写入文件的方法。他们仍然不匹配。实际上,我检查了两个文件并比较了间隔行和新行,一切都匹配。有人可以帮助解决这个问题。
//The below method is from org.apache.commons.io.FileUtils. I have simply copied and pasted it here for your reference.
public static boolean contentEquals(File file1,File file2) throws IOException {
boolean file1Exists = file1.exists();
if (file1Exists != file2.exists()) {
return false;
}
if (!file1Exists) {
// two not existing files are equal
return true;
}
if (file1.isDirectory() || file2.isDirectory()) {
// don't want to compare directory contents
throw new IOException("Can't compare directories,only files");
}
if (file1.length() != file2.length()) {
// lengths differ,cannot be equal
return false;
}
if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
// same file
return true;
}
InputStream input1 = null;
InputStream input2 = null;
try {
input1 = new FileInputStream(file1);
input2 = new FileInputStream(file2);
return IoUtils.contentEquals(input1,input2);
} finally {
IoUtils.closeQuietly(input1);
IoUtils.closeQuietly(input2);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)