Java Video Downloads

Source: Internet
Author: User
 /** * 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; }
    }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.