Java Encoding Mutual transfer (application/x-www-form-urlencoded)

Source: Internet
Author: User
Tags alphanumeric characters

In essence, Java.net.UrlEncoder is useful when converting a String to a static method in the Application/x-www-form-urlencoded MIME format, using the

But! In general, the Web application, when your server, page encoding, the request when the code has been modified to utf-8, still garbled, at this time the test to use the method written below

Use Urldecoder to decode the garbled data,

Here is a simple way to say the garbled scenes and simple conversion of the principle of execution:

First, when the form form prompts for data, the default content-type: application/x-www-form-urlencoded,

Of course, it can also be: Multipart/form-data, multipart/form-data generally used as form form to stream the form of data submitted, then set, enctype for the Form-data form,

However, in general, the baby sang face mentioned, the submission method is application/x-www-form-urlencoded, when the environment and so on have been configured for unified encoding, still appear Chinese characters

To the background is decoded processing situation (that is, the so-called garbled), at this time can try, the article mentioned below, using Urldecoder to decode and convert, and then converted to Chinese characters,

Of course, in the use of the scene, in fact there are many situations need to distinguish, of course, it must be strictly in accordance with the format to do the corresponding processing is the most correct way, there are some things need to deal with

Come here first, ^_^ bye.

Java uses Urldecoder and urlencoder to process Chinese

A Urlencoder

A utility class that is encoded in HTML format. The class contains a static method that converts a String to the application/x-www-form-urlencoded MIME format. For more information about HTML format encoding, see the HTML specification.

When encoding String, use the following rules:

The alphanumeric characters "a" to "Z", "a" to "Z" and "0" to "9" remain unchanged.
The special characters ".", "-", "*" and "_" remain unchanged.
The space character "" is converted to a plus sign "+".

All other characters are unsafe, so you first use some encoding mechanisms to convert them to one or more bytes. Each byte is then represented by a 3-character string "%xy", where XY is the two-bit hexadecimal representation of the byte. The recommended encoding mechanism is UTF-8. However, for compatibility reasons, if an encoding is not specified, the default encoding for the corresponding platform is used.

For example, using the UTF-8 encoding mechanism, the string "The Stringü@foo-bar" will be converted to "The+string+%c3%bc%40foo-bar" because in UTF-8, the character U is encoded as two bytes, C3 (hex) and BC (16 in Character @ is encoded as a byte 40 (hexadecimal).

Two Urldecoder

This class contains a static method that decodes a String from the application/x-www-form-urlencoded MIME format.

The conversion process is exactly the reverse of the process used by the Urlencoder class. Assume that all characters in the encoded string are one of the following: "A" to "Z", "a" to "Z", "0" to "9" and "-", "_", ".", and "*". The "%" character is allowed, but it is interpreted as the start of a special escape sequence.

The following rules are used in the conversion:

The alphanumeric characters "a" to "Z", "a" to "Z" and "0" to "9" remain unchanged.
The special characters ".", "-", "*" and "_" remain unchanged.
The plus sign "+" is converted to the space character "".

The "%XY" format sequence is treated as a byte, where XY is a two-bit hexadecimal representation of 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by characters whose encoding can generate these contiguous bytes. You can specify the encoding mechanism for decoding these characters, or, if not specified, the default encoding mechanism of the platform.

There are two possible ways that the decoder can handle illegal strings. One method is to throw a IllegalArgumentException exception regardless of the illegal character

Simple example:

Java Encoding Mutual transfer (application/x-www-form-urlencoded)

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.