Documentation

The Java™ Tutorials
Hide TOC
Working with URLs使用URL
Trail: Custom Networking

Lesson: Working with URLs课程:使用URL

URL is the acronym for Uniform Resource Locator. It is a reference (an address) to a resource on the Internet. URL是一致性资源定位器的首字母缩写。它是对互联网上资源的引用(地址)。You provide URLs to your favorite Web browser so that it can locate files on the Internet in the same way that you provide addresses on letters so that the post office can locate your correspondents.你提供你最喜欢的网络浏览器的URL,这样它就可以在互联网上找到文件,就像你在信件上提供地址那样,邮局就可以找到你的通讯员。

Java programs that interact with the Internet also may use URLs to find the resources on the Internet they wish to access. 与Internet交互的Java程序也可以使用URL来查找他们希望访问的Internet上的资源。Java programs can use a class called URL in the java.net package to represent a URL address.Java程序可以在java.net包中使用名为URL的类来表示URL地址。


Terminology Note:术语说明: 

The term URL can be ambiguous. URL这个词可能不明确。It can refer to an Internet address or a URL object in a Java program. 它可以在Java程序中引用Internet地址或URL对象。Where the meaning of URL needs to be specific, this text uses "URL address" to mean an Internet address and "URL object" to refer to an instance of the URL class in a program.当URL的含义需要明确时,本文使用“URL地址”表示互联网地址,“URL对象”表示程序中URL类的实例。


What Is a URL?什么是URL?

A URL takes the form of a string that describes how to find a resource on the Internet. URL采用字符串的形式,描述如何在Internet上查找资源。URLs have two main components: the protocol needed to access the resource and the location of the resource.URL有两个主要组件:访问资源所需的协议和资源的位置。

Creating a URL创建URL

Within your Java programs, you can create a URL object that represents a URL address. 在Java程序中,可以创建表示URL地址的URL对象。The URL object always refers to an absolute URL but can be constructed from an absolute URL, a relative URL, or from URL components.URL对象总是指绝对URL,但可以从绝对URL、相对URL或URL组件构造。

Parsing a URL解析URL

Gone are the days of parsing a URL to find out the host name, filename, and other information. 解析URL以查找主机名、文件名和其他信息的日子一去不复返了。With a valid URL object you can call any of its accessor methods to get all of that information from the URL without doing any string parsing!使用有效的URL对象,您可以调用它的任何访问器方法,从URL获取所有信息,而无需进行任何字符串解析!

Reading Directly from a URL直接从URL读取

This section shows how your Java programs can read from a URL using the openStream() method.本节介绍Java程序如何使用openStream()方法从URL读取内容。

Connecting to a URL连接到URL

If you want to do more than just read from a URL, you can connect to it by calling openConnection() on the URL. 如果您不只是想读取URL,还可以通过调用URL上的openConnection()连接到它。The openConnection() method returns a URLConnection object that you can use for more general communications with the URL, such as reading from it, writing to it, or querying it for content and other information.openConnection()方法返回一个URLConnection对象,您可以使用该对象与URL进行更一般的通信,例如从中读取、写入或查询内容和其他信息。

Reading from and Writing to a URLConnection从URLConnection读取和向URLConnection写入数据

Some URLs, such as many that are connected to cgi-bin scripts, allow you to (or even require you to) write information to the URL. 一些URL,例如许多连接到cgi bin脚本的URL,允许您(甚至要求您)将信息写入URL。For example, a search script may require detailed query data to be written to the URL before the search can be performed. 例如,搜索脚本可能需要在执行搜索之前将详细的查询数据写入URL。This section shows you how to write to a URL and how to get results back.本节介绍如何写入URL以及如何返回结果。


Previous page: Previous Lesson
Next page: What Is a URL?