获取服务器上文件使用url的方式的方式,如“http://localhost:9988/aa/download/1.exe”
//path是相对路径
private String getFileTime(HttpServletRequest httpReq, String path) throws Exception {
String reqUrl = httpReq.getRequestURL().toString();
String reqUri = httpReq.getRequestURI().toString();
String fileUrl = reqUrl.replace(reqUri, "");
fileUrl = fileUrl + "/" + path;
File exeFile = new File(new URL(fileUrl).openConnection().getURL().getFile());
long time = exeFile.lastModified();
Date data = new Date();
data.setTime(time);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.format(data);
}根据情况url中添加httpReq.getContextPath()来定位 (fileUrl + contextPath + "/" + path)。
本文介绍了一种通过HTTP请求获取服务器上指定文件最后修改时间的方法。该方法构造了一个URL用于定位服务器上的文件,并通过解析URL获取文件的实际路径。之后,利用Java的File类获取到文件的最后修改时间并将其格式化为“yyyy-MM-dd”的日期格式。

被折叠的 条评论
为什么被折叠?



