POI SXSSFWorkbook 导出文件同时获取文件长度

InputStream in = null;
OutputStream out = null;
SXSSFWorkbook wb = result.getData();
try {
    Date today = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //设为"UTF-8"IE乱码,谷歌正常,设为"gb2312"IE、谷歌均正常
    String fileName = ExportExcelUtil.getFileName("team");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    //wb文件流只能输出一次
    wb.write(os);
    byte[] byteArray = os.toByteArray();
    //将输出流写入
    in = new ByteArrayInputStream(byteArray);
    response.addHeader("Content-Length", String.valueOf(in.available()) + "");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("application/octet-stream");// 设置强制下载不打开
    //文件名编码根据需求转换,一般使用UTF-8
    response.addHeader("Content-Disposition",
            "attachment;fileName=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1"));// 设置文件名
    out = response.getOutputStream();
    // 循环取出流中的数据
    byte[] b = new byte[1024];
    int len;
    while ((len = in.read(b)) > 0) {
        out.write(b, 0, len);
    }
    out.flush();
    out.close();
    in.close();
} catch (Exception e) {
    log.error("[导出][失败]",e);
} finally {
    try {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

上一篇:pymysql 链接mysql


下一篇:java的IO---文件的字节、字符、缓冲器的复制