我尝试使用 Zip4j 生成一个 zip 文件以供下载。但我总是得到错误:
2015-05-09 15:56:24.306 错误 11748 --- [nio-8080-exec-4] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet] path [] 抛出异常 [请求处理失败;嵌套异常是 java.lang.IllegalStateException: getOutputStream() 已经为此响应调用] 根本原因
当调用 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 函数甚至没有得到响应,而是已经获得了流?