[Original] Copy file from the web site
/ ** * @ (#) Copyurl.java * * Copyright 2004 OpenSource Develop Team. All Rights Reserved. * /
// packagepackage com.opensource.URL;
// Import classesimport java.net. *; import java.io. *; import java.util.date; import java.util.StringTokenizer;
/ ** * UTILITY for COPYING FILES from the Internet to local disk * example: 1. java copyURL http://www.patriot.net/USERS/Anil/resume/Resume.gif * * 2. Java CopyURL http: // Www.ibm.com/index.html abcd.html * / public class copyURL {public static void main (string args []) {if (args.length <1) {system.err.println ("Usage: Java CopyURL URL [Localfile] "); system.exit (1);} try {url url = new url (args [0]); system.out.println (" Opening Connection to " args [0] " ... " ); URLConnection urlC = url.openConnection (); // Copy resource to local file, use remote file // if no local file name specified InputStream is = url.openStream (); // Print info about resource System.out.print ("Copying Resource (Type:" URLC.GetContentType ()); DATE Date = new date (urlc.getlastmodified ()); system.out.println (", Modified ON:" Date.Tolocalestring () ") ..."); system.out.flush (); FileOutputStream Fos = NULL;
IF (args.length <2) {string localfile = null; // get online name stringtokenizer st = new stringtokenizer (url.getfile (), "/"); while (st.hasmoretoKens ()) localfile = st.nextToken (); Fos = new fileoutputstream (localfile);} else fos = new fileoutputstream (args [1]); int one, count = 0; while ((OneChar = is.read ())! = -1) {FOS. Write (onecha); count ;}
Is.close (); fos.close (); system.out.println (count "byte (s) copied");} catch (mALFORMEDURLEXCEPTION E) {system.err.println (e.tostring ());} Catch (IOException E) {system.err.println (e.tostring ());}}}