First question: Simple URL to get resources download import java.net.url;import java.net.urlconnection;import java.io.inputstream; import java.io.fileoutputstream;class downloaddemo{ public static Void main (String[] args) { string src_url = "Http://one.jiangmin.com/jiangmin.zip"; try{ url url = new url (Src_url); URLConnection conn = Url.openconnection (); string type = conn.getcontenttype (); int length = conn.getcontentlength (); inputstreaM in = conn.getinputstream (); fileoutputstream fos = new fileoutputstream ("E:/jiangmin.zip"); byte[] buf = new byte[1024]; int len = 0; while ((Len = in.read (BUF)) != -1) { fos.write ( BUF); } fos.close (); in.close (); }catch (Exception e) { &nBsp; e.printstacktrace (); } }} second question: URL multi-threaded download Import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.jtextarea;import javax.swing.jbutton;import java.awt.event.mouseadapter;import java.awt.event.MouseEvent;import java.net.URL;import java.net.URLConnection;import java.io.inputstream;import java.io.randomaccessfile;import java.util.list;import java.util.arraylist;class download{ private static final long serialversionuid = -6944831795769317874l; public static void Main (String[] args) { new downloadwindow (); }}class DownloadWindow extends JFrame{ private jtextarea url_text; p Rivate jtextarea local_text; 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); 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,200,50); down_button.addmouselistener (New mouseadapter () { public&nbsP;void mouseclicked (mouseevent e) { string src_url = url_text.gettext (); system.out.println (Src_url); string local_ Addr = local_text.gettext (); new downloadmanager (downloadwindow.this,src_url,local_addr,5). Start (); } this.add (Down_button); this.setvisible (True); }}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,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 (Src_url); system.out.println (Src_url); int total_length = url.openconnection (). Getcontentlength (); RandomAccessFile raf = new Randomaccessfile (LOCAL_ADDR, "RW"); Raf.setlength (total_length); raf.close ( ); system.out.println (total_length); system.out.println (count); int block = total_length / count; DownloadInfo info = null; for (int i = 0;i < count;i++) { info = new downloadinfo (); info.setsrcurl (Src_url); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;INFO.SETLOCALADDR (LOCAL_ADDR); if ((count - 1) == i) { info.setendpos (total_length - 1); }else{ info.setstartpos ((i + 1) * block - 1); } info_list.add (Info); } }catch (Exception e) { e.printstacktrace (); } } public void start () { &nbSp; int i = 0; for ( Downloadinfo info : info_list) { system.out.println (i); new downloadthread (Window,info). Start (); i++; } }}class downloadthread extends Thread{ private DownloadWindow window; private downloadinfo info; public downloadthread (DownloadWindow window,downloadinfo info) { this.window = window; this.info = info; } public&nBsp;void run () { try{ system.out.println (Info.getsrcurl ()); 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 len = 0; while (len = in.read (BUF)) != -1) { raf.write (BUF); } 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; 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; &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STRING&NBSP;GETLOCALADDR () { return local_addr; } public 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&nbsP;void setendpos (int end_pos) { this.end_pos = end_pos; }}
Big Data Java Foundation 19th day job