javahttp返回⽂件流_(java)如何同时返回⽂件流和状态信息[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.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 = OutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = ad(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
java stream}finally {
try {
if (bis != null){
bis.close();
}
if (bos != null){
bos.close();
}
} catch (IOException e) { e.printStackTrace();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论