1

How can i download file from internet, when it don't has a direct URL? Main question - Where can i take name and extension for file?

For example when URL = "defaultSite.com/topic/9772.png" we can take last part of string and create file "9772.png", where we save stream of bytes.

But from URL like:

http://www.skype.com/go/getskype

https://www.dropbox.com/download?plat=win

we can't take file name, however byte stream we can save with no problem. So how can i download file like that?

upd: MIME can't help, i think, becouse file can have specific (user created) extension, or it return to me application/octet-stream even for .exe file. Besides problem whit file name left.

upd2: Yes, server return headers, but it may be without file name. There from skype example:

conn.getHeaderField 0 : HTTP/1.1 200 OK
conn.getHeaderField 1 : nginx
conn.getHeaderField 2 : application/octet-stream
conn.getHeaderField 3 : 30619240
conn.getHeaderField 4 : Thu, 28 Feb 2013 19:35:47 GMT
conn.getHeaderField 5 : bytes
conn.getHeaderField 6 : max-age=86400
conn.getHeaderField 7 : Thu, 04 Apr 2013 14:52:26 GMT
conn.getHeaderField 8 : keep-alive
conn.getHeaderField 9 : S

I need a reliable way to get name and extension.

4

2 回答 2

2

更重要的是,服务器没有建议文件名。服务器可以添加到下载响应的 HTTP 标头之一是

Content-Disposition: filename=checkimage.jpg

或给出另存为提示

Content-Disposition: attachment; filename=checkimage.jpg

(checkimage.jpg is arbitrary here.)

如果服务器没有提供文件名,那么你需要补一个。而且,如果它不使用附件形式,浏览器将使用 HTTP Header 中也提供的 MIME 类型来决定在哪个应用程序中打开返回的数据。

于 2013-04-04T14:43:23.477 回答
0

http://www.skype.com/go/getskype,我被重定向到http://www.skype.com/go/getskype-macosx-a然后到http://download.skype.com/macosx /Skype_6.3.59.582.dmg

因此,您需要遵循重定向,您使用的 java 库可能会自动执行此操作(但您没有说是哪个),但无论哪种方式,最后一个重定向都将指向确切的 URL。

于 2013-04-04T14:50:36.300 回答