Java Network Programming-general Linux technology-Linux programming and kernel information. The following is a detailed description.
1. Introduction to Java programming
1. Programming Environment:
Operating System RedHat7.0
Compiling software JDK1.2.2
Browser software Netscape6.0
2. Programming Method:
Use a text editor such as gedit to input a Java program. Java is used to store disks with filename suffixes. Run the "Javac file name" command to compile the Java program. After compilation, the suffix is generated. Class bytecode file. Finally, for Java Applitcation, run the command line "Java bytecode file name" to run the Java program. If it is a Java Applet, enter the HTML document that calls the Java Applet in the text editor and save the file with the suffix. htm. Run the "appletviewer HTML file name" command to run the Java Applet. Or use Netscape to open the HTML document.
3. Uniform Resource Locator (URL ):
URL (String, String, int, String)
Constructor to create a constructor that contains the protocol type, Host Name,
URL object of the port number and Path
URL (String, String, String)
Constructor: Creates a request that contains the protocol type, host name, and path.
URL object, where the port number is the default value
URL (String)
Create a URL object.
Host Name, port number, and Path
URL (URL, String)
Create a new URL object based on the given URL object and relative path.
Object getContent ()
Retrieve URL content and return it to the object
InputStream openStream ()
Returns an input stream from the resource.
URLConnection openConnection ()
Generate a URLConnection object
URLConnection class:
Protected URLConnection (URL)
Create a URLConnection class for the specified URL object
The type of the content to be guessed based on the input stream
2. Java Network functions and general steps for Obtaining Network Resources
Java programs can obtain images, sounds, HTML documents, text, and other resources of nodes on the network. For example, a Java program can read the latest data provided by a node at a certain time and display it as a chart. In programming, it is generally a URL-type object, and then the corresponding method in Java is used to obtain the resources represented by this object. The following describes several examples of Java Network functions.
3. retrieving images from the network
Java Applet can directly obtain and display images from nodes on the network. Now let's look at a simple example of image display:
(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // anywhereyoustart/pic.jpg '); ">
Try {
Image = getImage (new URL (url); // obtain the JPG image from the Web.
} Catch (Exception e ){}
} Public void paint (Graphics g ){
G. drawImage (image, 0, 0, this); // load the specified URL address in the network to the image
}
}
Use imag. run javac imag. java, the imag generated after compilation will be obtained. class, and finally create HTML documents that call the two Java applets, such as imag. the HTML document corresponding to the class can be as follows:
Save the HTML file to the imag.html file and open it with Netscape. If your computer is connected to the Internet, you can see the image displayed by the Java Applet on the network.
4. Obtain sound from the network
Java provides two programming methods for obtaining and playing sound files from the network. One is to use the play (URL) and play (URL, String) provided by Java) directly Play audio files on the network. The other type is to obtain the audio file from the network through getAudioClip (URL) or getAudioClip (URL, String) and generate an AudioClip object, then, perform operations on the object.
5. display other HTML documents and text files on the network
Use getAppletContext () provided by Java (). ShowDocument (URL) displays HTML documents and text files of other nodes, and displays images of other nodes on the network,
Import java. applet .*;
Import java. awt .*;
Import java.net .*;
Public class showdoc extends Applet
{
URL docur = null;
Public void paint (Graphics g ){
Try {
Docur = new URL ("http: // anywhereyoustart/index.html"); // index can be a TXT file.
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.