Stacked Q&A

Without creating ZIP file on server (hard disk) how to let client download a ZIP file with custom selected files?
  • 0 voting
  • 2022-08-12 00:00

    I have a requirement in which I will let users choose a list of files (using check box) and let him download all the files he selected to download by zipping them and show him the download prompt.

    I am able to do this by creating the zip file on the server hard disk and making use of this file and let the user to download the files, everything is going fine. But I don't want to create a zip file on the hard-disk, I want to create it on the fly and let user download the zip file, at any point of time there should be no .zip file on my server.

  • 1 80 0 0
첨부 파일
답변
총 1개의 답변이있습니다.
  • 답변일: 2022-08-12 00:00

    There is no difference in creating the file on the hard-disk or returning it by your spring-mvc-endpoint. Just make sure you redirect the ZipOutputStream to your response.getOutputStream()

    e.g.:

    response.setHeader("Content-disposition","attachment; filename=myfile.zip");         
    ZipOutputStream stream = new ZipOutputStream(response.getOutputStream());
    ZipEntry zipEntry = new ZipEntry(myFile);
    zipStream.putNextEntry(zipEntry);
    //write your file-content to zipstream e.g. like this
    zipStream.write(Files.readAllBytes(Paths.get("myfile")));
    
  • 0 0 퍼블 채택!
Tages
    활동순위
    유저
      파트너사