java - 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.
解决方案
在硬盘上创建文件或通过 spring-mvc-endpoint 返回文件没有区别。只要确保你重定向ZipOutputStream
到你的response.getOutputStream()
例如:
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")));
推荐阅读
- service-worker - 用户退出后可以向用户发送推送通知吗?
- typescript - 包含另一个数组的 Array 的 TypesScript 通用语法
- c++11 - Codeforces 中的“诊断检测到问题 [cpp.g++17-drmemory]: ~~Dr.M~~ Dr. Memory 版本 1.11.0”错误是什么?
- javascript - 如何将Object的值映射到整个数组中其对应键的出现次数作为数组?
- javascript - JSX:按字母顺序对对象进行排序
- java - 为什么我在构造函数上收到 Selenium init 错误?
- android - 将位图加载到 ImageView 可以旋转显示的图像
- delphi - 如何在 Delphi (FireMonkey) 中更改画布 FillMode
- java - 如何让我的输出从文本文件中读取?
- autodesk-forge - BIM 360 Docs 如何复制文件