/** * Download files to local * * * @param urlstring * downloaded file address * @param filename * Local filename * @param Timeout * Timeout time milliseconds * @throws Exception * Various exceptions/public static Boole
An download (string urlstring, String Filename,int timeout) {Boolean ret = false;
File File = new file (filename);
try {if (file.exists ()) {ret = true;
}else{//constructed URL url url = new URL (urlstring);
Open connection HttpURLConnection con = (httpurlconnection) url.openconnection ();
Con.setconnecttimeout (timeout);
Con.setreadtimeout (timeout);
Con.connect ();
int contentlength = Con.getcontentlength ();
Input stream InputStream is = Con.getinputstream ();
1K data buffering byte[] bs = new byte[1024]; Data read fromlength int len;
The file stream of the output files File2=new file (File.getparent ());
File2.mkdirs ();
if (File.isdirectory ()) {}else{file.createnewfile ();//Create File}
OutputStream OS = new FileOutputStream (file);
Start reading while (len = Is.read (BS))!=-1) {os.write (BS, 0, Len);
//finished, close all links os.close ();
Is.close ();
if (ContentLength!= file.length ()) {file.delete ();
ret = false;
}else{ret = true;
A catch (IOException e) {file.delete ());
ret = false; Logger.error ("[videoutil:download]:\n" + "video URL:" + urlstring + "\ n NEW FILENAME:" + filename + "Download failed!!
"); }finally {return ret;
/** * Breakpoint continued * @param urlstring * @param filename * @param timeout * @return * * public static Boolean Resumedownload (String urlstring, String Filename,int timeout) throws exception{Boolean R
ET = false;
File filefinal = new file (filename);
String tmpfilename = filename+ ". tmp";
File File = new file (tmpfilename);
try {if (filefinal.exists ()) {ret = true;
}else{long Contentstart = 0;
File File2=new file (File.getparent ());
if (file.exists ()) {Contentstart = File.length ();
}else{File2.mkdirs ();
}//construct URL url url = new URL (urlstring);
Open connection HttpURLConnection con = (httpurlconnection) url.openconnection (); Con.setconnecttimeout (TimEout);
Con.setreadtimeout (timeout); Sets the dot if (contentstart>0) {con.setrequestproperty ("RANGE", "bytes= +contentstart+")
-");
} con.connect ();
int contentlength = Con.getcontentlength ();
Input stream InputStream is = Con.getinputstream ();
100Kb data buffering byte[] bs = new byte[100*1024];
The length of data read int len;
Randomaccessfile osavedfile = new Randomaccessfile (tmpfilename, "RW");
Osavedfile.seek (Contentstart);
Start reading while (len = Is.read (BS))!=-1) {osavedfile.write (BS, 0, Len);
//finished, close all links osavedfile.close ();
Is.close ();
File.renameto (filefinal);
ret = true; } catch (IOException e) {
File.delete ();
ret = false; Logger.error ("[videoutil:download]:\n" + "video URL:" + urlstring + "\ n NEW FILENAME:" + filename + "Download failed!!
");
throw new Exception (e);
}finally {return ret; }
}