本文共 821 字,大约阅读时间需要 2 分钟。
Spring Mvc中用ResponseEntity方式下载文件如下:
@RequestMapping("/download")public ResponseEntitydownload(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 (FileUtils.readFileToByteArray(dwFile),headers, HttpStatus.CREATED); }}
转载地址:http://gfkja.baihongyu.com/