生成2个text文件并压缩成zip文件返回给浏览器不生成临时文件

//生成2个text文件并压缩成zip文件返回给浏览器不生成临时文件
try {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("test.zip", "UTF-8"));
    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    ZipEntry zipEntry = new ZipEntry("test1.txt");
    zos.putNextEntry(zipEntry);
    zos.write("test1".getBytes());
    zos.closeEntry();
    zipEntry = new ZipEntry("test2.txt");
    zos.putNextEntry(zipEntry);
    zos.write("test2".getBytes());
    zos.closeEntry();
    zos.close();
} catch (IOException e) {
    e.printStackTrace();

}

你可能感兴趣的:(java,前端,服务器)