【问题标题】:Java(Android) IOException: Authority expected at index 7: http://Java(Android) IOException:索引 7 处的权限:http://
【发布时间】:2011-08-15 14:55:43
【问题描述】:

我正在尝试让这个功能从互联网上下载文件。我向它传递了两个参数:从 - url 到网络上的文件,到 - 本地文件路径;打开流时它抛出IOException的问题:

Authority expected at index 7: http://

这是我的代码:

private boolean downloadFile(String pathFrom, String pathTo)
{
    URL url;
    ReadableByteChannel rbc;
    FileOutputStream fos;

    try {
        url = new URL(pathFrom);
        rbc = Channels.newChannel(url.openStream());
        fos = new FileOutputStream(pathTo);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        return true;
    } catch (MalformedURLException e) {
        Log.e(TAG, "Failed to download file (" + pathFrom + ") because of malformed URL.");
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Failed to download file (" + pathFrom + ") because FileNotFoundException occured when opening output stream: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "Failed to download file (" + pathFrom + ") because IOException occured when transfering file or opening input stream: " + e.getMessage());
    }

    return false;
}

如您所见,它还会打印文件的 url,这很好。您可以将其粘贴到浏览器中,它会打开文件。

有人知道是什么原因造成的和/或如何解决吗?

附:我同时拥有使用权限 - INTERNETWRITE_EXTERNAL_STORAGE

【问题讨论】:

    标签: java android file download ioexception


    【解决方案1】:

    尝试使用URLEncoderUTF-8 格式对您的网址进行编码。

    示例代码:

    String encodedUrl = URLEncoder.encode(url.toString(), "UTF-8"); 
    

    【讨论】:

    • 它弄乱了我的 URL(用 %2F 替换斜杠,用 %3A 替换 ':')并导致恶意 URL 异常。
    • 嗯,没有更多信息,您在索引 7 处的角色是什么?我认为这可能与 URL 本身有关。
    • 你是对的。我一遍又一遍地阅读日志,发现在某些时候 http://... 更改为 http:/... :|它是由 File.getParent() 引起的(用于从 URL 中删除文件名)。必须为此找到一个新功能。
    猜你喜欢
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多