Download URL content

  Abstract: URL to download the contents of 

  </ Td> </ tr> <tr> <td width="496" height="35" valign="top" class="ArticleTeitle"> / * 
  * Copyright (c) 2000 David Flanagan All rights reserved. 
  * This code is from the book Java Examples in a Nutshell, 2nd Edition. 
  * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. 
  * You may study, use, and modify it for any non-commercial purpose. 
  * You may distribute it non-commercially as long as you retain this notice. 
  * For a commercial use license, or to purchase the book (recommended), 
  * Visit http://www.davidflanagan.com/javaexamples2. 
  * / 
  Import java.io. *; 
  Import java.net .*; 

  / ** 
  * This simple procedure and the use of URL openStream () method to download 
  * URL content and copy the document or in the background. 
  ** / 
  (Public class GetURL 
  Public static void main (String [] args) ( 
  InputStream in = null; 
  OutputStream out = null; 
  Try ( 
  / / Detection parameters 
  If ((args.length! = 1) & (args.length! = 2)) 
  Throw new IllegalArgumentException ( "Wrong number of args"); 

  / / Set the flow 
  URL url = new URL (args [0]); / / create URL 
  Url.openStream in = () / / open a flow 
  If (args.length == 2) / / output access to the corresponding flow 
  Out = new FileOutputStream (args [1]); 
  Else out = System.out; 

  / / URL copy bytes to the output stream 
  Byte buffer [] = new byte [4096]; 
  Int bytes_read; 
  While ((bytes_read = in.read (buffer))! = -1) 
  Out.write (buffer, 0, bytes_read); 
  ) 
  / / On exceptions, the print error message and usage message. 
  Catch (Exception e) ( 
  System.err.println (e); 
  System.err.println ( "Usage: java GetURL  [   ] "); 
  ) 
  Finally (/ / Close always remember flow. 
  Try (in.close (); out.close ();) catch (Exception e) () 
  ) 
  ) 
  ) 


  Operation: 

  C: \ java> java GetURL http://127.0.0.1:8080/zz3zcwbwebhome/ home.html 

  C: \ java> </ td> <td width="188" valign="top" class="ArticleTeitle"> 
  </ Td> </ tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle"> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply