2

私は次のURLを持っています:

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」として使用できるように、文字列をウムラウトと特殊文字で処理する方法はありますか?

サーバーの設定にも問題があるのでしょうか?

4

2 に答える 2

1

さて、あなたは URL をデコードしようとしますが、実際にはそれをエンコードして、あなたが望むものにする必要があります。有効な 16 進記号ではない %&$ をデコードしようとするため、実際にはクラッシュします...

エンコードの結果: 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

于 2013-03-19T08:40:54.100 に答える
0

まず、Xavjer が指摘したように、URLをエンコードする必要があります。次に、URL を分割し、パスの「テキスト」部分のみをエンコードするのが理にかなっています。ドメイン名はエンコードされません (ラテン語以外のドメイン名を使用している場合は、 Punycode に従ってエンコードする必要があります)。また、パス区切り文字も保持する必要があります (URL 全体をエンコードする場合はそうではありません)。したがって、「ダウンロード」、「テスト」、およびファイル名+拡張子の部分のみをエンコードします

于 2013-03-19T08:45:02.990 に答える