Reputation: 322
Having trouble setting up a URL connection with Chinese characters in the URL. It works with Latin characters:
String xstr = "维也纳恩斯特哈佩尔球场" ;
URI uri = new URI("http","ajax.googleapis.com","/ajax/services/language/detect","v=1.0&q="+xstr,null);
URL url = uri.toURL();
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream() ;
The getInputStream() call results in:
java.lang.IllegalArgumentException: Invalid uri 'http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=???????????': Invalid query
Upvotes: 3
Views: 6964
Reputation: 186
Downloading image url with special characters. Below snippet show how to read image property from URL with special characters.
replace and with actual IP address and port number with your actual port number.
In case If you are working with domain, you can replace : with the domain name.
Replace the <file_path> with your file path.
try {
String fileName = "pexels-martin-péchy-1866149%20(1).jpg";
URI uri = new URI("http://<IP>:<port>/<file_path>/" + fileName);
URL url = new URL(uri.toASCIIString());
BufferedImage image = ImageIO.read(url);
int height = image.getHeight();
int width = image.getWidth();
System.out.println("Image downloaded successfully!: " + url.toString()+" height: "+height);
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 599
axtavt's answer above saved me from insanity, thanks! Just one comment (I could not figure out how to comment below the answer:)
If you start with a URL, you need to encode quotes before you build the URI:
String s = "your_url?with=\"quotes\"";
URI su = new URI (s.replaceAll("\"", "%22");
URL ur = new URL( su.toASCIIString());
Upvotes: 2
Reputation: 242786
The problem is caused by the fact that URI.toURL()
doesn't percent-encode non-ASCII characters. Use the following instead:
URL url = new URL(uri.toASCIIString());
Upvotes: 8
Reputation: 126587
Per the URI RFC (see section 2.4), non-US-ASCII characters aren't valid in a URI. You must encode them.
Upvotes: 0
Reputation:
I think it is related to the "UTF-8" charset. Have a look at this topic to learn more and also this chinese in java
Upvotes: 0