码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC之ResposeEntity下载文件

时间:2017-04-12 11:40:57      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:java   ima   upload   throws   ons   ping   二进制   tco   type   

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);
    }
}

  

Spring MVC之ResposeEntity下载文件

标签:java   ima   upload   throws   ons   ping   二进制   tco   type   

原文地址:http://www.cnblogs.com/foxting/p/6697933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!