Get download file name from URL or HttpUrlConnection?

南笙酒味 提交于 2019-12-25 07:37:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13435940/get-download-file-name-from-url-or-httpurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!