【发布时间】:2015-05-09 14:10:53
【问题描述】:
我尝试使用 Zip4j 生成 zip 文件以供下载。 但我总是得到错误:
2015-05-09 15:56:24.306 错误 11748 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet]在路径 [] 的上下文中抛出异常 [请求处理失败;嵌套异常是 java.lang.IllegalStateException: getOutputStream() has already been called for this response] 根本原因
当调用 zout.putNextEntry(file, null);在下面的函数中
public void EmployeeEncyrptedZipFileDownload(HttpServletResponse response, @RequestParam(value = "id", required = true) int employeeId) throws IOException, ZipException
{
//Prepare text file contents
String fileContent = "Hallo Welt";
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
final StringBuilder sb = new StringBuilder(fileContent);
final ZipOutputStream zout = new ZipOutputStream(response.getOutputStream());
File file = new File("mytext.txt");
zout.putNextEntry(file, null);
byte[] data = sb.toString().getBytes();
zout.write(data, 0, data.length);
zout.closeEntry();
zout.finish();
}
这怎么可能,因为 putNextEntry 函数甚至没有得到响应,而是已经获得了流?
【问题讨论】:
标签: java servlets web download zip4j