At the beginning of the page, the Action reads all the files in the file folder into an arraylist.
The Action Code is as follows:
Arraylist list = new arraylist ();
String Path = request. getrealpath ("/") + "file ";
String fullpath;
// System. Out. println (PATH );
Mydir = new file (PATH );
List. Clear ();
Contents = mydir. listfiles ();
For (INT I = 0; I <contents. length; I ++ ){
Fullpath = contents [I]. getname ();
List. Add (fullpath );
// System. Out. println (fullpath );
}
Request. setattribute ("list", list );
Actionforward forward = new actionforward ("/download. jsp ");
Return forward;
Then go to download. jsp. This page displays all the files and provides download connections. The Code is as follows:
<% @ Page Language = "Java" contenttype = "text/html; charset = GBK" Import = "Java. util. arraylist" %>
<Head>
<Style>
</Style>
</Head>
<Body>
<% Arraylist list = (arraylist) request. getattribute ("list ");
For (INT I = 0; I <list. Size (); I ++)
{
String A = java.net. urlencoder. encode (string) list. Get (I ));
Out. Print ("<a href =./loaded. do? Name = "+ A +"> "+ list. Get (I) +" </a> <br> ");
}
%>
</Body>
</Html>
Note: The purpose of the underlined code is to solve the problem.
Next, you can pass in to loadedaction directly or through a form.
The form code is as follows:
Package org. Aeolus. Struts. form;
Import javax. servlet. http. httpservletrequest;
Import org. Apache. Struts. Action. actionerrors;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionmapping;
Public class loadform extends actionform {
/*
* Generated Methods
*/
Private string name;
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}
The following is the code of the action.
Loadform Doc = (loadform) form;
String docname = new string (Doc. getname (). getbytes ("8859_1 "));
File F;
If (docname! = ""){
String docfullpath = request. getrealpath ("/");
F = new file (docfullpath + "file //" + docname );
Response. Reset ();
Response. setcontenttype ("application/X-msdownload; charset = GBK ");
System. Out. Print (response. getcontenttype ());
Response. setcharacterencoding ("UTF-8 ");
Docname = java.net. urlencoder. encode (docname, "UTF-8 ");
Response. setheader ("content-disposition", "attachment; filename =" + new string (docname. getbytes ("UTF-8"), "GBK "));
Bufferedinputstream BR = new bufferedinputstream (New fileinputstream (f ));
Byte [] Buf = new byte [1, 1024];
Int Len = 0;
Outputstream out = response. getoutputstream ();
While (LEN = Br. Read (BUF)> 0)
Out. Write (BUF, 0, Len );
Out. Close ();
Response. Wait ();
Actionforward forward = new actionforward ("/download. jsp ");
Return forward;
}
Return NULL;
Note: The purpose of the underlined code is to solve the problem.
Description
Response. setcharacterencoding ("UTF-8 ");
Docname = java.net. urlencoder. encode (docname, "UTF-8 ");
Response. setheader ("content-disposition", "attachment; filename =" + new string (docname. getbytes ("UTF-8"), "GBK "));
If this is not done, the file name to be downloaded is garbled.