在下载文件之前从 URL 解析文件名 [英] Parse file name from URL before downloading the file

查看:23
本文介绍了在下载文件之前从 URL 解析文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 URL 下载 ePub 文件.

I'm downloading an ePub file from a URL.

现在我想实现一种机制,如果用户尝试重新下载同一个文件,他应该收到警告/错误消息,并且该文件不应该再次下载.

Now I want to implement a mechanism by which if user tries to re-download the same file, he should get warning/error message and that file should not be downloaded again.

为了实现这一点,我需要检查我的库中存在的文件的名称和用户尝试下载的文件的名称.

To implement this, I need to check the name of the file present in my library with the name of the file user is trying to download.

但我只有这个下载链接,而不是文件名.

But I just have this download link, and not the file name.

如何在下载前获取文件名以便与现有文件进行比较?

How to get the name of the file before download in order to compare it with the existing file?

推荐答案

在 android 中你可以使用 guessFileName() 方法:

In android you can use the guessFileName() method:

URLUtil.guessFileName(url, null, null)

或者,Java 中的简单解决方案可以是:

Alternatively, a simplistic solution in Java could be:

String fileName = url.substring(url.lastIndexOf('/') + 1);

(假设您的网址格式为:http://xxxxxxxxxxxxx/filename.ext)

(Assuming your url is in the format: http://xxxxxxxxxxxxx/filename.ext)

2018 年 3 月 23 日更新

这个问题的点击率很高,有人评论说我的简单"解决方案不适用于某些网址,所以我觉得有必要改进答案.

This question is getting lots of hits and someone commented my 'simple' solution does not work with certain urls so I felt the need to improve the answer.

如果您想处理更复杂的 url 模式,我在下面提供了示例解决方案.它很快变得非常复杂,我很确定我的解决方案仍然无法处理一些奇怪的情况,但仍然如此:

In case you want to handle more complex url pattern, I provided a sample solution below. It gets pretty complex quite quickly and I'm pretty sure there are some odd cases my solution still can't handle but nevertheless here it goes:

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);
}

这个方法可以处理这些类型的输入:

This method can handle these type of input:

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

You can find the whole source code here: https://ideone.com/uFWxTL

这篇关于在下载文件之前从 URL 解析文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆