Java后端代码如下:
@RequestMapping(value="/atoDownLoad")
private ModelAndView atoDownLoad(HttpServletResponse response,HttpServletRequest request) throws Exception {
ModelAndView mv = this.getModelAndView();
String strUrl = request.getParameter("path").replaceAll(",", "\\\\");
System.out.println(strUrl+"00000000000");
String filename = strUrl.substring( strUrl.lastIndexOf("\\") + 1);
filename = new String(filename.getBytes("iso8859-1"),"UTF-8");
String path = strUrl;
File file = new File(path);
//如果文件不存在
if(!file.exists()){
mv.setViewName("file/FileDownload");
return mv;
}
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(path);
//创建输出流
OutputStream out = response.getOutputStream();
//缓存区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区中
while((len = in.read(buffer)) > 0){
out.write(buffer, 0, len);
}
//关闭
in.close();
out.close();
mv.setViewName("file/FileDownload");
return mv;
}
jsp代码:
<a class="layui-btn layui-btn-sm layui-btn-normal " onclick="sendPath()" >
<img src="static/layer/image/icon/xinzeng.png">下载</a>
<a id="ssss" href="" name="" ></a>
function sendPath(){
var paths = $(".ace input:checked");
if(paths.length>1){
return;
}else{
var x = $(".ace input:checked").parent().next().next().val();
var newsss = x.replace('\\',',');
var hrefs = '<%=basePath%>fileLook/atoDownLoad?path=' + newsss;
$('#ssss').attr('href',hrefs);
$("#ssss").append("<span></span>");
$("#ssss span").click();
}
}