Resumable upload for Java (formerly)

Source: Internet
Author: User

Function: You can download software and unfinished software.
If the software exists, it will be renamed and downloaded without overwriting, so as not to delete files.
The Code is as follows:

Package COM. test. day7.down; import Java. io. file; import Java. io. ioexception; import Java. io. inputstream; import Java. io. randomaccessfile; import java.net. httpurlconnection; import java.net. malformedurlexception; import java.net. URL;/*** @ author Bigbird * @ date 2011-9-10 08:34:57 * @ action */public class download {public static void down (string URL, long NPOs, string savepathandfile) {try {URL url = new URL (URL); httpurlconnection connection = (httpurlconnection) URL. openconnection (); // set the User-Agentconnection.setRequestProperty ("User-Agent", "netfox"); // set the start position of the resumable data transfer. setrequestproperty ("range", "bytes =" + NPOs); // obtain the inputstream input = connection. getinputstream (); randomaccessfile accessfile = new randomaccessfile (savepathandfile, "RW"); // locate the file pointer to the NPOs position accessfile. seek (NPOs); byte [] B = new byte [1024]; int nread; // read the byte stream from the input stream and write it to the file while (nread = input. read (B, 0, 1024)> 0) {(accessfile ). write (B, 0, nread);} connection. disconnect ();} catch (malformedurlexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public static long getremotefilesize (string URL) {long size = 0; try {httpurlconnection httpconnection = (httpurlconnection) (new URL (URL ). openconnection (); size = httpconnection. getcontentlength (); httpconnection. disconnect ();} catch (malformedurlexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} return size;} public static void main (string [] ARGs) {string url = "http://www.videosource.cgogo.com/media/0/16/8678/8678.flv"; string savepath = "E: // test file // "; string filename = URL. substring (URL. lastindexof ("/"); string filename2 = filename; httpurlconnection conn = NULL; try {conn = (httpurlconnection) (new URL (URL ). openconnection ();} catch (exception e) {e. printstacktrace ();} file = new file (savepath + filename2); // obtain the remote file size long filesize = getremotefilesize (URL); system. out. println ("remote file size =" + filesize); int I = 0; If (file. exists () {// first check whether it is complete, complete, change the name, jump out of the loop, incomplete, continue to download long localfilesize = file. length (); system. out. println ("the size of an existing file is:" + localfilesize); If (localfilesize <filesize) {system. out. println ("file resume"); down (URL, localfilesize, savepath + filename);} else {system. out. println ("file exists, download again"); do {I ++; filename = filename2.substring (0, filename2.indexof (". ") +" ("+ I +") "+ filename2.substring (filename2.indexof (". "); file = new file (savepath + filename);} while (file. exists (); try {file. createnewfile (); down (URL, 0, savepath + filename);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} else {// indicates that the file exists. Change the name to try {file. createnewfile (); down (URL, 0, savepath + filename2);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}

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.