1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100292.html;106,k100295.html;107,k100297.html;108,k100298.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100292.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100295.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100297.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100298.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 46 - Netzwerkprogrammierung
</table>
<hr>
<!-- Section -->
<a name="urldatenlesen"></a>
<h2>46.4 Daten mit Hilfe der Klasse URL lesen </h2>
<hr>
<ul>
<li><a href="k100296.html#urldatenlesen">46.4 Daten mit Hilfe der Klasse URL lesen</a>
</ul>
<hr>
<p>
Die Klasse <a name="ixa103404"><a href="index_u.html#ixb102196"><font color=#000080><tt>URL</tt></font></a></a>
wurde bereits in <a href="k100255.html#dieklasseurl">Abschnitt 40.1.1</a>
behandelt. Neben den dort beschriebenen Möglichkeiten besitzt
sie Methoden, um Daten von der Quelle zu lesen, die durch den URL
adressiert wird:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public final InputStream openStream()
throws IOException
public final Object getContent()
throws IOException
public URLConnection openConnection()
throws IOException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/net/URL.html" onClick="this.href=getApiDoc('java.net.URL')"><font color="#660066" size=-1>java.net.URL</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa103405"><a href="index_o.html#ixb102540"><font color=#000080><tt>openStream</tt></font></a></a>
wird ein <a href="index_i.html#ixb100642"><font color=#000080><tt>InputStream</tt></font></a>
geliefert, der wie die Methode <a href="index_g.html#ixb100671"><font color=#000080><tt>getInputStream</tt></font></a>
der Klasse <a href="index_s.html#ixb102527"><font color=#000080><tt>Socket</tt></font></a>
zum Lesen der Quelldaten verwendet werden kann. <a name="ixa103406"><a href="index_g.html#ixb102541"><font color=#000080><tt>getContent</tt></font></a></a>
versucht darüber hinaus, die Daten zu interpretieren. Dazu können
<a name="ixa103407"><i>Content Handler Factories</i></a> registriert
werden, die beispielsweise Text-, Image- oder Archivdateien interpretieren
und ein dazu passendes Java-Objekt liefern. Die Methode <a name="ixa103408"><a href="index_o.html#ixb102543"><font color=#000080><tt>openConnection</tt></font></a></a>
stellt eine Vorstufe von <a href="index_g.html#ixb102541"><font color=#000080><tt>getContent</tt></font></a>
dar. Sie liefert ein Objekt des Typs <a name="ixa103409"><a href="index_u.html#ixb102544"><font color=#000080><tt>URLConnection</tt></font></a></a>,
das eine Abstraktion einer protokollspezifischen Verbindung zwischen
einem Java-Programm und einem URL darstellt.
<p>
Als einfaches Beispiel wollen wir uns das folgende Programm <font color="#000077"><tt>SaveURL</tt></font>
ansehen. Es wird mit einem URL und einer Datei als Argument aufgerufen.
Mit Hilfe der Klasse <a href="index_u.html#ixb102196"><font color=#000080><tt>URL</tt></font></a>
stellt das Programm eine Verbindung zur angegebenen URL her und beschafft
durch Aufruf von <a href="index_o.html#ixb102540"><font color=#000080><tt>openStream</tt></font></a>
einen <a href="index_i.html#ixb100642"><font color=#000080><tt>InputStream</tt></font></a>.
Mit seiner Hilfe wird die Quelle gelesen und das Ergebnis in die als
zweites Argument angegebene Datei geschrieben:
<a name="listingid046008"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* SaveURL.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.net.*;
<font color="#555555">004 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">005 </font>
<font color="#555555">006 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> SaveURL
<font color="#555555">007 </font>{
<font color="#555555">008 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">009 </font> {
<font color="#555555">010 </font> <font color="#0000AA">if</font> (args.length != 2) {
<font color="#555555">011 </font> System.err.println(
<font color="#555555">012 </font> <font color="#0000FF">"Usage: java SaveURL <url> <file>"</font>
<font color="#555555">013 </font> );
<font color="#555555">014 </font> System.exit(1);
<font color="#555555">015 </font> }
<font color="#555555">016 </font> <font color="#0000AA">try</font> {
<font color="#555555">017 </font> URL url = <font color="#0000AA">new</font> URL(args[0]);
<font color="#555555">018 </font> OutputStream out = <font color="#0000AA">new</font> FileOutputStream(args[1]);
<font color="#555555">019 </font> InputStream in = url.openStream();
<font color="#555555">020 </font> <font color="#006699">int</font> len;
<font color="#555555">021 </font> <font color="#006699">byte</font>[] b = <font color="#0000AA">new</font> <font color="#006699">byte</font>[100];
<font color="#555555">022 </font> <font color="#0000AA">while</font> ((len = in.read(b)) != -1) {
<font color="#555555">023 </font> out.write(b, 0, len);
<font color="#555555">024 </font> }
<font color="#555555">025 </font> out.close();
<font color="#555555">026 </font> in.close();
<font color="#555555">027 </font> } <font color="#0000AA">catch</font> (MalformedURLException e) {
<font color="#555555">028 </font> System.err.println(e.toString());
<font color="#555555">029 </font> System.exit(1);
<font color="#555555">030 </font> } <font color="#0000AA">catch</font> (IOException e) {
<font color="#555555">031 </font> System.err.println(e.toString());
<font color="#555555">032 </font> System.exit(1);
<font color="#555555">033 </font> }
<font color="#555555">034 </font> }
<font color="#555555">035 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/SaveURL.java"><font color="#000055" size=-1>SaveURL.java</font></a></td>
</tr>
</table>
<i>
Listing 46.8: Daten von einem URL lesen</i></p>
<p>
Das Programm kann nun leicht verwendet werden, um den Inhalt beliebiger
URLs auf der Festplatte abzuspeichern. Die folgenden beiden Aufrufe
zeigen den Download der Hauptseite des Java-Servers von SUN und das
Laden einer Testseite von unserem in <a href="k100295.html#experimentwebserver">Abschnitt 46.3.3</a>
vorgestellten Web-Server:
<font color="#000077">
<pre>
java SaveURL http://java.sun.com x.html
java SaveURL http://localhost/index.html y.html
</pre>
</font>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100292.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100295.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100297.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100298.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|