JSP Multiple Files package download code

Source: Internet
Author: User
Tags rar zip


The main code of the program is now posted, for everyone's needs when referenced under:   
 

<% @page import= "java.util.*, java.net.*, java.text.*, java.util.zip.*, java.io.*"%> <%!  
Static vector expandfilelist (string[] files, boolean incldirs) {vector v = new vector ();  
if (files = null) return V;  
for (int j=0 J < files.length; i++) V.add (New File (Urldecoder.decode (FILES[J)));  
for (int i=0 i < v.size (); i++) {File F = (file) v.get (i);  
if (F.isdirectory ()) {file[] fs = F.listfiles ();  
for (int n = 0; n < fs.length n++) V.add (fs[n));  
if (!incldirs) {v.remove (i);  
i--;  
}} return v;  
Class Writer2stream extends outputstream{Writer out;  
Writer2stream (Writer W) {super ();  
out = W;  
public void write (int i) throws ioexception{out.write (i);  
public void Write (byte[] b) throws ioexception{for (int j=0;j<b.length;j++) {int n=b[j];  
Convert byte to Ubyte n= ((n>>>4) &0xf) *16+ (N&0XF);  
Out.write (n); } public void Write (byte[] b, int off, int len) throws Ioexception{for (int j = off, J < off + Len; j + +) {int n=b[j];  
n = ((n>>>4) &0xf) *16+ (N&0XF);  
Out.write (n);  
%> <%/////Download Compressed package Vector v = expandfilelist (request.getparametervalues ("Selfile"), false);  
if (v.size () = = 0) {request.setattribute ("error", "No files selected");  
} else{File Dir_file = new file (Request.getrealpath ("") + "/myfile/");  
int dir_l = Dir_file.getabsolutepath (). Length ();  
Response.setcontenttype ("Application/zip");  
Response.setheader ("Content-disposition", "Attachment;filename=" Downloadname.rar ")";  
Out.clearbuffer ();  
Zipoutputstream zipout = new Zipoutputstream (new Writer2stream (out));  
Zipout.setcomment ("Download Selected files nas one WinRAR file:ndownloadname.rar");  
Zipout.setlevel (1);  
for (int i=0;i<v.size (); i++) {File f = (file) v.get (i);  
if (F.canread ()) {Zipout.putnextentry (New ZipEntry (F.getabsolutepath (). substring (dir_l+1))); Bufferedinputstream FR = new BufferedinputstreaM (New FileInputStream (f));  
byte buffer[] = new BYTE[0XFFFF];  
int b;  
while ((B=fr.read ())!=-1) Zipout.write (b);  
Fr.close ();  
Zipout.closeentry ();  
} zipout.finish ();  
Out.flush (); }%>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.