JSP or servlet (and upload download file) Chinese garbled or not show solution

Source: Internet
Author: User

Chinese garbled solution in JSP or servlet:
1. By Post Request method:
Set before getting the request parameter:
Request.setcharacterencoding ("Utf-8");
To set the output encoding:
Response.setcontenttype ("Text/html;charset=utf-8");
or <%@ page contenttype= "Text/html;charset=utf-8"%>

2. Request in a Get way
The settings requested in get mode setcharacterencoding are invalid.
We need to make a separate transcoding of each parameter after getting the parameters.
String name =request.getparameter ("name");
Name=new String (Name.getbytes (), "utf-8");

3. Garbled in the database:
Set the encoding of the database to Utf-8

4. Use the filter to solve the Chinese garbled problem:


----------------------------------------------------------- 5. Upload and download of garbled or Chinese do not show problem resolution:
JSP or servlet upload file Chinese name garbled solution:

String filen=item.getname ();
String Filename=new string (filen.getbytes ("iso-8859-1"), "Utf-8");
String Filename=new string (Filen.getbytes (), "utf-8");//UTF-8 encoding of the extracted file name,
The above can solve the upload file name garbled and browser display and stored in the database are displayed normal.


troubleshoot problems when downloading files that are not displayed in Chinese:

	Response.setcontenttype ("Application/x-msdownload");		Response.setcontenttype ("Application/octet-stream");
		Response.setheader ("Content-disposition", "attachment;filename=" +
New String (Filename.getbytes ("Utf-8"), " Iso-8859-1 "));
Cause analysis, because the HTTP header default encoding for iso-8859-1 and we upload files in the download file process, extracted to the file name through the HTTP header.

So we need to upload the file name to be extracted to the UTF-8, and then in the download we also have to reverse transcoding for iso-8859-1.

I blog Station Original:


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.