【发布时间】:2017-11-22 05:32:36
【问题描述】:
我目前需要创建一个 zip 文件以供下载。这应该包含两 (2) 个要从字符串变量创建的 csv 文件。我不知道该怎么做。我的草稿在下面。
public @ResponseBody Object getFileV1(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=Reassigned Tickets Report " + new Date().toString() + ".zip");
String stringValue1 = "This is a test value for csv1";
String stringValue2 = "This is a test value for csv2";
InputStream is1 = new ByteArrayInputStream(stringValue1.getBytes("UTF-8"));
InputStream is2 = new ByteArrayInputStream(stringValue2.getBytes("UTF-8"));
ZipInputStream zin;
ZipEntry entry;
ZipOutputStream zout= new ZipOutputStream(response.getOutputStream());
zin = new ZipInputStream(is1);
entry = zin.getNextEntry();
zout.putNextEntry(entry);
zin = new ZipInputStream(is2);
entry = zin.getNextEntry();
zout.putNextEntry(entry);
zout.closeEntry();
zin.close();
zout.close();
response.flushBuffer();
return null;
} catch (Exception e) {
e.printStackTrace();
return e;
}
}
显然这是行不通的。可能是因为我还是个新手。请多多包涵。
我在调用“zout.putNextEntry”的那一行得到一个“java.lang.NullPointerException”。非常感谢您的建议。提前谢谢你。
【问题讨论】:
标签: java spring-mvc httpresponse zipfile download