Home > Back-end >  Java download file successful English, the Chinese file failed. The problem of Chinese garbled
Java download file successful English, the Chinese file failed. The problem of Chinese garbled

Time:10-08


Protected void doGet (it request, HttpServletResponse response) throws ServletException, IOException {
//1. Receive parameters
String path=request. The getParameter (" filename ");
System. The out. Println (" path "+ path);
If (path!=null) {
Path=new String (path. GetBytes (" ISO - 8859-1 "), "utf-8");
}
File file=new File(path);
//2. Download: set up two head and a flow
//get the file name
The String filename=file. The getName ();
System. The out. Println (" filename1 "+ filename);
//set the content-type
String type=getServletContext (.) getMimeType (filename);
The response. SetContentType (type);
//determine the browser type
String agent=request. GetHeader (" the user-agent ");

If (agent. The contains (" Firefox ")) {
//Firefox use base64 encoding
Filename=DownloadUtils. Base64EncoderFileName (filename);
} else {
//or other IE browser
filename=URLEncoder. Encode (filename, "utf-8");
Filename=filename. Replace (" + ", "");

System. The out. Println (" filename2 "+ filename);
}
//set the Content - Disposition
The response. SetHeader (" the Content - the Disposition ", "legislation; Filename="+ filename);
//set a representative file input stream
InputStream is=new FileInputStream (file);
OutputStream OS=response. GetOutputStream ();
//3. The two flow docking
int len=0;
Byte [] b=new byte [1024].
While ((len=is read (b))!=1) {
OS. Write (b, 0, len);
}
Is the close ();
}
front end
<%
Queue The queue=new LinkedList (a);
The File root=new File (" F://upload ");
The queue. The offer (root);
while(! Queue. IsEmpty ()) {
The File File=queue. The poll ();
The File files []=File. ListFiles ();
For (File f: files) {
If (f.i sFile ()) {
% & gt;

<% %=f.g etName () & gt;

<%
} else {
The queue. The offer (f);
}
}

}
% & gt;

Image name is: Chinese. JPG

This is the console print
Path: F:/upload/1/11/Chinese. JPG
Filename1:1?? .jpg
Filename2:2% 3 f % 3 f JPG

This is a browser
http://localhost:8080/web_test5/DownloadListServlet? Filename=F:/upload/1/11/Chinese. JPG



Java. IO. FileNotFoundException: F: \ upload \ 1 \ \?? Slogan.jpg (filename, directory name, or volume method is not correct,

Tried to decoding and encoding also not line, or gibberish, download file successfully in English, Chinese language file failed,
Help bosses!!!!! How do you solve this Chinese garbled

CodePudding user response:

 to help bosses!!!!!

CodePudding user response:

CodePudding user response:

Don't judge browser type, directly with the code try
 filename=agent. ToLowerCase (). The indexOf (" msie ")==1? New String (filename. GetBytes (" utf-8 "), "iso8859-1") : URLEncoder. Encode (filename, "utf-8"); 

CodePudding user response:

Or not, or the code

CodePudding user response:

CodePudding user response:

  • Related