Java(Android) IOException:索引 7 处预期权威:http://

2023-11-30

我正在尝试使此功能从互联网下载文件。我向它传递了两个参数:from - 网络上文件的 url,to - 本地文件路径;它引发的问题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,这很好。您可以将其粘贴到浏览器中,然后它会打开该文件。

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

附:我两个都有使用权限 - INTERNET and WRITE_EXTERNAL_STORAGE


尝试使用以下方式对您的网址进行编码URLEncoder, 在里面UTF-8 format.

示例代码:

String encodedUrl = URLEncoder.encode(url.toString(), "UTF-8"); 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java(Android) IOException:索引 7 处预期权威:http:// 的相关文章

随机推荐