从URL或HttpUrlConnection获取下载文件名? [英] Get download file name from URL or HttpUrlConnection?

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

问题描述

在Firefox中从以下URL下载文件(Eclipse的win32 zip)时,文件名称为 eclipse-jee-juno-SR1-win32.zip

  http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release /juno/SR1/eclipse-jee-juno-SR1-win32.zip&url=http://download.eclipse.org/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32 .zip& mirror_id = 1 

但是,这个文件名没有在内容处理头,这是获取服务器建议的文件名的标准方法。



这里,eclipse的下载仅仅是一个例子。我看到文件名是URL的一部分,但是有没有其他方法来获取文件名?在这种情况下,我可以使用正则表达式从URL提取文件名,但不能保证是没有Content-disposition标题的每个URL的一部分。



问题:当没有Content-disposition标题时,如何获取下载的文件名?还是更本地化,Firefox如何获得上述名称?


或者是Firefox在这里简单地解析URL,而且我遇到了一个案例,尽管从间接脚本传递的下载文件中提取出文件名,但它完全可以正常工作。

解决方案

Content Disposition是服务器建议文件名的标准方法。在没有内容处置标题的情况下,完全由客户端提供文件名。最常见的选择是采取路径的最后一段。



在没有内容处置标题的情况下,服务器甚至不是真的说url应该被下载到一个文件而不是显示。只是大多数浏览器默认保存为文件,无法显示。


In downloading a file (Eclipse's win32 zip) from the following URL in Firefox, the filename is known to be eclipse-jee-juno-SR1-win32.zip.

http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32.zip&url=http://download.eclipse.org/technology/epp/downloads/release/juno/SR1/eclipse-jee-juno-SR1-win32.zip&mirror_id=1

However, this file name is not specified in the Content-disposition header, which is the standard method of acquiring the server-suggested file name.

Here, eclipse's download is simply an example. I see that the file name is a part of the URL, but is there an alternative method to get the file name? I could use regex to extract the file name from the URL in this case, but it isn't guaranteed to be a part of every URL without a Content-disposition header.

Question: How can the download's file name be acquired when no Content-disposition header is present? Or, more localized, how does Firefox come up with the aforementioned name?

Or is Firefox simply parsing the URL here, and I've come across a case where it simply happens to work despite extracting the file name from an indirect, script-delivered download?

解决方案

Content Disposition is the standard method for the server to suggest a file name. In the absence of a content disposition header, it's entirely up to the client to come up with a file name. The most common option is to take the last segment of the path.

In the absence of a content disposition header, the server isn't even really saying that the url should be downloaded to a file rather than displayed. It's just that most browsers default to saving as a file anything they cannot display.

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

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