Does anyone know of a decent Java API/util to find and replace unsafe characters in a URL with their percent-encoded forms?
Asked
Active
Viewed 939 times
1
-
Have you found any before posting the question here? Have you tested it? What's the problem you get with such library? Or you did nothing and want us to do research and tests for you? – Luiggi Mendoza Jul 16 '13 at 14:50
-
2http://docs.oracle.com/javase/6/docs/api/java/net/URLEncoder.html – JB Nizet Jul 16 '13 at 14:50
2 Answers
3
"http://google.com?" + URLEncoder.encode("...", "UTF-8");
See javadocs.
One should add the expected character encoding.

Joop Eggen
- 107,315
- 7
- 83
- 138
1
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.

Community
- 1
- 1

eduard.dudar
- 111
- 2
- 5