Java Special Character URL escape

Source: Internet
Author: User

If the URL contains special characters, such as #, +, and &, and is not escaped, HTTP cannot be used for downloading (the client accesses httpserver through the get method ).

For example, the URL is: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/Only Love + can_the verse.mp3

If you do not escape the URI, after receiving the GET command in httpserver, there is an operation to decode the URI (if you do not execute the decode operation, you do not need to escape it, this is because some URLs perform the encode operation, and all the operations must be decode ).
Convert to a space, and then use the data in httpserver according to the decode URI. Because there is a plus sign in httpserver, all errors that do not exist in the http404 resource will be reported. The solution is to replace the plus sign with "% 2B" before Decode. Decode converts "% 2B" to "+" and then get.

The whole URL Processing in the dongle is:

1. Obtain the original URL: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/Only Love + can_the verse.mp3

2. Escape special characters in the original URL: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/only love % 2bcan_the verse.mp3

3. Click http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/only love % 2bcan_the verse.mp3

4. the URL for dongle to fetch data from the mobile phone httpserver is: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/Only Love + can_the verse.mp3

5. httpserver escapes the received URL: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/only love % 2bcan_the verse.mp3

6. httpserver decode the escaped URL and obtain: http: // 192.168.49.20: 8192/mnt/sdcard/miui/music/MP3/Only Love + can_the versecard.

7. dongle is successfully downloaded and played.

It seems like a circle is around, and it's okay to find something. Without the decode process, you don't need this round. However, in order to be compatible with Chinese names, the image URL must be encoded. decode the URL when it comes to dongle get to httpserver. You can also determine whether the get image is an image when decode is used. If it is not an image, decode it.

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.