Big Data Java Foundation 20th day Job

Source: Internet
Author: User

The first topic: Multi-threaded download, display the download progress bar. (Pause function and breakpoint resume for development). import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.jtextarea;import  javax.swing.jbutton;import javax.swing.jprogressbar;import java.awt.event.mouseadapter;import  java.awt.event.mouseevent;import java.awt.component;import java.net.url;import  java.net.urlconnection;import java.io.file;import java.io.fileinputstream;import  Java.io.fileoutputstream;import java.io.objectinputstream;import java.io.objectoutputstream;import  java.io.bytearrayinputstream;import java.io.inputstream;import java.io.randomaccessfile;import  java.util.list;import java.util.arraylist;import java.util.properties;class download{     private static final long serialversionuid = - 6944831795769317874l;    public static void main (String[] args) {      &Nbsp;  new downloadwindow ();     }}class downloadwindow extends  jframe{private jtextarea url_text;private jtextarea local_text;private jbutton  pause_button;private JProgressBar[] bar_arr;public boolean is_pause =  False;private int every_count = 0;    public downloadwindow () {         ini ();     }    public  void ini () {        this.setsize (600,400);         this.setlocation (600,300);         This.setlayout (null);         jlabel url_label = new  jlabel ("url : ");         url_label.setbounds ( 10,10,100,20);     &nbSp;   this.add (Url_label);        url_text =  New jtextarea ();         url_text.seteditable (True);         url_text.setbounds (100,10,400,20);         this.add (url_text);         jlabel local_label =  new jlabel ("local : ");         local_ Label.setbounds (10,40,100,20);         this.add (Local_label);         local_text = new jtextarea ();         local_text.seteditable (true);         local_ Text.setbounds (100,40,400,20);         this.add (Local_text);          Jbutton down_button = new jbutton ("Download");         down_button.setbounds (200,110,100,50);         down_ Button.addmouselistener (New mouseadapter () {             public void mouseclicked (mouseevent e) {String src_url = url_ Text.gettext (); String local_addr = local_text.gettext ();                 downloadmanager manager = new downloadmanager ( downloadwindow.this,src_url,local_addr,5); Addbar (Manager.getlistinfo ()); Manager.start ();             }        });         this.add (Down_button); Jbutton pause_button = new jbutton ("Stop");p Ause_Button.setbounds (320,110,100,50);p Ause_button.addmouselistener (New mouseadapter () {public void  Mouseclicked (mouseevent e) {downloadwindow.this.is_pause = ! Downloadwindow.this.is_pause;pause_button.settext (downloadwindow.this.is_pause ?  "Start" &NBSP;:   "Stop"); String src_url = url_text.gettext (); String local_addr = local_text.gettext (); if (! DownloadWindow.this.is_pause) {list<downloadinfo> info_list = new arraylist< Downloadinfo> (); Try{objectinputstream ois = new objectinputstream (new  FileInputStream (local_addr +  ". tmp")); list<downloadinfo> list =  (list<downloadinfo>) Ois.readobject ();} catch (Exception ex) {ex.printstacktrace ();} Downloadmanager manager = new downloadmanager (downloadwindow.this,info_list); AddBar ( Manager.getlistinfo ()); Manager.start ();}}); This.add (Pause_button);     &nbsP;   this.setvisible (True);     }public void addbar (List< Downloadinfo> info_list) {bar_arr = new jprogressbar[info_list.size ()];int y =  200;for (Int i=0;i<info_list.size (); i++) {downloadinfo info = info_list.get (i); bar_ Arr[i] = new jprogressbar (); System.out.println ("= = ="  + info.getendpos ()); SYSTEM.OUT.PRINTLN ("---"  + info.getstartpos ()); Bar_arr[i].setmaximum (Info.getendpos ()  -  Info.getstartpos ()  + 1);  bar_arr[i].setbounds (100,y +  (i*25), 400,10); This.add (Bar_arr [i]);} This.repaint ();} Public void updatebar (Int length,int index) {Int new_value = bar_arr[index]. GetValue ()  + length; System.out.println (New_value); Bar_arr[index].setvalue (New_value); if (New_value >= bar_arr[index]. Getmaximum ()) {every_count ++;if (every_count >= bar_arr.length) {FinishbaR ();}}} Public void finishbar () {for (Component comp : bar_arr) {this.remove (comp);} This.repaint ();}} Class downloadmanager{    private downloadwindow window;private string  src_url;private string local_addr;private int count;    private  downloadinfo info;private list<downloadinfo> info_list;public downloadmanager ( Downloadwindow window,list<downloadinfo> info_list) {This.window = window;this.info_ List = info_list;}     public downloadmanager (downloadwindow window,string src_url,string  local_addr,int count) {        this.window =  window;         "http://blog.51cto.com/viewpic.php?refimg=" + this.src_ url = src_url;this.local_addr = local_addr;this.count = count;Preparedownload ();     }public void preparedownload () {info_list = new  ArrayList<DownloadInfo> (Try{url url = new url); Int total_length (Src_url);  = url.openconnection (). Getcontentlength (); Randomaccessfile raf = new randomaccessfile (LOCAL_ADDR, "RW"); Raf.setlength (total_length); Raf.close ();int block = total_length / count;downloadinfo info = null; for (int i = 0;i < count;i++) {info = new downloadinfo (); Info.setindex (i); Info.setsrcurl (Src_url); info.setlocaladdr (LOCAL_ADDR); Info.setstartpos (I * block); if ((count - 1)  == i) {Info.setendpos (total_length - 1);} Else{info.setendpos ((i + 1)  * block - 1);} Info_list.add (info);}} catch (exception e) {e.printstacktrace ();}} Private list<downloadinfo> readlistinfo (file tmp_file) {LiSt<downloadinfo> info_list = new arraylist<downloadinfo> (); try{Properties  prop = new properties ();p rop.load (New fileinputstream (tmp_file)); String tmp_url = prop.getproperty ("url"); Int count = integer.parseint ( Prop.getproperty ("Count"));D ownloadinfo info = null;for (int i=0;i<count;i++) {String  value = prop.getproperty ("Thread.")  + i); String[] arr = value.split (";"); Info = new downloadinfo (); Info.setindex (i); Info.setsrcurl (Tmp_url); Info.setstartpos ( Integer.parseint (Arr[0]); Info.setendpos (Integer.parseint (arr[1])); Info.setamount (Integer.parseint (arr[2))); String path = tmp_file.getabsolutepath ();p ath = path.substring (0,path.lastIndexOf (".")); INFO.SETLOCALADDR (path); Info_list.add (info);} Return info_list;} catch (exception e) {e.printstacktrace ();} Return null;}     public&nbsP;void start () {for (downloadinfo info : info_list) {try{downloadthread s =  New downloadthread (Window,info); S.start (); S.join ();} catch (exception e) {e.printstacktrace ();}} if (window.is_pause) {Try{fileoutputstream fos = new fileoutputstream (local_addr +   ". tmp"); Objectoutputstream oos = new objectoutputstream (FOS); Oos.writeobject (info_ list);} catch (exception e) {e.printstacktrace ();}}     }public list<downloadinfo> getlistinfo () {return info_list;}} class downloadthread extends thread{private downloadwindow window;private  Downloadinfo info;public downloadthread (Downloadwindow window,downloadinfo info) { This.window = window;this.info = info;} Public void run () {Try{url url = new url (Info.getsrcurl ()); Urlconnection connect = url.openconnection (); ConnecT.setrequestproperty ("Range", "bytes="  + info.getstartpos ()  +  "-"  +  Info.getendpos ()); Inputstream in = connect.getinputstream (); Randomaccessfile raf = new randomaccessfile (Info.getlocaladdr (), "RW"); Raf.seek ( Info.getendpos ()); Byte[] buf = new byte[1024];int length = 0;while ((length  = in.read (BUF))  != -1) {System.out.println (length +  "+ +"  +  Info.getindex ()); Raf.write (buf,0,length); Window.updatebar (Length,info.getindex ()); Thread.Sleep (+); if (Window.is_pause) {break;}} Raf.close ();} catch (exception e) {e.printstacktrace ();}}} class downloadinfo{    private string src_url;private string  Local_addr;private int start_pos;private int end_pos;private int index;private  int amount;public downloadinfo () {}    public downloadinfo (String  src_url, String local_addr,int start_pos,int end_pos) {         " http://blog.51cto.com/viewpic.php?refimg= "+ This.src_url = src_url;this.local_addr = local_ addr;this.start_pos = start_pos;this.end_pos = end_pos;    }     public string getsrcurl () {        return  src_url;    }public void setsrcurl (string src_url) {"HTTP// blog.51cto.com/viewpic.php?refimg= "+ This.src_url = src_url;} Public string getlocaladdr () {return local_addr;} PUBLIC&NBSP;VOID&NBSP;SETLOCALADDR (string local_addr) {this.local_addr = local_addr;} Public int getstartpos () {return start_pos;} Public void setstartpos (Int start_pos) {this.start_pos = start_pos;} Public int getendpos () {return end_pos;} Public void seteNdpos (Int end_pos) {this.end_pos = end_pos;} Public int getindex () {return index;} Public void setindex (Int index) {this.index = index;} Public int getamount () {return amount;} Public void setamount (int amount) {this.amount = amount;}}


This article is from the "Forest Sensitive" blog, please be sure to keep this source http://senlinmin.blog.51cto.com/6400386/1793104

Big Data Java Foundation 20th day Job

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.