Java IO stream randomaccessfile multi-threaded download

Source: Internet
Author: User

http://www.verejava.com/?id=16994721196019

Package Com.randomaccessfile;import Java.io.file;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import        Java.io.randomaccessfile;public class testthreaddownload{public static void Main (string[] args) {try            {InputStream is=new FileInputStream (New File ("res/raf/water.jpg"));                        Long contentlength=is.available ();            Create a file with the same size as the file you want to download Randomaccessfile raf=new randomaccessfile ("res/raf/new2.jpg"), "RW";            Raf.setlength (contentlength);            Raf.close ();            Calculate how long earchlength=contentlength/5 to download for each segment;            if (contentlength%5!=0) {earchlength++;            } for (int i=0;i<5;i++) {new Thread (new Downloadthread (Earchlength,i)). Start ();  }} catch (FileNotFoundException e) {e.printstacktrace ();      } catch (IOException e) {e.printstacktrace ();        }} Static Class Downloadthread implements Runnable {private long earchlength;                private int i;            Public Downloadthread (Long earchlength,int i) {this.earchlength=earchlength;        This.i=i;            } @Override public void Run () {Randomaccessfile raf=null;                try {inputstream is=new fileinputstream (New File ("res/raf/water.jpg"));                Long skip=earchlength*i;                Is.skip (skip);                RAF = new Randomaccessfile (New File ("Res/raf/new2.jpg"), "RW");                Raf.seek (skip);                int l=0;                System.out.println (Raf.getfilepointer ()); while ((Raf.getfilepointer () <=skip+earchlength) && ((L=is.read ())!=-1)) {RAF.WR                ITE (l); }} catch (Filenotfoundexception e) {e.printstacktrace ();            } catch (IOException e) {e.printstacktrace ();                } finally {try {raf.close ();                } catch (IOException e) {e.printstacktrace (); }            }                    }            }}

http://www.verejava.com/?id=16994721196019

Java IO stream randomaccessfile multi-threaded download

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.