欢迎大家加入京东淘宝捡垃圾群:698433653
/**
* @param response 响应
* @Description: 文件下载
*/
@RequestMapping(value = "/downFile")
public void downFile(HttpServletResponse response,HttpServletRequest request) {
OutputStream out = null;
FileInputStream fis = null;
//文件路径
String filePath= request.getParameter("filePath");
//文件名
String fileName= request.getParameter("fileName");
try {
out = response.getOutputStream();
// 设置response的Header 防止文件名乱码
response.addHeader("Content-Disposition",
"attachment;filename="
+ new String((fileName).getBytes("utf-8"), "ISO-8859-1"));
response.setContentType("application/octet-stream");
fis = new FileInputStream(filePath);
int b;
while ((b = fis.read()) != -1) {
out.write(b);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
logger.error("执行 文件下载downFile 方法失败:" + e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
欢迎大家加入京东淘宝捡垃圾群:698433653