java - 处理带有变音符号和其他特殊字符的 URL

标签 java url encoding https

我有以下网址:

https://mantis.server.company/download/test/0022450-umlauts_öä_üüü_and_special_chars_%&$#.pdf

之前没有办法对字符串进行编码。 我只需要处理这个字符串(我知道它不是一个有效的 URL 字符串)以便可以打开该路径后面的文件。

String url = "https://mantis-daun.server.company/download/test/0022450-umlauts_öä_üüü_and_special_chars_%&$#.pdf";

try {
    url = URLDecoder.decode(url, "UTF-8");
    URL myConnection = new URL(url);
    URLConnection connectMe = myConnection.openConnection();
    // Only for error processing
    HttpURLConnection httpConn = (HttpURLConnection) connectMe;
    InputStream is;
    if (httpConn.getResponseCode() >= 400) {
        is = httpConn.getErrorStream();
    } else {
        is = httpConn.getInputStream();
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null)
        {
            System.out.println("-----" + line);
        }
        rd.close();     
    InputStream in = connectMe.getInputStream();
    BufferedInputStream bin = new BufferedInputStream(in);
    byte[] buffer = new byte[(int)connectMe.getContentLength()];
    int fi = 0;
    while(fi<buffer.length) {
        fi = fi + bin.read(buffer, fi, buffer.length - fi);
    }
    bin.close();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

通过这种方法我得到:

Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "&$"
    at java.net.URLDecoder.decode(URLDecoder.java:173)
    at org.mssql.main.MSSQLAccess.main(MSSQLAccess.java:34)

使用 url = url.replaceAll("%", "%25"); 我得到:

-----<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
-----<html><head>
-----<title>400 Bad Request</title>
-----</head><body>
-----<h1>Bad Request</h1>
-----<p>Your browser sent a request that this server could not understand.<br />
-----</p>
-----<hr>
java.io.IOException: Server returned HTTP response code: 400 for URL: https://mantis-daun.server.company/download/test/0022450-umlauts_öä_üüü_and_special_chars_%&$#.pdf
-----<address>Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny16 with Suhosin-Patch mod_ssl/2.2.9 OpenSSL/0.9.8o Server at mantis-daun.server.company Port 443</address>
-----</body></html>
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    at org.mssql.main.MSSQLAccess.main(MSSQLAccess.java:51)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://mantis-daun.server.company/download/test/0022450-umlauts_öä_üüü_and_special_chars_%&$#.pdf
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
at org.mssql.main.MSSQLAccess.main(MSSQLAccess.java:39)

如果我想在普通浏览器中打开“URL”,我也会收到“400:BAD REQUEST”。

那么,有没有办法处理带有变音符号和特殊字符的字符串,以便它可以用作“URL”?

也许服务器设置也有问题?

最佳答案

好吧,您尝试对 url 进行解码,但您实际上应该对其进行编码以生成您想要的内容。它实际上崩溃了,因为它试图解码 %&$ 这不是有效的十六进制符号...

编码将导致: https%3A%2F%2Fmantis-daun.server.company%2Fdownload%2Ftest%2F0022450-umlauts_%C3%B6%C3%A4_%C3%BC%C3%BC%C3%BC_and_special_chars_%25%26%24%23。 pdf

关于java - 处理带有变音符号和其他特殊字符的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494718/

相关文章:

c# - 检查两个 URL 是否相同

url - Elasticsearch 滚动聚合

python - 将附件添加到电子邮件消息会引发 TypeError : set_text_content() got an unexpected keyword argument 'maintype'

vb.net - 保存带有度数符号和 ASCII 编码的 CSV 文件

java - 使用随机的分秒数格式化日期

java - 如何将从两个表中检索的数据存储到 java/servlet/jsp 中的另一个表中

方法参数列表的Java 8并行流

php - 用于检测链接但不检测图像的正则表达式模式

Java - '\' 未用于转义字符串中的双引号“

json - ASN.1 vs JSON 什么时候适合使用它们?