<a href="/bxx/download?path=xxxxxxx&name=xxxxxxx">xxxxx</a>
@At
@Ok("void")
public void download(@Param("..") Record record,HttpServletResponse res) {
String strUserAgent = Mvcs.getReq().getHeader("user-agent");
String fileName = record.getString("name");
String path = record.getString("path");
String tomcat = Util.getTomcatPath().replaceAll("\\\\", "/");
try {
File file = new File(tomcat + path);
if(!file.exists()){
System.out.println("file not find ");
return ;
}
fileName = EncodeUtil.encodeFileName(fileName, strUserAgent);
Mvcs.getResp().setContentType("application/octet-stream; charset=UTF-8");
Mvcs.getResp().addHeader("Content-Disposition", "attachment; filename=" + fileName);
Streams.writeAndClose(Mvcs.getResp().getOutputStream(), Streams.fileIn(file));
Mvcs.getResp().flushBuffer();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
10 回复
改过之后的,但是找不到文件 还是不知道怎么友好提示,或者跳转404页面
@At
@Ok("raw")
public File download(@Param("..") Record record, HttpServletResponse resp) {
try {
String fileName = record.getString("name");
String path = record.getString("path");
resp.setHeader("Content-Disposition",
"attachment; filename=\"" + URLEncoder.encode(fileName, Encoding.UTF8) + "\"");
String tomcat = Util.getTomcatPath().replaceAll("\\\\", "/");
File file = new File(tomcat + path);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
没有返回404
点击找不到后,页面显示:
网址为 http://localhost:8888/bxacorp/down/download?path=upload/11/1101/110102/corp_safe_target/20180824/1535076019833_立新煤矿采煤工作面安全风险辨识清单清单详情aa.xlsx&name=立新煤矿采煤工作面安全风险辨识清单清单详情aa.xlsx 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。
还有分分钟被下载任何文件这个问题:
还请您不吝赐教怎么弄
判断一下文件是否存在,不存在就return HttpStatusView.HTTP_404
判断参数里面是否包含相对路径"..",而且要判断文件的绝对路径是否在允许的目录下
一般就传个文件名,或者id,哪有直接传路径的
文件找不到后,页面显示的还是上面那个错误消息。web.xml 配置了404 error_code ,为啥不跳转我的404.jsp页面呢...........哎,水平太菜了.......
@At
@Ok("raw")
public Object download(@Param("..") Record record, HttpServletResponse resp) {
try {
String fileName = record.getString("name");
String path = record.getString("path");
resp.setHeader("Content-Disposition",
"attachment; filename=\"" + URLEncoder.encode(fileName, Encoding.UTF8) + "\"");
String tomcat = Util.getTomcatPath().replaceAll("\\\\", "/");
File file = new File(tomcat + path);
if(file.exists()){
return file;
}else{
System.out.println("file not find");
return HttpStatusView.HTTP_404;
}
} catch (Exception e) {
e.printStackTrace();
return HttpStatusView.HTTP_404;
}
}
页面报错:
:8888/bxacorp/down/download?path=upload/11/1101/110102/corp_safe_target/201…A8%E8%AF%86%E6%B8%85%E5%8D%95%E6%B8%85%E5%8D%95%E8%AF%A6%E6%83%85aa.xlsx:1 GET http://localhost:8888/bxacorp/down/download?path=upload/11/1101/110102/corp…E%A8%E8%AF%86%E6%B8%85%E5%8D%95%E6%B8%85%E5%8D%95%E8%AF%A6%E6%83%85aa.xlsx net::ERR_INVALID_RESPONSE
添加回复
请先登陆