Java Network Knowledge

Source: Internet
Author: User
Tags file transfer protocol

 

(1) Load APPLE (small application) from the network)
Currently, most browsers support Java in the same way. In HTML, there is a special mark <APPLE> on any location of the .html file. This is a reference to a small application. A small application is not restricted by a region. It can be local or anywhere on the Internet. Its location is transparent to users. For a browser, the location is read in <APPLE>, then the browser locates the small application, and runs it. If a small application is located on a remote computer, the browser must load it before it can run.
(2) load images from URLs
First, you must first introduce the URL. It indicates an Uniform Resource Locator, which is the address of some resources on the network. Java programs can use a URL to reference or connect to network resources. For example, to remotely load a graph, the Java program must first create a URL to contain the image address. URL is a high-level interaction function in Java programs.
Ii. URL and usage
A URL represents a common resource locator. Provide the URL to the Web browser so that it can search for and locate files on the Internet. When writing Java programs, you can also use URLs to search for resources on the Internet. The class group java.net contains a class named URL, which Java can use to represent a URL address. The difference here is that the URL address represents an Internet address, and the URL object refers to an instance of URL-like in a Java program.
(1) URL Definition
A URL is an abbreviation used to indicate the Uniform Resource Locator. It is a reference to Internet resources (an address ). A URL not only indicates the name of a file on the network, but also directs to other resources on the network, such as database query and command output. For example: http://java.sun.com // All URLs have two main components:
(1) protocol identifier;
(2) Resource Name.
In the above example, http is the protocol identifier, and // java.sun.com/is the Resource Name. The Protocol identifier indicates the name of the protocol used to obtain the resource. In this example, Hypertext Transfer Protocol (HTTP) is used in hypertext transfer service. HTTP is only one of the multiple protocols used to access different types of resources on the Internet. Other protocols include file transfer protocol (FTP), gopher, file, and news ). The Resource Name is the complete address of the resource. The format of the Resource Name is completely dependent on the protocol used, but for most formats, the resource name includes one or more of the following components:
(1) Host Name: name of the machine where the resource is located.
(2) file name: the path of the file on the machine.
(3) port number: The connected port number (usually optional ).
(4) Reference: a reference to an internal resource named anchor. It often refers to a specific location in a file (generally optional ). In most cases, the host name and file name are required, while the port number and reference are optional. In the above example, java.sun.com is the host name, and the first slash '/' After the colon is short for the file/index.html. When building any URL, you should put the Protocol identifier first, followed by the colon ':', and then the Resource Name, namely: protocolID: resourceName contains a class URL in the class group java.net, which is used by Java programs to represent a URL address. A Java program can construct a URL object, open a connection to it, and then read and write in the connection.
3. Create a URL
In Java programs, you can create a URL object to represent a URL address. A URL object always points to an absolute URL, but it can be constructed by an absolute URL, a relative URL, or a URL component. The simplest way to create a URL object is to create a string in the "readable" format that represents the URL address. In Java, you can use the string of the preceding text to create a URL object: URL gamelan = new URL (http://www.linuxaid.com.cn/) The URL object created in this way represents an absolute URL. An absolute URL contains all the information required to reach the resource. In addition, you can create a URL object from a relative URL address.
(1) Relative URL
A relative URL only contains sufficient information for the resources of another URL (or in another URL context.
In HTML files, relative URLs are often used to specify the resource location. Required
To be written:
<A href000000002.html "> My Story </a>
<A href000000003.html "> Picture of My Pets </a>
These URLs are relative URLs, that is, these URLs are relative to the 1.html file containing these URLs.
In Java, you can specify a relative URL to create a URL object. For example, assume that the program has created a URL object for http://www.linuxaid.com.cn/and the name of a file on the site has been notified.
As linuxaid.animation.html, you can create a URL for the file on the linuxaid site by simply specifying the file name in the context of the initial linuxaid URL:
URL linuxaid = new URL (http://www.javaworld.com /);
URL linuxaidanimation = new url(linuxaid,”aid.animation.html ");
The above Code uses another form of construction process like URL, which allows you to create an object by a URL object (Basic URL) and a relative URL. This construction process can also be used to create a URL object for a famous connection (reference) in a file. For example, the hypothetical file linuxlinuxaid.animation.html contains a famous connection BOTTOM, which is located at the BOTTOM of the file, you can use the relative URL construction process to create a URL object (in the following statement, the character # Before BOTTOM is required): URL linuxaidAnimationBottom = new URL (linuxaid, "# BOTTOM ");
The URL construction process is generally in the following format:
URL (URL baseURL, String relativeURL)
The first parameter is a URL object, specifying the base address of the new URL, and the second parameter is a String object, which is a description of the Resource Name relative to the base address. If baseURL is null, this construction process treats relativeURL as an absolute URL. However, if relativeURL is specified as an absolute URL, baseURL is ignored during the construction process.
(2) Other URL Construction Processes
In addition to the construction process of the class URL, the class URL also provides two other construction processes. When you use a URL, such as an http url, its resource name contains the host name, file name, port number, reference, and other components. These construction processes are very useful. If you do not know the String object that contains the complete URL, It is very convenient to use the two construction processes as long as you know the different components of the URL. If you need to design a network program that allows users to select the protocol, host name, port number, and file name with the mouse, these components can construct a URL. The class URL provides the process of creating a URL using the Protocol, host name, and file name. URL: URL linuxaid = new URL ("http", "www.linuxaid.com.cn", "/linuxaid.animation.html"); this statement is equivalent to URL linuxaid = URL (http://www.linuxaid.com.cn/linuxaid.animation.html. The first parameter is the protocol, the second parameter is the host name, and the last parameter is the path name of the file. Note: The file name starts with a slash '/', indicating that the file name is relative to the root directory of the host. Another construction process of the class URL also adds the port number in the parameter list, for example:
URL linuxaid = new URL ("http", www.linuxaid.com, 80, "/linuxaid.animation.html ");
Among them, 80 is the port number, and other parameters are the same as before. This statement creates the following URL object:
Http://www.linunaid.com.cn: 80/linunxaid.animation.html
If you use these procedures to create a URL object, you can use the URL process toString () or toExternalForm () to obtain a String object containing the complete URL address.
(2) MalformedURLException exception
In the two versions of the URL-like construction process, when the parameters passed to them point to an empty or unknown protocol, they all throw a MalformedURLException exception. If you want to capture and handle this exception, you only need to put the URL construction process statement in a try/catch. For example:
Try {
URL url = new URL (....);
} Catch (MalformedURLException e ){
// Exception Handling Code
}
4. Analyze a URL
URL-like provides some URL object query procedures. You can use these to obtain the protocol, host name, port number, and file name contained in a valid URL object. The following describes the functions of these processes:

(1) getProtocol () returns the Protocol identifier component of the URL.
(2) getHost (), returns the host name component of the URL.
(3) getPort (): return the port number component of the URL. If the port is not set,-1 is returned;
(4) getFile (), returns the URL file name component.
(5) getRef () returns the reference component of the URL.
Note that not all URL addresses contain these components. URLs of the class provide these procedures because HTTP URLs contain these components and may be the most commonly used URLs. The class URL is HTTP-centered in a program. You can use the getXXX () process to obtain URL Information, which is irrelevant to the construction process of the created URL object.
The following is an example program ParseURL, which creates a URL object from a string of a specified URL and analyzes the URL using the URL object access process;
Import java.net .*;
Class ParseURL {
Public static void main (String [] args ){
URL url = null;
Try {
Url = new URL (http://www.ncsa.uiuc.edu: 8080/demoweb/urlprimer.html # INSTALL );
System. out. println ("protocol =" + url. getProtocol ());
System. out. println ("host =" + url. getHost ());
System. out. println ("filename =" + url. getFile ());
System. ou

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.