openConnection()等)
java.net包是Java语言中提供通用网络编程接口的类库。它包含了所有用于创建网络应用程序所需的基本类以及接口,包括支持 TCP/IP 协议的类。这些类的实例支持在网络上传输数据流。
java.net包中提供了许多类和接口,其中最常用的是URL类和URLConnection类,它们为从互联网或本地文件系统读取信息提供了强大的机制。URL类提供了一种用于获取URL资源的便捷方式,并支持HTTP、FTP和其他协议。URLConnection类提供了一种通过网络连接来检索URL资源的高级方法。
URL类:
URL类是打开到指定资源的连接的通用方法。它可以打开Web页面、文件、目录和其它资源。
打开连接的方法是调用URL类的资源访问方法openConnection()。接下来,这个方法会返回URLConnection类的对象,使用它来访问URL资源的数据。
以下是一个使用URL类获取一个网页的示例代码:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetWebPage {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.google.com/");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
URLConnection类:
URLConnection类是打开到URL资源的通用连接。它可以使用HTTP、FTP和其他网络协议来访问资源。使用URLConnection类执行HTTP请求和接收Web服务器的响应非常方便。
以下是一个使用URLConnection类获取一个网页的示例代码:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetWebPage {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.google.com/");
URLConnection conn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
调用URLConnection类的openStream()方法可以直接打开指定的URL,并返回InputStream类型的数据流。InputStream就像一个管子,允许读取远程文件、Web服务器响应等数据。
使用URLConnection类获取文件示例代码:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class GetFile {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.test.com/test.doc");
URLConnection conn = url.openConnection();
BufferedInputStream in = new BufferedInputStream(
conn.getInputStream());
FileOutputStream out = new FileOutputStream("test.doc");
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = in.read(buffer)) != -1)
out.write(buffer, 0, numRead);
in.close();
out.close();
}
}
除了openStream()方法之外,openConnection()方法也可以用来打开一个URL连接。它返回一个URLConnection对象,它提供了很多用于配置和执行请求的方法。
使用openConnection()方法获取Web页面代码:
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetWebPage {
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.google.com/");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
以上代码中的setDoOutput和setUseCaches方法可用于设置URLConnection对象。setDoOutput()方法指示您将要向Web服务器发送数据。setUseCaches()方法禁用缓存,使每次请求都是全新的请求。
总结:
java.net包是Java语言中最重要的一个包之一,提供了通用的网络编程接口。在Java中访问Web页面、文件或者其他资源可以通过URL类和URLConnection类完成。它们可以打开网络连接并获取到数据流,同时也提供了一系列的设置方法用于对URLConnection进行配置,比如设置是否使用缓存、是否要输出等等。
