java http 返回文件流_(java)如何同时返回文件流和状态信息

编程知识 更新时间:2023-04-06 15:45:09

[Java] 纯文本查看 复制代码/**

* 输出创建的Excel

* [url=home.php?mod=space&uid=952169]@Param[/url] fileName

* @param wb

* @param resp

*/

public static void respOutPutExcel(String fileName, XSSFWorkbook wb, HttpServletResponse resp) {

ByteArrayOutputStream os = new ByteArrayOutputStream();

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

wb.write(os);

System.out.println("导出excel成功");

byte[] content = os.toByteArray();

InputStream is = new ByteArrayInputStream(content);

// 设置response参数,可以打开下载页面

resp.reset();

resp.setContentType("application/vnd.ms-excel;charset=utf-8");

resp.setHeader("Access-Control-Allow-Origin", "*");

resp.setHeader("Content-Disposition",

"attachment;filename=" + new String((fileName).getBytes(), "iso-8859-1"));

ServletOutputStream out = resp.getOutputStream();

bis = new BufferedInputStream(is);

bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];

int bytesRead;

// Simple read/write loop.

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}finally {

try {

if (bis != null){

bis.close();

}

if (bos != null){

bos.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

更多推荐

java http 返回文件流_(java)如何同时返回文件流和状态信息

本文发布于:2023-04-06 15:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/fc57ffd2576e548f9256dcbb3bd398f7.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   状态   信息   java   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 49768文章数
  • 14阅读数
  • 0评论数