@RequestMapping(value = "/downloadfj")
public void downloadfj(HttpServletRequest request, HttpServletResponse response) throws IOException {
String resourceUrl = request.getParameter("fjUrl");
String filePath = request.getSession().getServletContext().getRealPath(resourceUrl);
String name = request.getParameter("fjName");
String suffix = resourceUrl.substring(resourceUrl.lastIndexOf("."));
name = name + suffix;
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
name = URLEncoder.encode(name, "UTF-8");// IE浏览器
} else {
name = new String(name.getBytes("utf-8"), "ISO-8859-1");// 其他浏览器
}
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename=" + name);
File file = new File(filePath);
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
out.close();
in.close();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。