将 URL 转换为文件名的简单方法

发布于 2024-12-29 15:36:46 字数 454 浏览 4 评论 0原文

我正在为 Android 编写一个异步图像下载器,只是想知道,给定一个任意 URL,例如:

http://www.android.com/images/brand/droid.gif< /a>

将唯一 url 转换为文件名的最佳方法是什么。我想过简单地分割 url 并抓取最后一部分,但我希望文件名能够代表整个 URL。我认为的其他替代方案是将所有正斜杠替换为下划线,或者简单地散列整个 URL 并存储它。

如果有人有任何想法,我很乐意听到他们!

谢谢

I'm writing an asynchronous image downloader for Android and was just wondering, given an arbitary URL such as:

http://www.android.com/images/brand/droid.gif

What would be the best way to convert the unique url to a filename. I thought about simply splitting the url and grabbing the last section, but I want the filename to be representative of the whole URL. The other alternatives I thought were replacing all the forward slashes with underscores or simply hashing the whole URL and storing this.

If anyone has any ideas I'd love to hear them!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2025-01-05 15:36:46

以防万一,通常使用MD5哈希。但我建议使用“aquery”库。在库中,您可以简单地异步下载图像并放置它来查看。它还支持简单的磁盘缓存、内存缓存。

In case, usually uses MD5 hash. but I suggest to use 'aquery' library. In library you can simply download Image asynchronous and put it to view. It also support disk cache, memory cache simply.

夏日落 2025-01-05 15:36:46

此方法将满足您的要求。它将生成一个代表原始 URL 的名称。您可以像这样调用 generateNameFromUrl(String url) 方法。

    String url = "http://www.android.com/images/brand/droid.gif";
    String uniqueName = generateNameFromUrl(url));

方法如下:

public static String generateNameFromUrl(String url){

    // Replace useless chareacters with UNDERSCORE
    String uniqueName = url.replace("://", "_").replace(".", "_").replace("/", "_");
    // Replace last UNDERSCORE with a DOT
    uniqueName = uniqueName.substring(0,uniqueName.lastIndexOf('_'))
            +"."+uniqueName.substring(uniqueName.lastIndexOf('_')+1,uniqueName.length());
    return uniqueName;
}

输入: "http://www.android.com/images/brand/droid.gif"
输出:“http_www_android_com_images_brand_droid.gif”

This method will be fulfill your requirements. It will generate a name which will represent original URL. You can call generateNameFromUrl(String url) method like this.

    String url = "http://www.android.com/images/brand/droid.gif";
    String uniqueName = generateNameFromUrl(url));

Method is given below:

public static String generateNameFromUrl(String url){

    // Replace useless chareacters with UNDERSCORE
    String uniqueName = url.replace("://", "_").replace(".", "_").replace("/", "_");
    // Replace last UNDERSCORE with a DOT
    uniqueName = uniqueName.substring(0,uniqueName.lastIndexOf('_'))
            +"."+uniqueName.substring(uniqueName.lastIndexOf('_')+1,uniqueName.length());
    return uniqueName;
}

Input: "http://www.android.com/images/brand/droid.gif"
Output: "http_www_android_com_images_brand_droid.gif"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文