Code for packaging and downloading multiple JSP files

Source: Internet
Author: User

Now Program Main Code Post it for your reference:
<% @ 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 the compressed file 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 [0 xFFFF];
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.