Java-download Tool

Source: Internet
Author: User


JAVA Program

Import Java. AWT. gridlayout; <br/> Import Java. AWT. event. actionevent; <br/> Import Java. AWT. event. actionlistener; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import java.net. httpurlconnection; <br/> Import java.net. URL; <br/> Import javax. swing. jbutton; <br/> Import javax. swing. jframe; <br/> Import javax. swing. jlabel; <br/> Import javax. swing. jpanel; <br/> Import javax. swing. jtextfield; <br/> public class downsomething extends jframe implements actionlistener {</P> <p>/*** <br/> jtextfield field = new jtextfield (20 ); <br/> jlabel label = new jlabel ("Enter the URL of the file:"); <br/> jlabel label2 = new jlabel ("file size :"); <br/> jlabel label4 = new jlabel ("downloaded:"); <br/> jlabel label5 = new jlabel ("0.0% "); <br/> jlabel label3 = new jlabel ("0.0 M"); <br/> jbutton button = new jbutton ("Download "); <br/> jpanel = new jpanel (); <br/> jpanel panel2 = new jpanel (); <br/> Public downsomething () {<br/> panel. add (Label); <br/> panel. add (field); <br/> panel2.add (label2); <br/> panel2.add (label3); <br/> panel2.add (label4 ); <br/> panel2.add (label5); <br/> panel2.add (button); <br/> This. add (panel); <br/> This. add (panel2); <br/> This. setlayout (New gridlayout (2, 1); <br/> button. addactionlistener (this); <br/> This. pack (); <br/> This. show (); <br/>}< br/> **/<br/> jlabel label1 = new jlabel ("downloaded :"); <br/> jlabel label2 = new jlabel ("0.0%"); <br/> jpanel = new jpanel (); <br/> Public downsomething () {<br/> panel. add (label1); <br/> panel. add (label2); <br/> This. add (panel); <br/> This. setlayout (New gridlayout (1, 1); <br/> This. setresizable (false); <br/> This. pack (); <br/> This. show (); <br/>}< br/>/** <br/> * @ Param URL: Path of the file to be downloaded <br/> * @ Param fileurl: Save path of the file <br/> * @ throws ioexception <br/> * @ throws interruptedexception <br/> */<br/> private int filelength; <br/> private double downlength; <br/> void download (string URL, string fileurl) throws ioexception, interruptedexception <br/>{< br/> URL u = new URL (URL); <br/> httpurlconnection urlcon = (httpurlconnection) U. openconnection (); <br/> // obtain the file size based on the response <br/> filelength = urlcon. getcontentlength (); <br/> system. out. println ("file size:" + math. ceil (filelength/1024/1024) + "M"); <br/> // label3.settext ("AAA"); <br/> file = new file (fileurl ); <br/> inputstream in = u. openstream (); <br/> fileoutputstream fou = new fileoutputstream (File); <br/> system. out. println ("downloading... "); <br/> int C = 0; </P> <p> while (C = in. read ()>-1) <br/>{< br/> fileinputstream Fi = new fileinputstream (File); <br/> downlength = math. ceil (Fi. available (); <br/> fou. write (c); <br/> // system. out. println ("downloaded:" + math. ceil (downlength/filelength) * 100) + "%"); <br/> label2.settext (math. ceil (downlength/filelength) * 100) + "%"); <br/> // label5.settext (math. ceil (downlength/filelength) * 100) + "%"); <br/>}< br/> system. out. println ("download completed"); </P> <p >}< br/> Public static void main (string [] ARGs) {<br/> downsomething = new downsomething (); </P> <p> try {<br/> downsomething. download ("http://himusic.org/sample/Sample_M/Track36.mp3", "C: // Documents and Settings // administrator // desktop //" + system. currenttimemillis () + ". MP3 "); <br/>}catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/>} catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/>/** <br/> Public void actionreceivmed (actionevent E) {<br/> string STR = ""; <br/> If (E. getsource () = button) <br/>{< br/> If (field. gettext (). contains (". ") <br/>{< br/> STR = field. gettext (). substring (field. gettext (). lastindexof (". "); <br/>}< br/> try {<br/> This. download (field. gettext (), "C: // Documents and Settings // administrator // desktop //" + system. currenttimemillis () + Str); <br/>}catch (ioexception E1) {<br/> e1.printstacktrace (); <br/>} catch (interruptedexception E1) {<br/> e1.printstacktrace (); <br/>}< br/> **/<br/> Public void actionreceivmed (actionevent E) {<br/> // todo auto-generated method stub </P> <p >}< br/>

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.