简体   繁体   中英

Without creating ZIP file on server (hard disk) how to let client download a ZIP file with custom selected files?

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.

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()

eg:

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")));

使用fileinputstream读取所有文件,转换为字节数组,然后在响应标题中将响应类型设置为“ content-type:application / zip”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM