控制器
@RequestMapping("download")
@ResponseBody
public void download(HttpServletRequest request, HttpServletResponse response,String filename,String proname,String name,Date time,String id ){
TorgUser vo = getCurrentUser(request).getOrguser();
String userid = vo.getId();
service.download(time,userid,id);
String fileName = null;
try {
fileName = new String(name.getBytes("GBK"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String realPath= ApplicationConstant.fileUrl;
int a = filename.indexOf("_");
String name1 = filename.substring(0, a);
String name2 = filename.substring(a+1,filename.length());
InputStream inputStream = null;
OutputStream out = null;
String path = realPath + "/" + proname + "/" + name1 + "/" + name2;
System.out.println(path);
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
inputStream = conn.getInputStream();
byte[] buffer = new byte[1024];
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setContentLength((int)conn.getContentLength());
response.setHeader("Content-Length", ""+(int)conn.getContentLength());
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
out = response.getOutputStream();
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
out.write(buffer,0,len);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
inputStream.close();
out.close();
out.flush();
}catch (Exception e){
e.printStackTrace();
}
}
}
web
<a id="xiazai" href="" style="display: none;"></a>
url:
$("#xiazai").attr("href",url);
document.getElementById("xiazai").click();