Struts implements a Chinese garbled solution for File Download

Source: Internet
Author: User
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.
 

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.