由于公司需要校验上传的文件是否有修改, 本来是用文件的最后一次修改时间确定的, 不过怎么获取url内文件的方式没有用心去找, 客户端那边也不使用这种了, 改为使用读取文件的MD5, 网上搜罗了一下, 最后总结为以下代码, 正常使用, 不足的地方请指点。
path是url, http之类的。 导入的包都是很基础的包, 一个ctrl shift + o 完全没问题。
import org.apache.commons.codec.digest.DigestUtils;
public String getMD5(String path) throws IOException{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream in = conn.getInputStream();
String md5Hex = DigestUtils.md5Hex(in);
return md5Hex;
}
---------------------
低版本的会报错:DigestUtils.md5Hex(in),没有InputStream传参类型的api
修正代码:
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public String getMD5(String path) throws IOException {
URL url = new URL("url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
String md5Hex = DigestUtils.md5Hex(in);
DigestUtils.md5Hex(md5Hex);
return md5Hex;
}













