Code for packaging and downloading multiple JSP files
Last Update:2018-12-08
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 ();
}
%>