转自http://blog.csdn.net/clj198606061111/article/details/20743769
注:需要commons.io包
JSP界面:
window.top.location.href="${pageContext.request.contextPath}/center/saveWord?wordId=" + id;
后台代码:
@RequestMapping("/saveWord")
public ResponseEntity<byte[]> saveWord(int wordId, HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = request.getSession().getServletContext().getRealPath("/images/word" + wordId + "/cauImg.png");
File file = new File(path);
System.out.println(path);
String dfileName = new String("测试.png".getBytes("UTF-8"), "iso8859-1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", dfileName);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
}