我正在从网址下载一个ePub文件。
现在我想要实现一个机制,如果用户尝试重新下载同一文件,他应该收到警告/错误消息,并且那个文件不应该再次被下载。
为了实现这个机制,我需要检查我的库中的文件名与用户正在尝试下载的文件名是否相同。
但我只有此下载链接,而没有文件名。
如何在下载之前获取文件名以便将其与现有文件进行比较?
URLUtil.guessFileName(url, null, null)
或者,Java中的一个简单解决方案可能是:简化版解决方案:
String fileName = url.substring(url.lastIndexOf('/') + 1);
(假设您的URL格式为:http://xxxxxxxxxxxxx/filename.ext
)
2018年3月23日更新
该问题受到了很多访问量,有人评论说我的“简单”解决方案对某些URL无效,因此我觉得有必要改进答案。
如果您想处理更复杂的URL模式,我提供了一个样例解决方案。它很快就变得相当复杂,我十分确信还有一些奇怪的情况我的解决方案仍然无法处理,但是无论如何,以下是解决方案:
public static String getFileNameFromURL(String url) {
if (url == null) {
return "";
}
try {
URL resource = new URL(url);
String host = resource.getHost();
if (host.length() > 0 && url.endsWith(host)) {
// handle ...example.com
return "";
}
}
catch(MalformedURLException e) {
return "";
}
int startIndex = url.lastIndexOf('/') + 1;
int length = url.length();
// find end index for ?
int lastQMPos = url.lastIndexOf('?');
if (lastQMPos == -1) {
lastQMPos = length;
}
// find end index for #
int lastHashPos = url.lastIndexOf('#');
if (lastHashPos == -1) {
lastHashPos = length;
}
// calculate the end index
int endIndex = Math.min(lastQMPos, lastHashPos);
return url.substring(startIndex, endIndex);
}
这个方法可以处理这些类型的输入:
Input: "null" Output: ""
Input: "" Output: ""
Input: "file:///home/user/test.html" Output: "test.html"
Input: "file:///home/user/test.html?id=902" Output: "test.html"
Input: "file:///home/user/test.html#footer" Output: "test.html"
Input: "http://example.com" Output: ""
Input: "http://www.example.com" Output: ""
Input: "http://www.example.txt" Output: ""
Input: "http://example.com/" Output: ""
Input: "http://example.com/a/b/c/test.html" Output: "test.html"
Input: "http://example.com/a/b/c/test.html?param=value" Output: "test.html"
Input: "http://example.com/a/b/c/test.html#anchor" Output: "test.html"
Input: "http://example.com/a/b/c/test.html#anchor?param=value" Output: "test.html"
您可以在此处找到完整的源代码:https://ideone.com/uFWxTL
URLUtil.guessFileName(url, null, null)
,我认为这是最好的Android方式。保持简单:
/**
* This function will take an URL as input and return the file name.
* <p>Examples :</p>
* <ul>
* <li>http://example.com/a/b/c/test.txt -> test.txt</li>
* <li>http://example.com/ -> an empty string </li>
* <li>http://example.com/test.txt?param=value -> test.txt</li>
* <li>http://example.com/test.txt#anchor -> test.txt</li>
* </ul>
*
* @param url The input URL
* @return The URL file name
*/
public static String getFileNameFromUrl(URL url) {
String urlString = url.getFile();
return urlString.substring(urlString.lastIndexOf('/') + 1).split("\\?")[0].split("#")[0];
}
public static String getFileNameFromUrl(URL url) {
String urlPath = url.getPath();
return urlPath.substring(urlPath.lastIndexOf('/') + 1);
}
在http://developer.android.com/reference/java/net/URL.html#getPath()上查看。
(意思是在该网站上查看)从apache commons-io
的FilenameUtils
获取文件名
URL url = URL(fileUrl)
String fileName = FilenameUtils.getName(url.getPath())
commons-io
依赖项。implementation "commons-io:commons-io:2.6"
http://www.example.com/some/path/to/a/file.xml?foo=bar#test
protected boolean need2Download(String fileName) {
File basePath = new File(BOOK_STORE_PATH);
File fullPath = new File(basePath, fileName);
if (fullPath.exists())
return false;
return true;
}
protected void downloadFile(String url) {
String fileName = url.substring(url.lastIndexOf('/') + 1);
if (need2Download(fileName)) {
// download
}
}
www.bookstore.com
),我从中选择一本书(具有URLwww.bookstore.com/book1.epub
),应该将其下载到我的图书馆。 我如何获取特定书籍的URL,即www.bookstore.com/book1.epub
???webView.getUrl()
除了第一次加载之外根本不会触发。 - GAMA