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

spring mvc 文件下载

时间:2015-05-19 14:29:05      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

在controller中进行代码编写;

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest req)
            throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //解决中文名称乱码问题
        String filename = new String("你好.xls".getBytes("UTF-8"), "iso-8859-1");
        headers.setContentDispositionFormData("attachement", filename);
        String realpath = req.getSession().getServletContext()
                .getRealPath("/resources/upload/");
        String pathname = realpath + File.separator + "123";
        File file = new File(pathname);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

 

spring mvc 文件下载

标签:

原文地址:http://www.cnblogs.com/stono/p/4514435.html

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