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