Encoding URL with German Characters

2019-08-22 03:58发布

In my app I receive a URL such as

http://www.wassersportlotse.de/php/lib/smart_image_resizer/image.php/Mühlendammschleuse.jpg?image=/media/images/uploads/Mühlendammschleuse.jpg

When there are no German characters in the fullurl I can just use it without encoding and it works fine. However if I receive a URL such as the one above it doesn't work (the ü is causing the problem). Below I have tried to encode the seperate parts of the URI to no avail. As alway advice is very much appreciated.

public ImageDownloader(String fullurl) throws URISyntaxException{
    URI uri = new URI(fullurl);

    path = uri.getPath();
    path = URLEncoder.encode(path);

    query = uri.getQuery();
    query = URLEncoder.encode(query);

    auth = uri.getAuthority();

    url = "http://" + auth + path + query;


}

5条回答
我只想做你的唯一
2楼-- · 2019-08-22 04:12

I recently had a problem with URLs for images whose names included umlauts and German special characters, and I lost a day looking for the solution. The images simply did not appear if there was an ä or and ü in the file name or the directory name. I thought it might be spring, or some other Java technology I am working with, or in the browser. And strangely enough, even with the url encoded, it failed to find the image. But in the end, the solution was in my tomcat server.xml configuration. In your server.xml file, find your connector and add these two lines:

URIEncoding="UTF-8"
useBodyEncodingForURI="true"

At the end, it should look something like this:

<Connector connectionTimeout="20000"
port="8080" 
protocol="HTTP/1.1" 
redirectPort="8443"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"/>

Now I do not need to url-encode the url. This is a help to my clients, because they can see the German words in the urls spelled correctly.

Here is another tip: if you are coding in eclipse and starting and stopping your server from inside eclipse, then the configuration file (server.xml) could be in your eclipse workspace in the Servers folder. It must be changed here for it to work with eclipse. This can be maddening, when you have made the change in your principal tomcat configuration, and the urls work there, but they are still broken when running the server in eclipse.

That did it for me. I hope it helps someone out there! :-)

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-22 04:15

Maybe the encoder das encode the Umlaut as UTF-8 characters (so ü would be encoded with two characters) and they are not put back together properly at the server (for us it didn't work with Tomcat). To solve this situation we used URLEncoder.encode(param, "ISO-8859-1") to encode the parameters.

查看更多
\"骚年 ilove
4楼-- · 2019-08-22 04:15

Have your tried unsing:

android.net.Uri.encode(urlString, ":/");

It encodes the string but skips ":" and "/".

查看更多
smile是对你的礼貌
5楼-- · 2019-08-22 04:19

There's no simple answer, because it depends on the server serving that URI which encoding is expected.

Usually it's UTF-8.

In that case: use String.getBytes, specifying the UTF-8 encoding, and obtain a byte array from that. Re-encode that byte array as string by taking all bytes <= 127 as-is, and substituting all others by the %hh form. (percent sign, then two hex digits). See http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.2.1.

查看更多
小情绪 Triste *
6楼-- · 2019-08-22 04:21

You can use Android's Uri class to help you out. That class has an encode() method which will use UTF-8 to encode your string.

查看更多
登录 后发表回答