使用springmvc中ResponseEntity下载文件

本文介绍了在Spring MVC项目中如何实现文件下载功能,详细讲解了使用ResponseEntity的两种方法。第一种方法涉及设置请求体、请求头和状态码,但可能在某些浏览器中直接打开文件而不是下载。第二种方法则利用Servlet的输出流ServletOutputStream,实现过程与第一种类似。

今天遇见了一个点击table列表中文件名,实现下载文件的功能。
因为这边的项目用的springmvc做的容器,以下是通过ajax访问该url通过输入流将数据(该数据通过url携带)中携带的文件内容(content)转换成字节存入缓存中。
通过springmvc封装的下载实现ResponseEntity将字符串输出成.docx文件下载。

第一种方法:

     ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);

通过设置请求体(body),请求头(headers),请求状态(statusCode)传回前端页面。

    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;

    @RequestMapping("/download/{content}/{title}")
    public ResponseEntity<byte[]> download(HttpServletRequest        request,@PathVariable String content ,@PathVariable String title ) throws IOException {
        //设置缓存
        byte[] body = null;
        //字节流读取String数据
        ByteArrayInputStream is = new ByteArrayInputStream("content".getBytes());
        body = new byte[is.available()];
        is.read(body);
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + title+".docx");
        //设置请求状态
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }

页面不能通过ajax访问url。
必须通过页面window.location.href = resqURL;属性来访问。

设置文件名时,有url中文乱码问题,解决方法。

//设定URLEncoder编码集为utf-8。具体原理可以百度。
String fileName =URLEncoder.encode(title+".doc", "utf-8");
//或者使用ResponseEntity中HttpHeaders headers对象的.add()方法设定报文头
//headers.add("Content-Disposition", "attchement;filename=" + fileName);

上面一种方法因为使用了return,可以会出现在特定的浏览器中自行打开文件,而不是下载,如ie浏览器对word、excel等文件的处理。

第二种方法:

使用servlet的输出流 ServletOutputStream

具体实现和上面相似:


@RequestMapping("/download/{infoId}/{fundCode}")
//这里创建了HttpServletResponse的实例对象。
    public void download(@PathVariable String infoId,@PathVariable String fundCode,HttpServletResponse response) throws IOException {
        //和上面一样,将content数据使用流放入缓存body中
        byte[] body = null;
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        body = new byte[is.available()];
        is.read(body);
        //将ContentType、Header等需要的设置放进 HttpServletResponse传回前端
        String fileName =URLEncoder.encode(title+".doc", "utf-8");

        response.setContentType("application/msword");
        response.addHeader("Content-Disposition", "attachment;filename=" +fileName);
        response.setContentLength(body.length);  
        //获取Servlet的输出流ServletOutputStream 
        ServletOutputStream sos = response.getOutputStream();  
        sos.write(body);  
        if (sos != null) {  
            sos.close();  
        }
    }
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值