ZipOutputStream.finish()并将文件写入错误的目录和Tomcat

时间:2017-02-03 13:13:16

标签: java tomcat zipoutputstream

当我为一个zipOutputStream调用.finish()和.close()以便在我的webApp页面上下载一个zip文件时,它将zip文件的内容保存在我的临时用户文件夹中(例如:c:/ user / appdata / local / temp)但我想将这些文件保存在TomcatHome / temp。

// setting headers
response.setContentType("application/zip");
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");

// creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);

for (int i = 0; i < listIdFile.size(); i++) {
  try {

    FileDTO file = this.fileDAO.getFileById(listIdFile.get(i));


    File fileFull = this.fileDAO.readFileFull(Integer.parseInt(file.getId()));

    zipOutputStream.putNextEntry(new ZipEntry(file.getNomeFile()));
    FileInputStream fileInputStream = new FileInputStream(fileFull);

    IOUtils.copy(fileInputStream, zipOutputStream);

    fileInputStream.close();
    zipOutputStream.closeEntry();

  } catch (Exception e) {
    log.error(e.getMessage());
    e.printStackTrace();
    // return downloadDocumentoDaLocale(response, listIdFile);

  }
}

if (zipOutputStream != null) {
  // zipOutputStream.finish();

  zipOutputStream.close();
  zipOutputStream.finish();
  IOUtils.closeQuietly(zipOutputStream);
}
IOUtils.closeQuietly(bufferedOutputStream);
IOUtils.closeQuietly(byteArrayOutputStream);

}

有什么建议吗?

谢谢

0 个答案:

没有答案