Troubleshoot Java engineering paths that contain Chinese

Source: Internet
Author: User

Problem:

The correct path is not found when the Java project path contains Chinese
***

Solve:

This is actually a problem with coding conversions. When we get the path using ClassLoader's GetResource method, the obtained path is encoded by Urlencoder.encode (path, "Utf-8"), and when there are Chinese and white spaces in the path, he converts the characters, so that is often not the real path we want, so we can call the Urldecoder.decode () method to decode, in order to get the original Chinese and white space path.
Java code:
String PackagePath = Url.getpath (). ReplaceAll ("%20", "");//resolve Path with spaces
PackagePath = Java.net.URLDecoder.decode (PackagePath, "utf-8"); To resolve paths that contain Chinese
***

Results:

/d:/java%e7%a8%8b%e5%ba%8f%ef%bc%88idea)/smartframework/core/target/classes/
After decoding:/d:/java program (IDEA)/smartframework/core/target/classes/
***

About decoding and encoding

Urlencoder.encode (string s, String enc)
Converts a string to the application/x-www-form-urlencoded format using the specified encoding mechanism

Urldecoder.decode (string s, String enc)
Decodes a application/x-www-form-urlencoded string using the specified encoding mechanism.

When sending using Urlencoder.encode encoding, when receiving the use of Urldecoder.decode decoding, all in accordance with the specified encoding format encoding, decoding, can ensure that no garbled

Troubleshoot Java engineering paths that contain Chinese

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.