java - How do I download a file that is embedded in a web site? -
at moment can download files has type of format:
https://jdbc.postgresql.org/download/postgresql-8.1-415.jdbc2.jar
but how download files aren't visible in url file? e.g skype's url path:
http://www.skype.com/sv/download-skype/skype-for-mac/downloading/
as guys can see, there no way can download file using
filepath.substring(filepath.lastindexof("/") + 1);
so there other ways this? did find file embedded in page using firebug
my question is, can programmatically go through page , access file?
here code works fine downloading
public static void filedownload(string urlfile) throws ioexception { url url = new url(urlfile); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); int responsecode = httpurlconnection.getresponsecode(); if (responsecode == httpurlconnection.http_ok) { string filename = ""; string disposition = httpurlconnection.getheaderfield("content-disposition"); string contenttype = httpurlconnection.getcontenttype(); int contentlength = httpurlconnection.getcontentlength(); if (disposition != null) { int index = disposition.indexof("filename="); if (index > 0) { filename = disposition.substring(index + 10, disposition.length() - 1); } } else { filename = urlfile.substring(urlfile.lastindexof("/") + 1, urlfile.length()); } system.out.println("content-type= " + contenttype); system.out.println("disposition= " + disposition); system.out.println("content-length= " + contentlength); system.out.println("file name= " + filename); inputstream inputstream = httpurlconnection.getinputstream(); string savefilepath = getdesiredpath() + file.separator + filename; fileoutputstream fileoutputstream = new fileoutputstream(savefilepath); int byteread = -1; byte[] buffer = new byte[buffer_size]; while ((byteread = inputstream.read(buffer)) != -1) { fileoutputstream.write(buffer, 0, byteread); } fileoutputstream.close(); inputstream.close(); system.out.println("file downloaded"); } else { system.out.println("no file download. server replied httpcode=" + responsecode); } httpurlconnection.disconnect(); }
it's first time working file management , code taken here.
you can download file if file download link embedded in page.
something in web page html:
. . . <a href="skype.exe">download skype</a> . . .
for downloading page , scanning links may use jsoup
code may this:
document doc = jsoup.connect("http://example.com/").get(); elements anchors = doc.select("a"); // untested code (var anchor of anchors) // ecma 6 (i think) { if (anchor.href.endswith(".exe") { // if href not full url i.e. not starting http:// var downloadlink = url + anchor.href; // download file url } }
Comments
Post a Comment