问题描述
我需要计算csv文件的校验和。每次文件中的数据更改时,校验和都将更改。在这方面,我发现在互联网上没有什么有用的。
I need to calculate the checksum of a csv file. The checksum will change every time the data in the file is changed. I found nothing useful over the internet in this regard.
推荐答案
首先,这个问题不是特定于JSP。 JSP只是一个HTML代码生成器。在JSP文件中编写Java代码而不是普通的Java类不会使它成为JSP问题。如果你专注于使用Java关键字解决未来的Java问题,而不是使用JSP关键字,你会更多地帮助自己。
First of all, this problem is not specific to JSP. JSP is just a HTML code generator. Writing Java code in a JSP file instead of a normal Java class doesn't make it a JSP problem. You would help yourself more if you concentrate on solving future Java problems using the "Java" keyword, not using the "JSP" keyword.
FileInputStream input = new FileInputStream("/path/to/file.csv");
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[10240];
for (int length = 0; (length = input.read(buffer)) > 0;) {
md5.update(buffer, 0, length);
}
byte[] hash = digest.digest();
您可能想要将散列转换为十六进制。
You may want to convert the hash to hex afterwards.
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xff) < 0x10) {
hex.append("0");
}
hex.append(Integer.toHexString(b & 0xff));
}
String hexString = hex.toString();
这篇关于如何在JSP中为CSV文件生成md5校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!