含有中文字符的无效 URI (Java)
在设置 URL 中包含中文字符的 URL 连接时遇到问题。它适用于拉丁字符:
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() ;
getInputStream() 调用会产生:
java.lang.IllegalArgumentException: Invalid uri 'http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=???????????': Invalid query
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该问题是由于
URI.toURL()
不对非 ASCII 字符进行百分比编码而引起的。请改用以下内容:The problem is caused by the fact that
URI.toURL()
doesn't percent-encode non-ASCII characters. Use the following instead:上面 axtavt 的回答让我免于疯狂,谢谢!只有一条评论(我不知道如何在答案下面进行评论:)
如果您以 URL 开头,则需要在构建 URI 之前对引号进行编码:
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:
我认为这与“UTF-8”字符集有关。查看此主题以了解更多信息和还有这个 java 中的中文
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
根据 URI RFC(请参阅第 2.4 节),非 US-ASCII 字符不是在 URI 中有效。您必须对它们进行编码。
Per the URI RFC (see section 2.4), non-US-ASCII characters aren't valid in a URI. You must encode them.
下载带有特殊字符的图像 URL。下面的代码片段展示了如何从带有特殊字符的 URL 中读取图像属性。
将 和 替换为实际 IP 地址,将端口号替换为您的实际端口号。
如果您正在使用域名,则可以将 : 替换为域名。
替换<文件路径>与您的文件路径。
<前><代码>尝试{:<端口>/<文件路径>/" + 文件名);
字符串文件名 = "pexels-martin-péchy-1866149%20(1).jpg";
URI uri = new URI("http://
URL url = 新 URL(uri.toASCIIString());
BufferedImage 图像 = ImageIO.read(url);
int height = image.getHeight();
int 宽度 = image.getWidth();
System.out.println("图片下载成功!:" + url.toString()+" height: "+height);
} catch (异常 e) {
e.printStackTrace();
}
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.