<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      JAVA的網絡技巧與編程 二 (轉)--建議放入精華區 ? 因我等新手愛看

      [摘要]五、顯示網絡上其他HTML文檔利用Java提供的getAppletContext().showDocument(URL)可以顯示其他結點的HTML文檔,同前面的顯示網絡上其他結點的圖象,有兩種格式,...
      五、顯示網絡上其他HTML文檔



      利用Java提供的getAppletContext().showDocument(URL)可以

      顯示其他結點的HTML文檔,同前面的顯示網絡上其他結點的圖象,

      有兩種格式,下面各舉一例:

      ●程序8 格式一

      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");



      }

      }



      ●程序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. 利用URL類的openStream(),獲得對應的InputStream類的對象

      如:

      InputStream filecon = fileur.openStream();

      3. 將InputStream對象轉化為DataInputStream類的對象

      如:

      DataInputStream filedata = new DataInputStream(filecon);

      4. 讀取內容

      如對前面的filedata,可用filedata.readLine() 一行一行讀

      取內容,或用filedata.readchar一個字符一個字符讀取內容。 對

      讀取到的內容,可由Java Applet進行各種處理, 并將處理結果用

      各種方式顯示出來。

      下面的例子是讀取 http://www.shu.edu.cn/~xyx/doc/manhua.html

      文件內容的例子,為簡潔起見,該例中只將文件的內容逐行讀出,

      并在文本區顯示出來。

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

      }

       }

      }



      七、動態使用網絡上資源

      在前面介紹的例子的基礎上,可以動態地利用網絡上的資源。

      其方法是編制一個線程,每隔一定時間自動到相應結點讀取最新的

      內容。本文對線程的編制不再展開,讀者可參考有關文章或直接套

      用下面的例子。

      例如對上例中讀取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 文件的內容。



      九、創建URL對象的方法



      在前面的例子中我們統一使用new URL(url字符串)的形式創建

      URL對象。其實,Java提供了四種創建URL對象的形式:

      1.new URL(url字符串)本文中的程序均采用此種格式,如:

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

      2.new URL(協議,主機名,文件名或路徑)如程序2中的

      Stringurl = "http://www.shu.edu.cn/~xyx/img/shnet.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,文件名或路徑)



      十、實現網絡功能的其他方法



      以上著重介紹了利用Java的URL類實現從網絡上獲取聲音、 圖

      象、HTML文檔及文件數據的編程方法。Java的網絡功能很強大,除

      上面介紹的外,還可以利用URLconnection 類實現更廣泛的網絡功

      能,如向WWW 服務器上的 CGI 程序發送信息等; 通過 Socket 及

      ServerSocket類,可以自己編寫客戶軟件及服務軟件,并可以自己

      設計通訊協議。



      〖參考文獻〗

      Laura Lemay,Charles L. Perkins"Teach Yourself JAVA in 21 Days"

      摘自《Internet世界》


      日韩精品一区二区三区高清