search
HomeJavaJavaBaseWhat to do if the java url is Chinese garbled?

What to do if the java url is Chinese garbled?

Nov 15, 2019 am 09:46 AM
javaurlChinese garbled

What to do if the java url is Chinese garbled?

url Chinese garbled java

1. Transcode the string: newString("xxxxx" .getBytes("iso-8859-1"),"utf-8")

This transcoding method has great disadvantages because it uses the specified character set to encode this String into a byte sequence, and stores the result into a new byte array, then decodes the resulting byte array using the specified character encoding to construct a new String string. In this case, it is possible to encounter a situation where all one Chinese character cannot be decoded. In this way, the previous words will be displayed normally, but the last word may be garbled.

So it is not recommended to use this method.

2. Transcode before passing the parameters, and then transcode them back after receiving the parameters.

There are two ways to do this:

The first one:

Before passing parameters: use java.net. URLEncoder.encode("xxxx","utf-8"), convert Chinese into hexadecimal characters.

After receiving the parameters: Use java.net.URLDncoder.decode("xxxx", "utf-8") to convert hexadecimal characters into Chinese.

What needs to be noted with this method is that after using encode, special characters will appear. At this time, the special characters need to be replaced with the corresponding hexadecimal. Because special characters are also garbled when passed as parameters in the URL path.

Second type:

Before passing parameters: encodeURI(“xxxx”).

After receiving parameters: Use java.net.URLDncoder.decode("xxxx", "utf-8") to convert hexadecimal characters into Chinese.

What needs to be noted in this method is that after using encodeURI to transcode, special characters will appear. At this time, the special characters need to be transcoded, so use encodeURI twice, that is:

encodeURI(encodeURI("xxxx")).

These two transcoding methods are very useful, so it is recommended that everyone use them.

Specific usage:

1. Client:

url=encodeURI(url);

Server:

String linename = new String(request.getParameter(“name”).getBytes(“ISO-8859-1”),“UTF-8”);

2. Client:

url=encodeURI(encodeURI(url)); //用了2次encodeURI

Server:

String linename = request.getParameter(name);
//java : 字符解码
linename = java.net.URLDecoder.decode(linename , “UTF-8”);

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of What to do if the java url is Chinese garbled?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment