Java's network functions and programming a

Source: Internet
Author: User
Tags format object ftp functions net network function string ftp protocol
Programming | Network function and programming of network Java
Xu Xianxiao
(25#, Shanghai University Computing Center)

Summary: The Java language is the most popular programming language on the Internet, and this article

Java network functions, to Java from the network to obtain images, sound,

The HTML document and the text file and so on the programming method has made the preliminary introduction, with

This paper introduces the method of dynamically acquiring resources on network. In this paper

Provides a wide range of simple and understandable examples.

Key words: Java;internet network



The Java language is a new programming language on the Internet, with Java features and base

This method of programming has been introduced in a number of articles. But the vast majority of Java enthusiasts would prefer

To learn more about Java programming method, this article on the Java network functions and programming methods

To make a preliminary introduction.

In order to facilitate the first contact with Java readers, this article first on Java programming some common sense

For a brief introduction.



Introduction to Java Programming



1. Programming Environment:



For most readers, the following configuration is a more economical option:

Operating system Win95

Compiling software JDK1.01

Browse software Netscape2.0 above (32-bit)



2. Programming Method:



Enter Java programs with a text editor such as edit, Notepad, and so on. Java

FileName suffix disk.

Then execute the command line: "Javac filename" to compile the Java program. Build after compilation

A byte-code file with a. class suffix.

Finally, if it is a Java applitcation, execute the command line: "Java Word

Section code file name to run the Java program.

If it is a Java applet, the Java applet is called with a text editor input

HTML document with. htm for the filename suffix. To execute the command line again:

"Appletviewer HTML filename" to run the Java applet. or with Netscape

Open the HTML document.



3. Notes on the procedure in this article



In order for the program to be the most concise embodiment of the programming method it represents, the process in this article

The order is generally in the simplest form, omitting the thread and other contents. Therefore, the procedure in this article does not

is a "good" program, but easiest for beginners to understand.

After compiling all the programs in this article, the generated bytecode files and corresponding HTML documents

Uploaded to Http://www.shu.edu.cn/~xyx/test/jvnet, all correct

Run. Readers connected to the Internet can open the address in a browser to see how it works.

Readers connected to the Internet can also enter and compile the program on the local hard drive,

Open the HTML document with Netscape's File/open File menu, experience programming methods and

View the run effect. If the reader wants to put Java applets on their host or other

On the FTP server, called in Netscape using the HTTP protocol or FTP protocol, for security

Sexual restrictions should be modified as follows:

If the reader has an account number on a WWW host, you can make a personal homepage (generally

To create a WWW or public_html directory in the user's root directory, the homepage address is

http://HostName/~ personal account), you can use this procedure in the corresponding

The http://www.shu.edu.cn/~xyx/part is modified to the reader's own web node address,

The compiled bytecode file and corresponding HTML document are then uploaded to their node.

If the reader's computer is connected to the Internet, you can also find an uploaded

FTP nodes, such as: Ftp://ftp.shnet.edu.cn/incoming, will be in this program

The corresponding http://www.shu.edu.cn/~xyx/part is modified to the address of the FTP node.

Upload the compiled bytecode file and the corresponding HTML document to the node to check

Look at the running effect.

If the reader's computer is not networked, it can also be run on a stand-alone Web service soft

Pieces such as WebSTAR for Win95, the corresponding HTTP in this program: www. Shu

The. edu.cn/~xyx/section is modified to the form "http://local IP address" to simulate

Network programming.



Java network features and general steps to get resources on the network



Java programs can get nodes on the network image, sound, HTML documents and text

Resources and to deal with the resources available. For example, a Java program can have every

Set time to read the latest data provided by a node and display it as a chart.

In programming, Mr. General becomes an object of a URL type and then uses Java

Gets the resource represented by the object in the corresponding method in the The following are described separately

Several examples of Java network functionality, and thus introduce several different programming methods.



Third, obtain the image from the network



Java applets can get images directly from nodes on the network and display them. For

To understand how the programming method differs from the programming of local display images, let's not 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 one of the simplest examples of capturing and displaying images, in this case, the first

GetImage (Getdocumentbase (), image file name) from the location of the HTML document

The image is test.gif, and this generates an object image of the image type, and then uses the

DrawImage (image, 0, 0,this) shows the image on the screen.

If you want to get images from other nodes on the network, the key is to create the corresponding

Other nodes are objects of the image type, once the object of the image type is obtained,

You can perform any possible image manipulation on it.

Java provides the following ways to create an image that corresponds to another node:

GetImage (new URL (String))

There are two ways to use the format:



String url = "Node 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 ("Node url");

}

catch (Malformedurlexception e) {

System.out.println ("Can ' t Open the URL");

}

Image=getimage (Imgur);



The previous format generates a URL object with a new URL (URL) and acts directly as a

GetImage, the latter format generates a URL using the new URL (URL) first

object, and then passed to GetImage. The two formats are essentially the same. In both formats,

The parts of the generated URL object are included in the



try{

Get URL Object

}

catch (Malformedurlexception e) {

Error prompt

}

In

For example, to invoke a http://www.shu.edu.cn/~xyx/img/shnet.jpg node.

Image, the first form of the 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 form 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 (Malformedurlexception e) {



System.out.println ("Can ' t Open the URL");

}

Image=getimage (Imgur);

}

public void Paint (Graphics g) {

G.drawimage (image, 0, 0,this);

}

}



Save the above two programs in Imag.java and Imag2. Java, and execute

Javac Imag.java and Javac Imag2.java will be compiled and generated imag

. class and Imag2.class, and finally create the HTML that calls the two Java applets

Files, such as the imag.class corresponding HTML document can be as follows:

< HTML >

< head >

< title >example </title >


< Center >

< applet Code=imag.class width=550 height=250 >

</applet >




Save the HTML document in test.html file, open with Netscape, if you

Computer is connected to the Internet, you can see what the Java Applet displays from the Web

The image obtained on the envelope. (for the other different Java applets in this article, the corresponding

HTML document as long as the corresponding "Code=imag.class" can be modified. )



Iv. getting sound from the Internet



Java has two types of programming methods for getting sound files from a network and playing sounds.

Is the use of Java provided play (URL) and plays (url,string) direct Playback Network

The sound file on the other is via getAudioClip (URL) or getAudioClip

(url,string) Get the sound file from the network first and generate the AudioClip type

object, and then manipulate the object.

The use of the former format is:



String Audur = "Node url";

try {

Play (New URL (Audur));

catch (Exception e) {}



Or



String Audur = "Node url";

try {

Play (New URL (Audur), sound filename);

catch (Exception e) {}

The format used by the latter is:

String Audur = "Node url";

AudioClip Loopclip;

try {

Loopclip = getAudioClip (new URL (Audur));

}

catch (Exception e) {

System.out.println ("Can ' t Open the URL");

}



Or



String Audur = "Node url";

AudioClip Loopclip;

try {

Loopclip = getAudioClip (new URL (Audur), sound filename);

}

catch (Exception e) {

System.out.println ("Can ' t Open the URL");

}



All four of the above formats are to generate the URL object part--"New URL (URL)"

As a direct parameter to play or getaudioclip; As with the previous image processing example,

You can also use the new URL (URL) to get a URL object, and then pass to play or

getAudioClip. For the first play (URL) format, you can also use the following series

Process 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 is a simple example of the above four formats for getting and playing sound files from the network.

For programming purposes:

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) {}

}

}



Program 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 Audur = "http://www.shu.edu.cn/~xyx/java/Animator/audio/";

try {

Play (New URL (Audur), "bark.au");

catch (Exception e) {}

}

}



Program 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 (Malformedurlexception e) {

System.out.println ("Can ' t Open the URL");

}

Loopclip = getAudioClip (Auur, "bark.au");



}



public void Paint (Graphics g) {

Loopclip.loop ();

}

}





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.