//controller中还是要主语文件名,切记将文件类型传递过来
@RequestMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request, HttpServletResponse response,
@RequestParam("fileName") String fileName) throws Exception{
String path = request.getServletContext().getRealPath("/uploadFile/");
File thefile = new File(path+File.separator+fileName);
// 下载显示文件名,为了解决中文名称乱码
HttpHeaders httpHeaders = new HttpHeaders();
String downloadFileName = new String (fileName.getBytes("UTF-8"),"iso-8859-1");
// 设置下载方式为attachment
httpHeaders.setContentDispositionFormData("attachment",downloadFileName);
// 用二进制流的方式下载文件
httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(thefile),httpHeaders, HttpStatus.CREATED);
}
jsp中
<a href="download?fileName=文件名">文件名</a>