首页 > 编程语言 > 详细

spring mvc 文件下载

时间:2015-05-19 14:26:23      阅读:202      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!