Spring Mvc中用ResponseEntity方式下载文件如下:
@RequestMapping("/download") public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("fileName") String fileName) throws IOException { String path = request.getServletContext().getRealPath("/uploadImages/"); File dwFile = new File(path+File.separator+fileName); HttpHeaders headers = new HttpHeaders(); //下载显示的中文名,解决中文名称乱码问题 String downloadFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); //application/octet-stream:二进制流数据 headers.setContentDispositionFormData("attachment",downloadFileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(dwFile),headers, HttpStatus.CREATED); } }
时间: 2024-10-01 01:55:49