JavaWeb 實現(xiàn)多個文件壓縮下載功能
文件下載時,我們可能需要一次下載多個文件。批量下載文件時,需要將多個文件打包為zip,然后再下載。
實現(xiàn)思路有兩種:
一是將所有文件先打包壓縮為一個文件,然后下載這個壓縮包,
二是一邊壓縮一邊下載,將多個文件逐一寫入到壓縮文件中。我這里實現(xiàn)了邊壓縮邊下載。
下載樣式:

點擊下載按鈕,會彈出下載框:

下載后就有一個包含剛剛選中的兩個文件:


代碼實現(xiàn):
FileBean
public class FileBean implements Serializable {
private Integer fileId;// 主鍵
private String filePath;// 文件保存路徑
private String fileName;// 文件保存名稱
public FileBean() {
}
public Integer getFileId() {
return fileId;
}
public void setFileId(Integer fileId) {
this.fileId = fileId;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
控制層:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public String download(String id, HttpServletRequest request,
HttpServletResponse response) throws IOException {
String str = "";
if (id != null && id.length() != 0) {
int index = id.indexOf("=");
str = id.substring(index + 1);
String[] ids = str.split(",");
ArrayList<FileBean> fileList = new ArrayList<FileBean>();
for (int i = 0; i < ids.length; i++) {// 根據(jù)id查找genericFileUpload,得到文件路徑以及文件名
GenericFileUpload genericFileUpload = new GenericFileUpload();
genericFileUpload = genericFileUploadService.find(Long.parseLong(ids[i]));
FileBean file = new FileBean();
file.setFileName(genericFileUpload.getFileName());
file.setFilePath(genericFileUpload.getFilePath());
fileList.add(file);
}
//設置壓縮包的名字
//解決不同瀏覽器壓縮包名字含有中文時亂碼的問題
String zipName = "download.zip";
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename="+ zipName);
//設置壓縮流:直接寫入response,實現(xiàn)邊壓縮邊下載
ZipOutputStream zipos =null;
try{
zipos=new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipos.setMethod(ZipOutputStream.DEFLATED);//設置壓縮方法
}catch(Exception e){
e.printStackTrace();
}
DataOutputStream os=null;
//循環(huán)將文件寫入壓縮流
for(int i=0;i<fileList.size();i++){
String filePath=fileList.get(i).getFilePath();
String fileName=fileList.get(i).getFileName();
File file=new File(filePath+"/"+fileName);//要下載文件的路徑
try{
//添加ZipEntry,并ZipEntry中寫入文件流
//這里,加上i是防止要下載的文件有重名的導致下載失敗
zipos.putNextEntry(new ZipEntry(i+fileName));
os=new DataOutputStream(zipos);
InputStream is=new FileInputStream(file);
byte[] b = new byte[100];
int length = 0;
while((length = is.read(b))!= -1){
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
}catch(Exception e){
e.printStackTrace();
}
}
//關閉流
try {
os.flush();
os.close();
zipos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "redirect:list.jhtml";
}
總結
以上所述是小編給大家介紹的JavaWeb 實現(xiàn)多個文件壓縮下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
spring cloud zuul 與 sentinel的結合使用操作
這篇文章主要介紹了spring cloud zuul 與 sentinel 的結合使用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring Cloud Alibaba和Dubbo融合實現(xiàn)
這篇文章主要介紹了Spring Cloud Alibaba和Dubbo融合實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
Spring BeanPostProcessor源碼示例解析
這篇文章主要為大家介紹了Spring BeanPostProcessor源碼示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
SpringMVC中@RequestMapping注解用法實例
通過@RequestMapping注解可以定義不同的處理器映射規(guī)則,下面這篇文章主要給大家介紹了關于SpringMVC中@RequestMapping注解用法的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06

