Get download file name from URL or HttpUrlConnection? - java

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.

Related

Get default file name from download with HttpUrlConnection

I am trying to figure out how to get the default file name of a file I download using an HttpUrlConnection.
I use a REST API to download the file so I can't parse the URL for the name and the "Content-Disposition" header doesn't contain the name either but when I put the link in my browser it will download the file with the right name so I'm thinking it must be possible to get the name from the result of the HTTP request somehow.
I have read the following two posts which addressed this problem without solving it for my particular situation: HttpURLConnection downloaded file name and
Get download file name from URL or HttpUrlConnection?

Java/Javascript read content-disposition file content

For my web site, I need to get some data from an URL whose response headers contains Content-Disposition attribute which forces me to download the file. I would like to know how I can read the content of the file without downloading the file to disk and do I/O to read it.
Doing so in either Java or JavaScript would be fine.
Content-Disposition is just advisory. If you use a non-browser client (Java, curl, wget...) and do a GET request, you can just do whatever you want.
(I guess this means your question isn't sufficiently specific)

Get absolute filepath

I have org.springframework.web.multipart.commons.CommonsMultipartFile object and i need to get absolute file path on client device. Thanks
I need to show picture to user, without saving on server.
You are not allowed to get the full path of client device, maybe you can spy the user's username or OS what he dont like you to know.
Use FileApi from Javascript instead (How can I draw an image from the HTML5 File API on Canvas?).
Most browsers will not include the path information on the client.
For example if you have a CommonsMultipartFile you can call getFileItem() to receive a FileItem and then from the FileItem call getName().
For example:
commonsMultipartFile.getFileItem().getName();
However as the documentation states:
Returns the original filename in the client's filesystem, as provided
by the browser (or other client software). In most cases, this will be
the base file name, without path information. However, some clients,
such as the Opera browser, do include path information.

Download url in to directory

I need a download a text/plain file in to a folder. The url does not end with .txt but it has content-type etc... properly set. When I use the browser it immediately prompts me to save the file. The browser automatically puts proper file name also.
Using java how can i download that url in to a folder? Note that I dont know the filename also but I want the file to be saved in a directory.
code to download a file is easy... my question is that I dont know by what name should i save my file. the filename is part of content-disposition header, now how do i extract that?
The HTTP protocol uses the HTTP headers to define some information about the data transferred.
You have the content-disposition header that can have a property filename that is generated by the server. This holds the name of the file being transferred. But it is optional. Should you handle the case it is not present. Here is the doc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html
Depending on how you download the file, you'll have dozen of ways to retrieve this file name from the http header.
Give a look to the apache http client for instance.
HIH
M.

How to set content-disposition to inline for static file(s) in java Google App Engine

I know how to set content-disposition header for dynamic requests, but how to set it for static files.
Problem is that GAE java production version automatically sets to attachment content-disposition for static file requests (btw default local development content-disposition is inline), but I need to set it to inline.
Now when somebody tries to open static pdf file browser automatically starts download instead to try to open it in new tab.
Besides mime type and expiration, you don't have much control over how static files are served (see Google app.yaml documentation). If you need fine-grained control, you can store the PDF in the Datastore as a Blob and write a handler for it.
(Not to be confused with the Blobstore API)
All my pdf files are under /pdf/ path. I have created web request for /pdfi/ uri that with url fetch service fetches requested file under /pdf/ path and sets content-disposition to inline.
Now where ever (static html, etc.) there was link for any pdf file I have replaced /pdf/ with /pdfi/ and everything works. When and if GAE team allow us to set content-disposition for static files I will simply change every link from /pdfi/ to /pdf/

Categories

Resources