Java network function and programming

zhaozj2021-02-08  302

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:

example </ title></p> <p></ hEAD></p> <p><center></p> <p><Applet Code = Imag.class Width = 550 Height = 250></p> <p></ applet></p> <p></ html></p> <p>Use the HTML document into the Test.html file, open with Netscape, if you</p> <p>The computer can be seen in the Internet, you can see the Java Applet from the network</p> <p>The image obtained is available. (For other different Java Applets in this article, corresponding</p> <p>The HTML document is modified to modify the corresponding "code = imag.class". )</p> <p>Fourth, get a sound from the network</p> <p>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</p> <p>The sound file, the other is through GetAudioclip (URL) or GetAudioclip</p> <p>(URL, STRING) First get the sound file from the network and generate the Audioclip type</p> <p>Object, then operate the object.</p> <p>The predecessor's usage format is:</p> <p>String audur = "Nonpode URL";</p> <p>Try {</p> <p>Play (New URL (AUDUR));</p> <p>} catch (exception e) {}</p> <p>or</p> <p>String audur = "Nonpode URL";</p> <p>Try {</p> <p>Play (New URL (AUDUR), sound file name);</p> <p>} catch (exception e) {}</p> <p>The format used by the latter is:</p> <p>String audur = "Nonpode URL";</p> <p>Audioclip loopclip;</p> <p>Try {</p> <p>Loopclip = GetAudioclip (New URL (Audur));</p> <p>}</p> <p>Catch (Exception E) {</p> <p>System.out.println ("Can't Open THE URL");</p> <p>}</p> <p>or</p> <p>String audur = "Nonpode URL";</p> <p>Audioclip loopclip;</p> <p>Try {</p> <p>LoopClip = GetAudioclip (New URL (AUDUR), sound file name);</p> <p>}</p> <p>Catch (Exception E) {</p> <p>System.out.println ("Can't Open THE URL");</p> <p>}</p> <p>The four formats above will generate the URL object part - "New URL (URL)"</p> <p>Directly as a parameter of Play or GetAudioClip;</p> <p>You can also use the "New URL (URL)" to get a URL object, then pass it to the Play or</p> <p>Getaudioclip. If the format of the first Play (URL) can also be adopted</p> <p>Cheng format:</p> <p>URL AUDUR = NULL;</p> <p>Try {</p> <p>Audur = New URL ("Node URL");</p> <p>} catch (exception e) {</p> <p>System.out.println ("Can't Open THE URL");</p> <p>}</p> <p>Play (Audur);</p> <p>The following four kinds of formats obtained from the network are given a simple example.</p> <p>For programming:</p> <p>● Program 4 format one</p> <p>Import java.applet. *;</p> <p>Import java.awt. *;</p> <p>Import java.net. *;</p> <p>Public Class Sound1 Extends Applet</p> <p>{Audioclip loopclip;</p> <p>Public void paint (graphics g) {</p> <p>String audur = "http://www.shu.edu.cn/~xyx/java/animator/audio/bark.au";</p> <p>Try {</p> <p>Play (New URL (AUDUR));</p> <p>} catch (exception e) {}</p> <p>}</p> <p>}</p> <p>● Procedure 5 format two</p> <p>Import java.applet. *;</p> <p>Import java.awt. *;</p> <p>Import java.net. *;</p> <p>Public Class Sound2 Extends Applet</p> <p>{Audioclip loopclip;</p> <p>Public void Paint (Graphics G) {string Aud = "http://www.shu.edu.cn/~xyx/java/animator/audio/";</p> <p>Try {</p> <p>Play (New URL (Audur), "Bark.au");</p> <p>} catch (exception e) {}</p> <p>}</p> <p>}</p> <p>● Procedure 6 format three</p> <p>Import java.applet. *;</p> <p>Import java.awt. *;</p> <p>Import java.net. *;</p> <p>Public class sound extends applet {</p> <p>Audioclip loopclip;</p> <p>Public void init () {</p> <p>String audur = "http://www.shu.edu.cn/~xyx/java/animator/audio/bark.au";</p> <p>Try {</p> <p>Loopclip = GetAudioclip (New URL (Audur));</p> <p>} catch (exception e) {}</p> <p>}</p> <p>Public void paint (graphics g) {</p> <p>Loopclip.loop ();</p> <p>}</p> <p>}</p> <p>● Program 7 format four</p> <p>Import java.applet. *;</p> <p>Import java.awt. *;</p> <p>Import java.net. *;</p> <p>Public Class Sound0 Extends Applet {</p> <p>Audioclip loopclip;</p> <p>URL AUUR;</p> <p>Public void init () {</p> <p>Try {</p> <p>Auur = New URL ("http://www.shu.edu.cn/~xyx/java/animator/audio/");</p> <p>}</p> <p>Catch (Malformedurlexcection E) {</p> <p>System.out.println ("Can't Open THE URL");</p> <p>}</p> <p>LoopClip = GetAudioclip (auur, "bark.au");</p> <p>}</p> <p>Public void paint (graphics g) {</p> <p>Loopclip.loop ();</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-917.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="917" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'MXF5brjSxMGH5YUULvSWtQeAKTpyWCPBN7zi1C_2BEnrL_2B5kXqRiu56t2WsZwrArLeYs38Opsl52B9zTnXZAzIPA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>