Java(Android)IOException:在索引7处期望的权限:http://

时间:2011-08-15 14:55:43

标签: java android file download ioexception

我正在尝试从互联网上创建此功能下载文件。我传递了两个参数:from - url到net上的文件,到 - 本地文件路径;打开流时出现问题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;
}

正如您所看到的,它还会打印文件的网址,这很好。您可以将其粘贴到浏览器中,然后打开文件。

任何人都知道是什么导致它和/或如何解决它?

P.S。我有使用权限 - INTERNETWRITE_EXTERNAL_STORAGE

1 个答案:

答案 0 :(得分:3)

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

示例代码:

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