简体   繁体   English

从URL或HttpUrlConnection获取下载文件名吗?

[英]Get download file name from URL or HttpUrlConnection?

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 . 在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

However, this file name is not specified in the Content-disposition header, which is the standard method of acquiring the server-suggested file name. 但是,此文件名未在Content-disposition标头中指定, Content-disposition标头是获取服务器建议的文件名的标准方法。

Here, eclipse's download is simply an example. 在这里,eclipse的下载只是一个示例。 I see that the file name is a part of the URL, but is there an alternative method to get the file name? 我看到文件名是URL的一部分,但是是否有其他方法来获取文件名? 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. 在这种情况下,我可以使用正则表达式从URL中提取文件名,但是如果没有Content-disposition标头,则不能保证它是每个URL的一部分。

Question: How can the download's file name be acquired when no Content-disposition header is present? 问:如果没有Content-disposition标头,如何获取下载文件的文件名? Or, more localized, how does Firefox come up with the aforementioned name? 或者,更本地化的是,Firefox如何提出上述名称?

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? 还是Firefox只是在这里解析URL,而我遇到了这样一种情况,尽管它是从间接的脚本交付下载中提取文件名的,但它恰好可以工作吗?

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. 在没有内容处理标头的情况下,服务器甚至没有真正说应该将URL下载到文件而不是显示。 It's just that most browsers default to saving as a file anything they cannot display. 只是大多数浏览器默认将它们无法显示的所有内容保存为文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM