我正在尝试使这个功能从互联网下载文件。我向它传递两个参数:从- 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,而且它很好。您可以将其粘贴到浏览器中,然后它将打开文件。
有人知道是什么原因和/或如何修复它吗?
我有两种使用权限- INTERNET和WRITE_EXTERNAL_STORAGE
发布于 2011-08-15 15:16:05
尝试使用URLEncoder以UTF-8格式对您的url进行编码。
样本代码:
String encodedUrl = URLEncoder.encode(url.toString(), "UTF-8"); https://stackoverflow.com/questions/7066460
复制相似问题