search
HomeJavaJavaBaseSolution to garbled url in java

Solution to garbled url in java

Dec 03, 2019 am 10:29 AM
java

Solution to garbled url in java

Solution to garbled url in java: (Recommended: java video tutorial)

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 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, you may encounter the situation that you cannot decode all the Chinese characters. 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 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: Character decoding

linename = java.net.URLDecoder.decode(linename , “UTF-8”);

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Solution to garbled url in java. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools