Posted on 2008-03-15 00:51
kooyee 閱讀(1039)
評論(1) 編輯 收藏 所屬分類:
Swing/Applet
---- 我們知道,在Java Applet中出于安全性考慮,Applet是不允許對文件進行操作的,不僅不允許寫文件,而且不允許讀文件。盡管我們在編制Applet時即使使用了文件操作的語句Java不會報錯,在開發工具(如Cafe)中調試時也能夠正常運行,但當我們在瀏覽器中運行這個Applet時瀏覽器就會報錯。但有時我們的確要讀取文件中的內容,比如要將服務器中的.txt文件內容在Applet中顯示出來,是不是就沒有辦法了呢?
---- 不!有辦法。決竅就是我們不要將這些服務器上的文件作為普通文件來處理,而是將它們作為網絡資源來獲取它們的內容。在Java中可用于獲取網絡資源的類主要有兩種,一是URL類,另一個是URLConnection類。兩個類都提供了以字節流的方式讀取資源信息的方法,而且可以對資源信息的類型作出判斷,以便作相應的處理。不同之處是URLConnection類可提供的信息比URL類要多得多,它除了可以獲取資源數據外,還可以提供資源長度、資源發送時間、資源最新更新時間、資源編碼、資源的標題等許多信息。
---- 以下是兩個類的常用方法。
URL類:
· URL(String, String, int, String)
構造方法,創建一個包含協議類型、主機名、
端口號和路徑的URL對象
· URL(String, String, String)
構造方法,創建一個包含協議類型、主機名和路徑
的URL對象,其中端口號為缺省值
· URL(String)
構造方法,創建一個URL對象,參數將協議
、主機名、端口號和路徑組合起來
· URL(URL,String)
構造方法,根據給定URL對象與相對路徑創建一個新的URL對象
· Object getContent( )
檢索URL內容信息,并返回給對象
· InputStream openStream( )
從資源處返回一個輸入流
· URLConnection openConnection( )
生成一個URLConnection對象
URLConnection類:
· protected URLConnection(URL)
構造方法,創建一個針對指定URL對象的URLConnection類
· Object getContent( )
返回URL對象所對應的內容
· InputStream getInputStream( )
獲取從對象中讀取的字節流
· Protected static String guessContentTypeFromStream(InputStream is)
根據輸入流猜測內容的類型
---- 下面以讀取服務器上的.txt文件內容為例說明如何在Applet中讀取文件。設服務器的IP地址為202.114.1.16,.txt文件的路徑為/file/sample.txt。以下是讀取sample.txt內容的Applet的源代碼。
//getfile.html
< HTML >
< HEAD >
< TITLE >讀取文件的Applet< /TITLE >
< /HEAD >
< BODY >

這是服務器上TXT文件的內容< 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();
i = is.read();

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

is.close();


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

return txt;
}
}

以上JAVA程序在兩種系統中調試均通過,兩種系統的配置分別為:
(1) 服務器:Digital Unix + Oracle Webserver3.0
瀏覽器:Netscape4.0.5或IE4.0
(2) 服務器: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();
寫入
在服務器端用servlet/jsp得到request,然后對其進行處理(save to a file or其他).注意這里要接收jsp/servlet的response, 否則它不運行
// 根據時間得文件名
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");