Java(安卓) IOException : Authority expected at index 7: http://

标签 java android file download ioexception

我试图让这个函数从互联网上下载文件。我向它传递了两个参数:从 - 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

最佳答案

尝试使用 URLEncoder 对您的网址进行编码, 在 UTF-8格式。

示例代码:

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

关于Java(安卓) IOException : Authority expected at index 7: http://,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7066460/

相关文章:

android - SpanGroupIndex 和 SpanIndex 回收器 View

java - 获取值并将其显示在 EditText 上

android - ListView 动画单项

bash - 在 awk 中在文件中搜索时使用变量作为搜索模式时面临的问题

java - 适用于 JUnit 的便捷 Java 图像查看器

java - 使用 javax.json 从列表创建 json

java - 避免 catch 子句中实际/逻辑上无法访问的代码

java - 如何在评级栏中设置 float 类型x.5?

java - 通过 Java 实时检测新创建的文件

javascript - 外部 JavaScript 文件运行两次