0

when i call .finish() and .close() for a zipOutputStream for download a zip file on a page of my webApp, it saves the content of zip file in my temp user folder ( example: c:/user/appdata/local/temp ) but i want to save this files in 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);

}

Any suggest?

Thank you

Alex
  • 111
  • 1
  • 13
  • Most likely you have not set up writing of the file correctly. Would you mind sharing an extract of your code with us that is demonstrating/describing your problem? – Alexander Feb 03 '17 at 13:31
  • i insert this upstairs – Alex Feb 03 '17 at 13:46
  • 1
    You're not writing any file in the above code - you're writing your zip file to a ByteArrayOutputStream but your code then just ignores what you've written and doesn't send it to the client. Are you talking about the location that the file gets saved in **by your web browser**? – Erwin Bolwidt Feb 03 '17 at 14:23
  • when the operation is flush(), i control my c:/user/appdata/local/temp and here, there are the files in the zip file. I want that the path where these files are write is tomcat home/temp – Alex Feb 03 '17 at 18:00

0 Answers0