Java's network functions and programming two

Source: Internet
Author: User
Tags add ftp functions getmessage net network function string thread
Programming | Network v. Displaying other HTML documents on the network



Use Java-provided getAppletContext (). showdocument (URL) to

An HTML document that displays other nodes, and an image of the other nodes in the previous display network,

There are two formats, each of which is shown in the following example:

Program 8 Format One

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://www.shu.edu.cn/~xyx/doc/manhua.html");

}

catch (Malformedurlexception e) {

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

}

if (docur!= null) {

getAppletContext (). showdocument (Docur, "_blank");

}

}

}



Program 9 Format Two

Import java.applet.*;

Import java.awt.*;

Import java.net.*;

public class Showdoc2 extends Applet

{

URL docur= null;

public void Paint (Graphics g) {

try {

getAppletContext (). showdocument (The new URL ("http://www.shu.edu.cn/

~xyx/doc/manhua.html "));

}

catch (Malformedurlexception e) {

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

}

}

}



Read the contents of the file on the network



The network function mentioned above is to display or play the image, sound of nodes on the network and

HTML document and does not process its contents. In fact, Java can also read the network

The contents of the file and process its contents.

The steps to read the contents of a file on a network can be as follows:

1. Create an object of a URL type

Such as:

String url = "ftp://202.120.127.218/incoming/test/readtxt.html";

URL fileur;

try {

fileur = new URL (URL); }

catch (Malformedurlexception e) {

System.out.println ("Can ' t get URL:");

}

2. Using the OpenStream () of the URL class to obtain the corresponding InputStream class object

Such as:

InputStream Filecon = Fileur.openstream ();

3. Convert a InputStream object to an object of the DataInputStream class

Such as:

DataInputStream filedata = new DataInputStream (Filecon);

4. Read content

As with the previous filedata, you can read the Filedata.readline () line

Fetch the content, or read the content in one character Filedata.readchar a character. Right

The content that is read can be handled by the Java applet, and the processing results are

Various ways to show it.

The following example is read http://www.shu.edu.cn/~xyx/doc/manhua.html

An example of a file's contents, for simplicity, in which only the contents of the file are read-by-line,

and display it in the text area.

Program 10

Import java.io.*;

Import java.net.*;

Import java.awt.*;

Import java.applet.*;

public class Showfile extends applet{

URL fileur;

TextArea Showarea = new TextArea ("Please wait a while for get

Text ", 10,70);

public void init () {

String url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";

try {fileur = new URL (URL);}

catch (Malformedurlexception e) {

System.out.println ("Can ' t get URL:");

}

Add (Showarea);

}



public void Paint (Graphics g) {

InputStream Filecon = null;

DataInputStream filedata = null;

String Fileline;

try {

Filecon = Fileur.openstream ();

Filedata = new DataInputStream (Filecon);

while ((Fileline = Filedata.readline ())!= null) {

Showarea.appendtext (fileline+ "\ n");

}

}

catch (IOException e) {

System.out.println ("Error in I/o:" + e.getmessage ());

}

}

}



Vii. dynamic use of resources on the network

On the basis of the examples described earlier, resources on the network can be used dynamically.

The method is to compile a thread that automatically reads the latest at the corresponding node at a certain time

Content. In this paper, the thread is no longer developed, the reader can refer to the article or direct set

Use the example below.

For example, read the Http://www.shu.edu.cn/~xyx/doc/manhua in the previous example

An example of the contents of the. html file, after adding the thread, is shown below. This example is updated every 5 seconds

Data at once. If the http://www.shu.edu.cn/~xyx/doc/manhua.html

There are some fast changing information such as the stock market, and there are procedures at any time dynamically

Update its contents, the Java applet is added to the web to allow the browser to get

Dynamic information. Further, the data can also be processed in the program, and the graphic

method to display the processing results. For example, the data for each moment is plotted as a curve, and the browser can

See the curve of the dynamic change.

Program 11

Import java.io.*;

Import java.net.*;

Import java.awt.*;

Import java.applet.*;

public class Dynashow extends Java.applet.Applet

Implements Runnable {

Thread Dthread;

URL fileur;

TextArea Showarea = new TextArea ("Wait for a while ...", 10,70);

public void init () {

String url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";

try {fileur = new URL (URL);}

catch (Malformedurlexception e) {

System.out.println ("Can ' t get URL:");

}

Add (Showarea);

}



public void Start () {

if (Dthread = null)

{

Dthread = new Thread (this);

Dthread.start ();

}

}

public void Stop () {

if (dthread!= null) {

Dthread.stop ();

Dthread = null;

}

}



public void Run () {

InputStream Filecon = null;

DataInputStream filedata = null;

String Fileline;

while (true) {

try {

Filecon = Fileur.openstream ();

Filedata = new DataInputStream (Filecon);

while ((Fileline = Filedata.readline ())!= null) {

Showarea.appendtext (fileline+ "\ n");

}

}

catch (IOException e) {

System.out.println ("Error in I/o:" + e.getmessage ());

}

try{

Dthread.sleep (5000);

}

catch (Interruptedexception e) {}

Repaint ();

}

}

}



VIII. limitations of Java network capabilities



For security reasons, when browsing with Netscape, Java applets can only

The host on which it resides establishes a connection, so most of the previous programs are compiled and stored only

On the http://www.shu.edu.cn/~xyx corresponding host. Storage to other host machines

You need to change the node address in the program. Otherwise, the browser will display a security error.

However, there is no such restriction on displaying other HTML documents on the network (such as programs 8, 9), reading

The program can be compiled and placed on any WWW server or FTP server, and can be shipped normally

Yes.

In addition, when the browser opens the HTML document that invokes the Java applet from the local disk,

is also not subject to this restriction. Therefore, all the programs in this article can be stored on the local disk compilation, only

To be opened with Netscape's File/open File menu, it works correctly.

For another Java program--java application, there is no such restriction, for example

For example, to read the contents of the file on the network program 10, the corresponding Java application can

Programming as follows:



Program 11

Import java.io.*;

Import java.net.*;

Import java.awt.*;

Class Showfile2 {

public static void Main (String args[]) {

InputStream Filecon = null;

DataInputStream filedata = null;

String Fileline;

String url = "http://www.shu.edu.cn/~xyx/doc/manhua.html";

URL fileur;

try {

fileur = new URL (URL);

Filecon = Fileur.openstream ();

Filedata = new DataInputStream (Filecon);

while ((Fileline = Filedata.readline ())!= null) {

System.out.println (fileline+ "\ n");

}

}

catch (IOException e) {

System.out.println ("Error in I/o:" + e.getmessage ());

}

}

}

Save it in Showfile2.java, and compile it with Javac Showfile2.java,

Simply perform "Java Showfile2" to print on the screen

The contents of the http://www.shu.edu.cn/~xyx/doc/manhua.html file.



Ix. ways to create a URL object



In the previous example we used the form of a new URL (URL string) to create

The URL object. In fact, Java provides four kinds of forms to create URL objects:

1.new url (URL string) The procedures in this article are in this format, such as:

New URL ("http://www.shu.edu.cn/~xyx/doc/manhua.html")

2.new URL (protocol, host name, filename, or path), as in program 2

String url = "Http://www.shu.edu.cn/~xyx/img/shnet.jpg";

Image = GetImage (new URL), part can be changed to:

Image = GetImage (new URL ("http", "www.shu.edu.cn", "/~xyx/img/shnet.jpg"));

3.new URL (protocol, host name, port number, filename or path) 1

such as: New URL ("http", "www.shu.edu.cn", "/~xyx/doc/manhua.html")

4.new url (base url, file name, or path)



X. Other ways to realize network function



The above focuses on the use of Java URL Class implementation from the network to obtain sound, graphics

Programming methods for images, HTML documents, and file data. Java's network is powerful, except

As described above, you can also use the URLConnection class to achieve more extensive network work

, such as sending information to a CGI program on the WWW server, etc. through sockets and

ServerSocket class, you can write your own customer software and service software, and can own

Design communication protocols.



Reference Document

Laura lemay,charles L. Perkins "Teach Yourself JAVA in"

An excerpt from the world of the Internet

Related Article

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.