【问题标题】:FileNotFoundException for URL containing latin characters包含拉丁字符的 URL 的 FileNotFoundException
【发布时间】:2012-09-20 14:44:56
【问题描述】:

我在加载包含以下拉丁字符的图片时遇到问题:č、ć、š、đ、ž。 该代码对其他链接完美无缺,但是当它到达这个或任何其他包含拉丁字符时:

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

虽然您可以尝试将链接复制到地址栏以查看它是否有效。 那我该怎么办?

【问题讨论】:

    标签: android url character latin


    【解决方案1】:

    尝试使用URLEncoder.encode(String s, String charsetName),它将特殊字符转换为% 分隔的数字形式。

    请参阅URLEncoder 文档。

    【讨论】:

    • 谢谢,它有助于研究编码,现在我可以使用它了。
    • 事实上,即使你没有外来字符,我还是强烈推荐 URLEncoding 参数。它可以防止各种错误
    猜你喜欢
    • 1970-01-01
    • 2018-06-21
    • 2010-12-27
    • 1970-01-01
    • 2016-05-19
    • 2011-01-27
    • 1970-01-01
    • 2017-12-16
    • 2021-10-25
    相关资源
    最近更新 更多