KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > util > UrlGetter


1 /*
2  * UrlGetter is part of the Cofax content management sytem library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other
19  * related informaion.
20  *
21  * $Header: /cvsroot/cofax/cofax/src/org/cofax/util/UrlGetter.java,v 1.1.2.1 2006/12/11 16:30:00 fxrobin Exp $
22  */

23
24 package org.cofax.util;
25
26 import java.net.*;
27 import java.io.*;
28
29 public class UrlGetter {
30
31     private static URL address;
32
33     private static String JavaDoc localname;
34
35     private static StringBuffer JavaDoc page;
36
37     public static StringBuffer JavaDoc getpage(URL address) {
38
39         StringBuffer JavaDoc textIn = new StringBuffer JavaDoc(16384);
40
41         try {
42             BufferedReader in = new BufferedReader(new InputStreamReader(address.openStream()));
43
44             String JavaDoc inputLine;
45
46             while ((inputLine = in.readLine()) != null)
47                 textIn.append(inputLine);
48             in.close();
49         }
50
51         catch (IOException e) {
52             System.err.println("Caught IOException: " + e.getMessage());
53         }
54
55         return textIn;
56     }
57
58     public static void printpage(StringBuffer JavaDoc contents, String JavaDoc localname) {
59
60         try {
61             FileOutputStream out; // declare a file output object
62
PrintStream p; // declare a print stream object
63
out = new FileOutputStream(localname);
64
65             p = new PrintStream(out); // Connect print stream to the output
66
// stream
67
p.println(contents);
68         }
69
70         catch (FileNotFoundException e) {
71             System.err.println("Caught Exception: " + e.getMessage());
72         }
73
74     }
75
76     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
77
78         address = new URL(args[0]);
79         localname = args[1];
80
81         page = UrlGetter.getpage(address);
82         UrlGetter.printpage(page, localname);
83
84     }
85
86 }
87
Popular Tags