将 URL 转换为文件名的简单方法
我正在为 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以防万一,通常使用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.
此方法将满足您的要求。它将生成一个代表原始 URL 的名称。您可以像这样调用
generateNameFromUrl(String url)
方法。方法如下:
输入: "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.Method is given below:
Input: "http://www.android.com/images/brand/droid.gif"
Output: "http_www_android_com_images_brand_droid.gif"