Java network function and programming
Xu Yingxiao
(Shanghai University Computing Center 25 #)
Abstract: Java language is the most popular programming language on the Internet, this article is targeted
Java's network features, get images, sounds from Java from the web.
The programming method such as HTML document and text file has made a preliminary introduction, the same
The method of dynamically obtaining resources on the network is introduced. In the article
Provides a large number of concise understandable examples.
Keywords: Java; Internet; network
Java language is an emerging programming language on the Internet, the characteristics of Java, and base
There are many articles that have been described in this programming method. However, the majority of Java enthusiasts more hope
Learn more about Java more step-by-step programming methods, this article on Java's network function and its programming method
A preliminary introduction.
In order to facilitate the first time to contact Java readers, this article first on Java programming some common sense
Simple introduction.
First, Java Programming Introduction
1. Programming environment:
For most readers, the following configuration is a more economical choice:
Operating system WIN95
Compile software JDK1.01
Browse the software Netscape 2.0 or more (32-bit)
2. Programming method:
First enter the Java program with a text editor such as Edit, Notepad, etc., to .java
File name suffix store.
Perform the command line: "Javac file name" to compile the Java program. Compiled
The suffix is a .class's bytecode file.
Finally, if it is Java Applitcation, execute the command line: "Java word
The SPA file name is to run the Java program.
If it is a java applet, enter the java applet with the text editor.
HTML document, with .htm as the file name suffix store. Perform the command line:
"AppletViewer HTML File Name" to run Java Applet. Or use Netscape
Open the HTML document.
3. Description of the procedures in this article
In order to make the program to be the most concise programming method, the procedures in this article
The order generally uses the simplest form, omitted the thread and other content. Therefore, the program of this article is not
It is a "good" program, but it is easier for beginners to understand.
After all the programs of this article, the generated byte code file and the corresponding HTML document
It has been uploaded to http://www.shu.edu.cn/~xyx/test/jvnet, can be correct
run. The reader connected to the Internet can open the address with the browser to view the running effect.
Readers in the Internet can also enter and compile the programs of this article in the local hard drive.
Use the Netscape's File / Open File menu to open the HTML document, experience the programming method and
View the running effect. If the reader wants to put Java Applet on your host or other
On the FTP server, use the HTTP protocol or FTP protocol in Netscape, for security
Sex restrictions should be modified as follows:
If the reader has an account on a WWW host, you can do your personal homepage.
Create a WWW or public_html directory in the user's root directory, HOMEPAGE's address is
HTTP: // Hostname / ~ Personal account), you can correspond to this article
Http://www.shu.edu.cn/~xyx/ Part Modification is the reader's own web node address,
Then load the generated byte code file and the corresponding HTML document to your own node.
If the reader's computer is connected to the Internet, you can also find one can upload.
FTP nodes, such as: ftp: //ftp.shnet.edu.cn/incoming, modify the corresponding http://www.shu.edu.cn/~xyx/ of this article to the FTP node,
Upload the generated byte code file and the corresponding HTML document to this node, check
Look at the operation.
If the reader's computer is not connected, you can run the web service soft on a single machine.
For example, WebStar for Win95, the corresponding http: // www. Shu in this article
. 12 to simulate the form of "http: // local IP address" to "http: // local IP address"
network programming.
Second, Java network function and get the general step of obtaining resources on the network
Java programs can get images, sounds, HTML documents, and text
And other resources and can be processed for the obtained resources. For example, a Java program can be every other
The latest data provided by a node is read, and it is displayed in the form of a chart.
On programming processing, generally become a URL type object, then use Java
The corresponding method (METHOD) obtains the resources represented by the object. The following introduces
Several examples of Java network function, and thereby introduce several different programming methods.
Third, get image from the network
Java applets can get images directly from the network to get images and display it. for
What is the difference between understanding its programming methods and programming from the local display image, let us first consider
Network function, to see an example of a simple image display:
● Program 1
Import java.applet. *;
Import java.awt. *;
Public class imag0 extends applet {
Image image;
Public void init () {
Image = getImage (GetDocumentBase (), "Test.gif");
}
Public void paint (graphics g) {
g.drawImage (image, 0, 0, this);
}
}
This is the simplest acquisition and display image, in this case, first
GetImage (getDocumentbase (), image file name) is adjusted from the HTML document
With image Test.gif, and thereby generate an object image of an image type, then use
DrawImage (image, 0, 0, this) displays the image on the screen.
If you want to get images from other nodes on the network, the key is to create a corresponding network.
Other nodes of Image types, once you get an object of Image type,
Any possible image operation can be performed.
Java provides the following ways to create images corresponding to other nodes:
GetImage (New URL)
It can be used in the format:
String URL = "Non-point URL";
Image image;
Try {
Image = GetImage (New URL (URL));
}
Catch (Exception E) {
System.out.println ("Can't Open THE URL");
}
or
URL IMGUR = NULL;
Image image;
Try {
Imgur = New URL ("Nonpare URL");
}
Catch (Malformedurlexcection E) {
System.out.println ("Can't Open THE URL");
}
Image = getImage (IMGUR);
The previous format generates a URL object with "New URL", and directly as a parameter of GetImage, and then uses "New URL (URL)" to generate a URL.
Object, then pass it to GetImage. The two formats are essentially the same. In both formats,
Part of the generated URL object is included
Try {
Get URL objects
}
Catch (Malformedurlexcection E) {
Error prompt
}
in.
For example, want to call http://www.shu.edu.cn/~xyx/img/shnet.jpg node
Image, the first format complete program is as follows:
● Program 2
Import java.applet. *;
Import java.net. *;
Import java.awt. *;
Public class imag extends applet {
Image image;
Public void init () {
String Url = "http://www.shu.edu.cn/~xyx/img/shnet.jpg";
Try {
Image = GetImage (New URL (URL));
} catch (exception e) {}
}
Public void paint (graphics g) {
g.drawImage (image, 0, 0, this);
}
}
The second format is complete, as follows:
● Program 3
Import java.applet. *;
Import java.net. *;
Import java.awt. *;
Public class imag2 extends applet {
Image image;
URL IMGUR = NULL;
Public void init () {
Try {
Imgur = New URL ("http://www.shu.edu.cn/~xyx/img/shnet.jpg");
}
Catch (Malformedurlexcection E) {
System.out.println ("Can't Open THE URL");
}
Image = getImage (IMGUR);
}
Public void paint (graphics g) {
g.drawImage (image, 0, 0, this);
}
}
The two programs described above are implemented in imag.java and imag2. Java store, execution
Javac imag.java and javac imag2.java will be generated after compiled IMAG
.class and imag2.class, last creation of HTML texts that call these two java applets
The HTML document corresponding to iMag.class can be as follows:
hEAD>
applet>
html>
Use the HTML document into the Test.html file, open with Netscape, if you
The computer can be seen in the Internet, you can see the Java Applet from the network
The image obtained is available. (For other different Java Applets in this article, corresponding
The HTML document is modified to modify the corresponding "code = imag.class". )
Fourth, get a sound from the network
Java gets a sound file from the Internet and playing a sound programming method, one is to use the Play (URL) and Play (URL, String) of Java to play the network directly
The sound file, the other is through GetAudioclip (URL) or GetAudioclip
(URL, STRING) First get the sound file from the network and generate the Audioclip type
Object, then operate the object.
The predecessor's usage format is:
String audur = "Nonpode URL";
Try {
Play (New URL (AUDUR));
} catch (exception e) {}
or
String audur = "Nonpode URL";
Try {
Play (New URL (AUDUR), sound file name);
} catch (exception e) {}
The format used by the latter is:
String audur = "Nonpode URL";
Audioclip loopclip;
Try {
Loopclip = GetAudioclip (New URL (Audur));
}
Catch (Exception E) {
System.out.println ("Can't Open THE URL");
}
or
String audur = "Nonpode URL";
Audioclip loopclip;
Try {
LoopClip = GetAudioclip (New URL (AUDUR), sound file name);
}
Catch (Exception E) {
System.out.println ("Can't Open THE URL");
}
The four formats above will generate the URL object part - "New URL (URL)"
Directly as a parameter of Play or GetAudioClip;
You can also use the "New URL (URL)" to get a URL object, then pass it to the Play or
Getaudioclip. If the format of the first Play (URL) can also be adopted
Cheng format:
URL AUDUR = NULL;
Try {
Audur = New URL ("Node URL");
} catch (exception e) {
System.out.println ("Can't Open THE URL");
}
Play (Audur);
The following four kinds of formats obtained from the network are given a simple example.
For programming:
● Program 4 format one
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public Class Sound1 Extends Applet
{Audioclip loopclip;
Public void paint (graphics g) {
String audur = "http://www.shu.edu.cn/~xyx/java/animator/audio/bark.au";
Try {
Play (New URL (AUDUR));
} catch (exception e) {}
}
}
● Procedure 5 format two
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public Class Sound2 Extends Applet
{Audioclip loopclip;
Public void Paint (Graphics G) {string Aud = "http://www.shu.edu.cn/~xyx/java/animator/audio/";
Try {
Play (New URL (Audur), "Bark.au");
} catch (exception e) {}
}
}
● Procedure 6 format three
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public class sound extends applet {
Audioclip loopclip;
Public void init () {
String audur = "http://www.shu.edu.cn/~xyx/java/animator/audio/bark.au";
Try {
Loopclip = GetAudioclip (New URL (Audur));
} catch (exception e) {}
}
Public void paint (graphics g) {
Loopclip.loop ();
}
}
● Program 7 format four
Import java.applet. *;
Import java.awt. *;
Import java.net. *;
Public Class Sound0 Extends Applet {
Audioclip loopclip;
URL AUUR;
Public void init () {
Try {
Auur = New URL ("http://www.shu.edu.cn/~xyx/java/animator/audio/");
}
Catch (Malformedurlexcection E) {
System.out.println ("Can't Open THE URL");
}
LoopClip = GetAudioclip (auur, "bark.au");
}
Public void paint (graphics g) {
Loopclip.loop ();
}
}