I would like a function that detects invalid characters in a URL and replaces them with their encoded equivalent. EG:
ensureValidUrl("http://example.com/invalid url/") // "http://example.com/invalid%20url/"
I had tried URLEncoder.encode
, but this also encodes the protocol, which I don't want.
static String getValidURL(String invalidURLString){
try {
// Convert the String and decode the URL into the URL class
URL url = new URL(URLDecoder.decode(invalidURLString, StandardCharsets.UTF_8.toString()));
// Use the methods of the URL class to achieve a generic solution
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
// return String or
// uri.toURL() to return URL object
return uri.toString();
} catch (URISyntaxException | UnsupportedEncodingException | MalformedURLException ignored) {
return null;
}
}
Using a combination of URI and URL classes, your solution can be achieved. More on URL and URI and Charsets .
Usage:
System.out.println(getValidURL("http://example.com/invalid url/"));
// http://example.com/invalid%20url/
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.