简体   繁体   中英

Java API/util to find and replace unsafe characters with their percent encoded forms?

有谁知道像样的Java API / util来用其百分比编码形式查找和替换URL中的不安全字符?

"http://google.com?" + URLEncoder.encode("...", "UTF-8");

See javadocs .

One should add the expected character encoding.

See Java - Convert String to valid URI object for pure Java style:

String urlStr = "http://abc.dev.domain.com/0007AC/ads/800x480 15sec h.264.mp4";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

Beware that it handles protocols available to Java natively. Otherwise you may need to register your own one like "s3" for Amazon S3 URIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM