java - How to get full video download from a link? -
i trying download video link, downloads small part of it, can't watched @ all. how download entire video no matter how large file link?
try { url url; byte[] buf; int byteread, bytewritten = 0; url = new url(faddress); outstream = new bufferedoutputstream(new fileoutputstream(destinationdir + "\\" + localfilename)); conn = url.openconnection(); = conn.getinputstream(); buf = new byte[size]; while ((byteread = is.read(buf)) != -1) { outstream.write(buf, 0, byteread); bytewritten += byteread; } } catch (exception e) { e.printstacktrace(); } { try { is.close(); outstream.close(); } catch (ioexception e) { e.printstacktrace(); } } }
it looks server may redirect other location code doesn't handle. final location can try method (based on: http://www.mkyong.com/java/java-httpurlconnection-follow-redirect-example/):
public static string getfinallocation(string address) throws ioexception{ url url = new url(address); httpurlconnection conn = (httpurlconnection)url.openconnection(); int status = conn.getresponsecode(); if (status != httpurlconnection.http_ok) { if (status == httpurlconnection.http_moved_temp || status == httpurlconnection.http_moved_perm || status == httpurlconnection.http_see_other) { string newlocation = conn.getheaderfield("location"); return getfinallocation(newlocation); } } return address; }
now need change
url = new url(faddress);
to
url = new url(getfinallocation(faddress));
Comments
Post a Comment