Get filename for friendly URL in Java

How to find out the file name behind the URL (without parsing the URL and its //). For instance:

If you are accessing this URL using a browser:

http://www.ubuntu.com/start-download?distro=desktop&bits=32&release=lts

it goes into "ubuntu-12.04-desktop-i386.iso" . How can I get this file name in java to load a file with its original name?

thank

+3
source share
2 answers

More efficient way:

You can simply connect to the URL and then get the URL you are redirected to by doing the following:

URL foo = new URL("http://www.ubuntu.com/start-download?distro=desktop&bits=32&release=lts");
HttpURLConnection fooConnection = (HttpURLConnection) foo.openConnection();
URL secondFoo = new URL(fooConnection.getHeaderField("Location"));
fooConnection.setInstanceFollowRedirects(false);
URLConnection fooURL = secondFoo.openConnection();
+3
source

HEAD, ; ( HTTP-, Perl) :

$ HEAD -S 'http://www.ubuntu.com/start-download?distro=desktop&bits=32&release=lts'
HEAD http://www.ubuntu.com/start-download?distro=desktop&bits=32&release=lts
302 Moved Temporarily
HEAD http://ubuntu.virginmedia.com/releases//precise/ubuntu-12.04-desktop-i386.iso
200 OK
Connection: close
Date: Tue, 01 May 2012 22:54:56 GMT
Accept-Ranges: bytes
ETag: "1f83678-2bd4b000-c175d600"
Server: Apache
Content-Length: 735358976
Content-Type: application/octet-stream
Last-Modified: Mon, 23 Apr 2012 12:27:04 GMT
Client-Date: Tue, 01 May 2012 22:54:54 GMT
Client-Peer: 194.117.143.72:80
Client-Response-Num: 1

302 , , URL- . , HTTP, . , - , . , .

+3

All Articles