我正在尝试在Webview中下载文件。我使用DownloladListener
来处理下载请求,然后使用DownloadManager
下载文件。一切运作良好,但我不能从这个网址获取文件的名称和扩展名:http://unionpeer.com/dl.php?t=1502119(这是刺客信条电影.torrent文件)。我怎样才能获得至少文件扩展名?提前谢谢!
下载文件:
String url = (String) objects[0];
String defaultDownloadPath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(true).setTitle(getFileName(url))
.setDescription("Download")
.setDestinationInExternalPublicDir(getPublicExternalPath(defaultDownloadPath),
getFileName(url));
dm.enqueue(request);
getfileName() - 比URLUtils.guessFileNameFromUrl()更好一点
String temp = "";
String extension = url.substring(url.lastIndexOf("."));
for (int i = 0; i < extension.length(); i++) {
if (i != 0){
if (!Character.isLetter(extension.charAt(i)) && !Character.isDigit(extension.charAt(i))) {
break;
}
}
temp += extension.charAt(i);
}
return String.valueOf(System.currentTimeMillis() + temp);