Java的网络功能与编程 3

来源:互联网 发布:淘宝网我喜欢 编辑:程序博客网 时间:2024/05/21 16:12

 七、动态使用网络上资源

  在前面介绍的例子的基础上,可以动态地利用网络上的资源。其方法是编制一个线程,每隔一定时间自动到相应结点读取最新的内容。本文对线程的编制不再展开,读者可参考有关文章或直接套用下面的例子。

  例如对上例中读取http://www.shu.edu.cn/~xyx/doc/manhua.html文件内容的例子,加入线程后如下所示。该例子每隔5秒更新一次数据。如果http://www.shu.edu.cn/~xyx/doc/manhua.html中存放的是一些变化较快的信息如股市行情等,并有程序随时动态地更新其内容,则在Web中加入这种Java Applet,可以让流览者得到动态的信息。进一步,也可以在程序中对数据进行处理,并用图形方式显示处理结果。例如将各时刻的数据绘制成曲线,流览者可以看到动态变化的曲线。

//程序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();
}
}
}

  八、Java网络能力的限制

  出于安全性考虑,在用netscape浏览时,Java Applet 只能和其所在的主机建立连接,因此,前面的程序编译后大部分只能存放在http://www.shu.edu.cn/~xyx对应的主机上。存放到其他主机时需更改程序中的结点地址。否则浏览器将显示安全出错。

  但对显示网络上其他HTML文档没有此限制(如程序8、9),读者可以将程序编译后放到任意WWW服务器或FTP服务器,均可正常运行。

  此外,当浏览器从本地盘打开调用Java Applet的HTML文档时,也不受此限制。因此,本文所有的程序都可存放在本地盘编译,只要用netscape的File/Open File菜单打开,便可正确运行。

  对于另一种Java程序--Java Application,也无此限制,例如对于读取网络上文件内容的程序10,对应的Java Application可作如下编程:

  ●程序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());
}
}
}

  将其以showfile2.java存盘,用javac showfile2.java编译后,只需执行“java showfile2”便可以在屏幕上打印出http://www.shu.edu.cn/~xyx/doc/manhua.html 文件的内容
 七、动态使用网络上资源

  在前面介绍的例子的基础上,可以动态地利用网络上的资源。其方法是编制一个线程,每隔一定时间自动到相应结点读取最新的内容。本文对线程的编制不再展开,读者可参考有关文章或直接套用下面的例子。

  例如对上例中读取http://www.shu.edu.cn/~xyx/doc/manhua.html文件内容的例子,加入线程后如下所示。该例子每隔5秒更新一次数据。如果http://www.shu.edu.cn/~xyx/doc/manhua.html中存放的是一些变化较快的信息如股市行情等,并有程序随时动态地更新其内容,则在Web中加入这种Java Applet,可以让流览者得到动态的信息。进一步,也可以在程序中对数据进行处理,并用图形方式显示处理结果。例如将各时刻的数据绘制成曲线,流览者可以看到动态变化的曲线。

//程序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();
}
}
}

  八、Java网络能力的限制

  出于安全性考虑,在用netscape浏览时,Java Applet 只能和其所在的主机建立连接,因此,前面的程序编译后大部分只能存放在http://www.shu.edu.cn/~xyx对应的主机上。存放到其他主机时需更改程序中的结点地址。否则浏览器将显示安全出错。

  但对显示网络上其他HTML文档没有此限制(如程序8、9),读者可以将程序编译后放到任意WWW服务器或FTP服务器,均可正常运行。

  此外,当浏览器从本地盘打开调用Java Applet的HTML文档时,也不受此限制。因此,本文所有的程序都可存放在本地盘编译,只要用netscape的File/Open File菜单打开,便可正确运行。

  对于另一种Java程序--Java Application,也无此限制,例如对于读取网络上文件内容的程序10,对应的Java Application可作如下编程:

  ●程序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());
}
}
}

  将其以showfile2.java存盘,用javac showfile2.java编译后,只需执行“java showfile2”便可以在屏幕上打印出http://www.shu.edu.cn/~xyx/doc/manhua.html 文件的内容

原创粉丝点击