含有中文字符的无效 URI (Java)

发布于 2024-10-14 22:58:27 字数 536 浏览 9 评论 0原文

在设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

合久必婚 2024-10-21 22:58:27

该问题是由于 URI.toURL() 不对非 ASCII 字符进行百分比编码而引起的。请改用以下内容:

URL url = new URL(uri.toASCIIString());  

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());  
温柔嚣张 2024-10-21 22:58:27

上面 axtavt 的回答让我免于疯狂,谢谢!只有一条评论(我不知道如何在答案下面进行评论:)

如果您以 URL 开头,则需要在构建 URI 之前对引号进行编码:

String s = "your_url?with=\"quotes\"";
URI su = new URI (s.replaceAll("\"", "%22");
URL ur = new URL( su.toASCIIString());

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());
罪#恶を代价 2024-10-21 22:58:27

我认为这与“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

水水月牙 2024-10-21 22:58:27

根据 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.

岛歌少女 2024-10-21 22:58:27

下载带有特殊字符的图像 URL。下面的代码片段展示了如何从带有特殊字符的 URL 中读取图像属性。

  1. 将 和 替换为实际 IP 地址,将端口号替换为您的实际端口号。

  2. 如果您正在使用域名,则可以将 : 替换为域名。

  3. 替换<文件路径>与您的文件路径。

    <前><代码>尝试{
    字符串文件名 = "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.

  1. replace and with actual IP address and port number with your actual port number.

  2. In case If you are working with domain, you can replace : with the domain name.

  3. 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();
     }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文