如果通过form表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,
页面会显示下载文件。
@RequestMapping(value = “/url”)
public void exportFile(HttpServletRequest req, HttpServletResponse response, String rptId)
throws Exception {
OutputStream out = null;
try {
String rptName = “file”;
String fileName = new String((rptName + excelAble.getFileSuffix()).getBytes(“GBK”),
“8859_1”);
response.reset();
response.setContentType(“application/octec-stream”);
response.setHeader(“Content-disposition”, “attachment; filename=” + fileName);
out = response.getOutputStream();
excelAble.exportFile(out);
} catch (Exception e) {
logger.error(e);
} finally {
if (out != null) {
out.close();
}
}
}