文件下載時,我們可能需要一次下載多個文件。批量下載文件時,需要將多個文件打包為zip,然后再下載。
實現思路有兩種:
一是將所有文件先打包壓縮為一個文件,然后下載這個壓縮包,
二是一邊壓縮一邊下載,將多個文件逐一寫入到壓縮文件中。我這里實現了邊壓縮邊下載。
下載樣式:
點擊下載按鈕,會彈出下載框:
下載后就有一個包含剛剛選中的兩個文件:
代碼實現:
filebean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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; } } |
控制層:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
@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++) { // 根據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,實現邊壓縮邊下載 zipoutputstream zipos = null ; try { zipos= new zipoutputstream( new bufferedoutputstream(response.getoutputstream())); zipos.setmethod(zipoutputstream.deflated); //設置壓縮方法 } catch (exception e){ e.printstacktrace(); } dataoutputstream os= null ; //循環將文件寫入壓縮流 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 實現多個文件壓縮下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/huanhuanxiaoxiao/article/details/76422328