包含拉丁字符的URL引发FileNotFoundException异常

3

我在加载包含拉丁字符如:č、ć、š、đ、ž的图片时遇到了问题。代码可以无误地处理其他链接,但当它遇到这样或任何包含拉丁字符的链接时:

InputStream input = null;
            try {
                URL url = new URL(http://www.novosti.rs/upload/thumbs/images/2012//09/28j/Supruga%20Gorana%20Savića_75x45.jpg);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.connect();
                input = conn.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                conn.disconnect();
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

它会抛出一个:

java.io.FileNotFoundException: http://www.novosti.rs/upload/thumbs/images/2012//09/28j/Supruga%20Gorana%20Savića_75x45.jpg

虽然您可以尝试将链接复制到地址栏中以查看其是否有效。那么我能做什么呢?

1个回答

2
尝试使用URLEncoder.encode(String s, String charsetName)方法,它会将特殊字符转换为以%分隔的数字形式。
请参阅URLEncoder文档

1
谢谢,这有助于研究编码问题,现在我已经解决了。 - Filip V.
2
事实上,即使您没有使用奇特的字符,我仍然强烈建议对参数进行URL编码。这可以防止各种错误。 - Joe Plante

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接