SpringMVC如何返回文件流到页面

颐牟权月 2016-06-03 12:34:49
其实就是一个下载功能。当我读取到流之后,下一步该如何操作?


@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
public
@ResponseBody
InputStream downLoad(Long id) {
InputStream is = ExcelUtils.outSampleExcel();
return is;
}


我不确定返回类型应该是InputStream 或者File。
下面那段代码又该如何实现?

response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((list.get(0).get("NAME").toString() + ".xls").getBytes(), "iso-8859-1"));



从文件流返回到页面下载该如何实现?
...全文
26227 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cczulee 2017-08-15
  • 打赏
  • 举报
回复
引用 1 楼 monkeyqqqq 的回复:
下载功能你在服务器读流干嘛 直接返回个文件路径让页面去请求不就完了
如果文件名是中文该怎么处理呢?
颐牟权月 2016-06-03
  • 打赏
  • 举报
回复
引用 5 楼 zhangjihao 的回复:
好吧,你帅你优雅): 加上: produces = {"application/vnd.ms-excel;charset=UTF-8"}试试:
@RequestMapping(value = "/downLoad", method = RequestMethod.GET,  produces = {"application/vnd.ms-excel;charset=UTF-8"})
,
刘辟~
颐牟权月 2016-06-03
  • 打赏
  • 举报
回复
引用 4 楼 wjm1993 的回复:
[quote=引用 3 楼 zhangjihao 的回复:] 客户要一车水泥,你把车给客户了,你的意思是“车给你,自己拉,爱咋咋的?” ^_^ 写一个原理性的代码给楼主: @RequestMapping(value = "/downLoad", method = RequestMethod.GET) public void downLoad(Long id, HttpServletResponse response) { InputStream is = null; response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try {filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");}catch (Exception e) {e.printStackTrace();} response.addHeader("Content-Disposition","attachment;filename=" + filename); OutputStream out = null; try{ is = ExcelUtils.outSampleExcel(); out = response.getOutputStream(); int length = 0; byte buffer[] = new byte[1024]; while((length = is.read(buffer)) != -1){ out.write(buffer, 0, length); } } catch (Exception e) { } finally{ //close.... } } 至于Spring MVC如何封闭得更优雅,不在本例之内。
非常感谢,如果可以的话,我还是希望可以拉的更优雅一点。 这是我现在的写法,写入xls的是乱码。请问这个应该是用POI创建xls时的问题还是response返回时的问题呢?

@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
    public ResponseEntity<byte[]> downLoad(Long id) throws IOException {
        //todo  get List<GeneSampleRecord>

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "table.xls");
        return new ResponseEntity<byte[]>(ExcelUtils.outSampleExcel(),
                headers, HttpStatus.CREATED);
    }
[/quote] 已经成功解决,请收下我的膝盖。
howsun_zh 2016-06-03
  • 打赏
  • 举报
回复
好吧,你帅你优雅): 加上: produces = {"application/vnd.ms-excel;charset=UTF-8"}试试:
@RequestMapping(value = "/downLoad", method = RequestMethod.GET,  produces = {"application/vnd.ms-excel;charset=UTF-8"})
,
颐牟权月 2016-06-03
  • 打赏
  • 举报
回复
引用 3 楼 zhangjihao 的回复:
客户要一车水泥,你把车给客户了,你的意思是“车给你,自己拉,爱咋咋的?” ^_^ 写一个原理性的代码给楼主: @RequestMapping(value = "/downLoad", method = RequestMethod.GET) public void downLoad(Long id, HttpServletResponse response) { InputStream is = null; response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try {filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");}catch (Exception e) {e.printStackTrace();} response.addHeader("Content-Disposition","attachment;filename=" + filename); OutputStream out = null; try{ is = ExcelUtils.outSampleExcel(); out = response.getOutputStream(); int length = 0; byte buffer[] = new byte[1024]; while((length = is.read(buffer)) != -1){ out.write(buffer, 0, length); } } catch (Exception e) { } finally{ //close.... } } 至于Spring MVC如何封闭得更优雅,不在本例之内。
非常感谢,如果可以的话,我还是希望可以拉的更优雅一点。 这是我现在的写法,写入xls的是乱码。请问这个应该是用POI创建xls时的问题还是response返回时的问题呢?

@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
    public ResponseEntity<byte[]> downLoad(Long id) throws IOException {
        //todo  get List<GeneSampleRecord>

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "table.xls");
        return new ResponseEntity<byte[]>(ExcelUtils.outSampleExcel(),
                headers, HttpStatus.CREATED);
    }
howsun_zh 2016-06-03
  • 打赏
  • 举报
回复
客户要一车水泥,你把车给客户了,你的意思是“车给你,自己拉,爱咋咋的?” ^_^ 写一个原理性的代码给楼主: @RequestMapping(value = "/downLoad", method = RequestMethod.GET) public void downLoad(Long id, HttpServletResponse response) { InputStream is = null; response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try {filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");}catch (Exception e) {e.printStackTrace();} response.addHeader("Content-Disposition","attachment;filename=" + filename); OutputStream out = null; try{ is = ExcelUtils.outSampleExcel(); out = response.getOutputStream(); int length = 0; byte buffer[] = new byte[1024]; while((length = is.read(buffer)) != -1){ out.write(buffer, 0, length); } } catch (Exception e) { } finally{ //close.... } } 至于Spring MVC如何封闭得更优雅,不在本例之内。
颐牟权月 2016-06-03
  • 打赏
  • 举报
回复
引用 1 楼 monkeyqqqq 的回复:
下载功能你在服务器读流干嘛 直接返回个文件路径让页面去请求不就完了
我又没说读的是本地文件,是从数据库取数据后生成的xls文件流。
狂暴的小猴儿 2016-06-03
  • 打赏
  • 举报
回复
下载功能你在服务器读流干嘛 直接返回个文件路径让页面去请求不就完了

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧