kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財(cái)富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          在Applet中讀取,寫入文件內(nèi)容

          Posted on 2008-03-15 00:51 kooyee 閱讀(1042) 評論(1)  編輯  收藏 所屬分類: Swing/Applet
           

          ---- 我們知道,在Java Applet中出于安全性考慮,Applet是不允許對文件進(jìn)行操作的,不僅不允許寫文件,而且不允許讀文件。盡管我們在編制Applet時(shí)即使使用了文件操作的語句Java不會(huì)報(bào)錯(cuò),在開發(fā)工具(如Cafe)中調(diào)試時(shí)也能夠正常運(yùn)行,但當(dāng)我們在瀏覽器中運(yùn)行這個(gè)Applet時(shí)瀏覽器就會(huì)報(bào)錯(cuò)。但有時(shí)我們的確要讀取文件中的內(nèi)容,比如要將服務(wù)器中的.txt文件內(nèi)容在Applet中顯示出來,是不是就沒有辦法了呢?


          ---- 不!有辦法。決竅就是我們不要將這些服務(wù)器上的文件作為普通文件來處理,而是將它們作為網(wǎng)絡(luò)資源來獲取它們的內(nèi)容。在Java中可用于獲取網(wǎng)絡(luò)資源的類主要有兩種,一是URL類,另一個(gè)是URLConnection類。兩個(gè)類都提供了以字節(jié)流的方式讀取資源信息的方法,而且可以對資源信息的類型作出判斷,以便作相應(yīng)的處理。不同之處是URLConnection類可提供的信息比URL類要多得多,它除了可以獲取資源數(shù)據(jù)外,還可以提供資源長度、資源發(fā)送時(shí)間、資源最新更新時(shí)間、資源編碼、資源的標(biāo)題等許多信息。


          ---- 以下是兩個(gè)類的常用方法。


          URL類:

          · URL(String, String, int, String)

          構(gòu)造方法,創(chuàng)建一個(gè)包含協(xié)議類型、主機(jī)名、

          端口號(hào)和路徑的URL對象

          · URL(String, String, String)

          構(gòu)造方法,創(chuàng)建一個(gè)包含協(xié)議類型、主機(jī)名和路徑

          的URL對象,其中端口號(hào)為缺省值

          · URL(String)

          構(gòu)造方法,創(chuàng)建一個(gè)URL對象,參數(shù)將協(xié)議

          、主機(jī)名、端口號(hào)和路徑組合起來

          · URL(URL,String)

          構(gòu)造方法,根據(jù)給定URL對象與相對路徑創(chuàng)建一個(gè)新的URL對象

          · Object getContent( )

          檢索URL內(nèi)容信息,并返回給對象

          · InputStream openStream( )

          從資源處返回一個(gè)輸入流

          · URLConnection openConnection( )

          生成一個(gè)URLConnection對象


          URLConnection類:

          · protected URLConnection(URL)

          構(gòu)造方法,創(chuàng)建一個(gè)針對指定URL對象的URLConnection類

          · Object getContent( )

          返回URL對象所對應(yīng)的內(nèi)容

          · InputStream getInputStream( )

          獲取從對象中讀取的字節(jié)流

          · Protected static String guessContentTypeFromStream(InputStream is)

          根據(jù)輸入流猜測內(nèi)容的類型


          ---- 下面以讀取服務(wù)器上的.txt文件內(nèi)容為例說明如何在Applet中讀取文件。設(shè)服務(wù)器的IP地址為202.114.1.16,.txt文件的路徑為/file/sample.txt。以下是讀取sample.txt內(nèi)容的Applet的源代碼。


          //getfile.html

           

          < HTML >
          < HEAD >
          < TITLE >讀取文件的Applet< /TITLE >
          < /HEAD >
          < BODY >

          這是服務(wù)器上TXT文件的內(nèi)容
          < BR >

          < Applet code="getFile.class" width=200 height=100 >

          < /Applet >
          < /BODY >

          < /HTML >

           


          //getFile.java

          import java.awt.*;
          import java.applet.*;
          import java.net.*;
          import java.io.*;

          public class getFile extends Applet
          {
          String info;

          public void init()
          {
          URL url;
          URLConnection urlc;

          resize(
          200,100);
          setBackground(Color.white);

          try{

          url 
          = new URL("http://202.114.1.16/file/sample.txt");
          urlc 
          = url.openConnection();
          urlc.connect();

          info 
          = getInfo(urlc);
          }
          catch(MalformedURLException mfe){
          System.out.println(
          "URL form error!");
          }
          catch(IOException ioe){
          System.out.println(
          "IO Exception!");
          }

          }


          public void paint(Graphics g)
          {
          g.setColor(Color.red);
          g.drawString(info,
          50,50);
          }


          public String getInfo(URLConnection urlc)
          {
          String txt 
          = new String();
          InputStream is;
          int i;

          try{
          is 
          = urlc.getInputStream();
          = is.read();
          while(i != -1){
          txt 
          = txt + (char)i;
          = is.read();
          }


          is.close();

          }
          catch(IOException ioe){
          System.out.println(
          "IO Exception!");
          txt 
          = new String("File read failed!");
          }


          return txt;
          }

          }

           


          以上JAVA程序在兩種系統(tǒng)中調(diào)試均通過,兩種系統(tǒng)的配置分別為:

          (1) 服務(wù)器:Digital Unix + Oracle Webserver3.0

          瀏覽器:Netscape4.0.5或IE4.0

          (2) 服務(wù)器:Windows98 + Pws

          瀏覽器:Netscape4.0.5或IE4.0



          用bufferedreader的方法
          //create url to the file on server in applet class
                      URL url = new URL(this.getCodeBase(),"filename");
                      URLConnection urlc 
          = url.openConnection();
                      
                      BufferedReader in 
          = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
                      String line
          =null;
                      
          if( (line = in.readLine()) != null ) 
                      
          {            
                          System.out.printv(line);            
                      }

                      in.close();

          寫入
          //create url to the file on server
                  URL url = new URL(config.getCodeBase(),"servlet/jsp name");
                  URLConnection urlc 
          = url.openConnection();

                      urlc.setDoOutput(true);

                  PrintStream stream = new PrintStream( urlc.getOutputStream() );
                  stream.println("param name="+
          "something write to file");

                      BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream()));注意這里要接收jsp/servlet的response, 否則它不運(yùn)行

          在服務(wù)器端用servlet/jsp得到request,然后對其進(jìn)行處理(save to a file or其他).注意這里要接收jsp/servlet的response, 否則它不運(yùn)行
          // 根據(jù)時(shí)間得文件名
          Calendar calendar = Calendar.getInstance();
          String fileame 
          = String.valueOf(calendar.getTimeInMillis()) +".html";
          fileame 
          = request.getRealPath("/")+fileame;//生成的html文件保存路徑
          FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件輸出流
          byte tag_bytes[] = templateContent.getBytes();
          fileoutputstream.write(tag_bytes);
          fileoutputstream.close();

          讀取server端properties文件
                      URL url = new URL(this.getCodeBase(),"filename.properties");
                      URLConnection urlc 
          = url.openConnection();
                      
                          props.load(urlc.getInputStream());
                      String prop1 
          = props.getProperty("prop1");            
                          String prop2 
          = props.getProperty("prop2");

          評論

          # re: 在Applet中讀取,寫入文件內(nèi)容  回復(fù)  更多評論   

          2008-03-20 22:14 by AIJUN
          哇塞 這個(gè)你都懂啊,我研究了多年,被你一篇文章就點(diǎn)醒,你就是我的偶像了.好崇拜你哦~~~~
          主站蜘蛛池模板: 陇西县| 广宁县| 习水县| 贡嘎县| 汶川县| 长顺县| 富顺县| 崇礼县| 图们市| 若尔盖县| 阿尔山市| 陈巴尔虎旗| 息烽县| 通江县| 日土县| 精河县| 顺义区| 唐山市| 碌曲县| 江达县| 云梦县| 商水县| 绥化市| 凤山市| 四川省| 蓬莱市| 定州市| 云南省| 清远市| 鹤岗市| 墨竹工卡县| 新干县| 中宁县| 阜宁县| 兴文县| 徐水县| 巴彦县| 金湖县| 汨罗市| 荔浦县| 陆良县|