SpringMVC文件下载
废话不多说直接上代码
@RequestMapping(value = "/outReport.json", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public ResponseEntity<byte[]> download( String fileUrl,HttpServletResponse response, HttpServletRequest request) throws IOException {
String shuchu=request.getSession().getServletContext().getRealPath(EcConstants.REPORT_TEMPLATE_TEMP_DIR);
File file = new File(fileUrl);
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + java.net.URLEncoder.encode("生成的文件名.docx", "UTF-8"));
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;
}