Encode only part of the host URL and make sure you use IDN.toASCII()
, notIDN.toUnicode()
String fullUrl = "http://見.香港/images/wonton.jpg";
URL url = new URL(fullUrl);
url.getProtocol();
url.getHost();
url.getPath();
String asciiHost = IDN.toASCII(url.getHost());
String validUrl = url.getProtocol() + "://" + asciiHost + url.getPath();
System.out.println(validUrl);
BufferedImage bi = ImageIO.read(new URL(validUrl));
Console output:
http: //xn--nw2a.xn--j6w193g/images/wonton.jpg
Note that you may need to use the URLEncode part of a URI resource if it should contain characters such as spaces.
source
share