跳到主要内容

导出文件及压缩

· 阅读需 2 分钟
flankx

1 导出文件

  • 1.1 设置返回的请求头
Response HeaderValueDesc
Content-typeapplication/json资源的MIME类型
Content-Dispositionattachment;filename=xxx.json展示形式
  • 1.2 获取请求导出的资源并写入输出流
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.addHeader("Content-Disposition", "attachment;filename=xxx.json");

String content = ...; // 此处导出字符串
try (ServletOutputStream outStr = response.getOutputStream();
BufferedOutputStream buff = new BufferedOutputStream(outStr)) {
buff.write(content.getBytes(StandardCharsets.UTF_8));
buff.flush();
} catch (Exception e) {
e.printStackTrace();
}

2 导出文件并压缩

2.1 使用压缩工具Apache Commons Compress

  • 引入POM依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.22</version>
</dependency>
  • 加入Zip压缩
ZipArchiveOutputStream zipOutput = ...;
ZipArchiveEntry entry = new ZipArchiveEntry(name);
entry.setSize(size);
zipOutput.putArchiveEntry(entry);
zipOutput.write(contentOfEntry);
zipOutput.closeArchiveEntry();
  • 多个文件加入Zip压缩
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=xxx.zip");
response.setContentType("application/zip");

List<content> texts = ...; // 此处导出字符串列表
try (ServletOutputStream outStr = response.getOutputStream();
ZipArchiveOutputStream zipOutput = new ZipArchiveOutputStream(outStr)) {
for (int i = 0; i < texts.size(); i++) {
byte[] content = texts.get(i).getBytes(StandardCharsets.UTF_8)
String entryName = "xxx_" + i + ".txt"
ZipArchiveEntry entry = new ZipArchiveEntry (entryName);
entry.setSize(content.length);
zipOutput.putArchiveEntry(entry);
zipOutput.write(content);
zipOutput.closeArchiveEntry();
}
} catch (IOException e) {
e.printStackTrace();
}

3 示例代码

Loading Comments...