当前位置: 首页 > 图灵资讯 > 技术篇> JAVA的网络功能与编程 二

JAVA的网络功能与编程 二

来源:图灵教育
时间:2024-02-28 17:23:57

  使用Java提供的getapletContext()显示网络上其他HTML文档.showDocument(URL)HTML文档可以显示其他结点,与之前显示网络上其他结点的图像相同,有两种格式,以下是一个例子:

  ●一import程序8格式 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");}}}

  ●程序9 格式二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(new URL("http://www.shu.edu.cn/~xyx/doc/manhua.html"));} catch (MalformedURLException e) {System.out.println("Can't open the URL ");}}}上述网络功能仅显示或播放网络上结点的图像, 声音和HTML文档,内容没有得到处理。事实上,Java还可以读取网络文件的内容并处理其内容。在网上阅读文件内容的步骤如下:1. 创建URL类型的对象,如: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. 使用URLopenStream(),InputStream对应的对象如下:InputStream filecon = fileur.openStream();3. 将InputStream对象转化为DatainputStream对象,如:DataInputStream filedata = new DataInputStream(filecon);4. 阅读内容如前面的filedata,可使用filedatata.readLine() 一行一行读取内容,或使用filedatata.readchar一个字符一个字符读取内容。 Java可以阅读所读内容 Applet进行各种处理, 并以各种方式显示处理结果。 Java可以阅读所读内容 Applet进行各种处理, 并以各种方式显示处理结果。下面的例子是阅读 http://www.shu.edu.cn/~xyx/doc/manhua.为了简单起见,html文件内容的例子,本例中只逐行读取文件内容,并显示在文本区域。

  ●10import程序 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 gettext",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());}}}七、在前面介绍的例子的基础上动态利用网络资源,网络上的资源可以动态利用。方法是编制线程,每隔一段时间自动读取相应结点的最新内容。方法是编制一个线程,每隔一段时间自动到相应的结点读取最新内容。本文不再开始编制线程,读者可以参考相关文章或直接应用以下例子。例如,在上例中读取httpp://www.shu.edu.cn/~xyx/doc/manhua.添加线程后,html文件内容的例子如下所示。例子每5秒更新一次数据。若http://www.shu.edu.cn/~xyx/doc/manhua.html存储一些快速变化的信息,如股市,并且有程序随时动态更新其内容,所以在web中添加这个Java Applet,流观者可以获得动态信息。此外,数据也可以在程序中处理,处理结果可以图形显示。例如,将每个时刻的数据绘制成曲线,观众可以看到动态变化的曲线。//程序11import java.io.*;import java.net.*;import java.awt.*;import java.applet.*;public class dynashow extends java.applet.Appletimplements 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可以作为以下编程:

  ●11import程序11 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 文件的内容。在前面的例子中,我们统一使用newl对象的创建方法 URL以url字符串的形式创建url对象。事实上,Java提供了四种形式来创建URL对象:1.new URL(url字符串)本文所有程序均采用此格式,如:newURL("http://www.shu.edu.cn/~xyx/doc/manhua.html")2.new URL(协议,主机名,程序2中的Stringurl,如文件名或路径 = "https://www.tulingxueyuan.cn/d/file/p/20240228/cuko3wz1smp.jpg";image = getImage(new URL(url));部分可以改为:image = getImage(new URL("http","www.shu.edu.cn","/~xyx /img/shnet.jpg"));3.new URL(协议、主机名、端口号、文件名或路径)1如:new URL("http","www.shu.edu.cn",80, "/~xyx/doc/manhua.html")4.new URL(基准url,以上重点介绍了使用JavaURL类实现从网络中获取声音的方法, 编程图像、HTML文档和文件数据的方法。Java的网络功能非常强大。除上述介绍外,URLconection还可以使用 类实现更广泛的网络功能,如向WWWW 服务器上的 CGI 程序发送信息等; 通过 Socket Serversocket可以自己编写客户软件和服务软件,并设计自己的通信协议。