Posted on 2008-03-15 00:51 
kooyee 阅读(1057) 
评论(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 >
< HTML >
 < HEAD >
< HEAD >
 < TITLE >读取文件的Applet< /TITLE >
< TITLE >读取文件的Applet< /TITLE >
 < /HEAD >
< /HEAD >
 < BODY >
< BODY >

 这是服务器上TXT文件的内容< BR >
这是服务器上TXT文件的内容< BR >

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

 < /Applet >
< /Applet >
 < /BODY >
< /BODY >

 < /HTML >
< /HTML >

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

 public class getFile extends Applet
public class getFile extends Applet


 {
{
 String info;
String info;

 public void init()
public void init()


 {
{
 URL url;
URL url;
 URLConnection urlc;
URLConnection urlc;

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


 try
try {
{

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

 info = getInfo(urlc);
info = getInfo(urlc);

 }catch(MalformedURLException mfe)
}catch(MalformedURLException mfe) {
{
 System.out.println("URL form error!");
System.out.println("URL form error!");

 }catch(IOException ioe)
}catch(IOException ioe) {
{
 System.out.println("IO Exception!");
System.out.println("IO Exception!");
 }
}
 }
}

 public void paint(Graphics g)
public void paint(Graphics g)


 {
{
 g.setColor(Color.red);
g.setColor(Color.red);
 g.drawString(info,50,50);
g.drawString(info,50,50);
 }
}

 public String getInfo(URLConnection urlc)
public String getInfo(URLConnection urlc)


 {
{
 String txt = new String();
String txt = new String();
 InputStream is;
InputStream is;
 int i;
int i;


 try
try {
{
 is = urlc.getInputStream();
is = urlc.getInputStream();
 i = is.read();
i = is.read();

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

 is.close();
is.close();


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

 return txt;
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
//create url to the file on server in applet class
 URL url = new URL(this.getCodeBase(),"filename");
            URL url = new URL(this.getCodeBase(),"filename");
 URLConnection urlc = url.openConnection();
            URLConnection urlc = url.openConnection();
 
            
 BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
            BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
 String line=null;
            String line=null;
 if( (line = in.readLine()) != null )
            if( (line = in.readLine()) != null ) 

 
             {
{            
 System.out.printv(line);
                System.out.printv(line);            
 }
            }
 in.close();
            in.close();
写入
在服务器端用servlet/jsp得到request,然后对其进行处理(save to a file or其他).注意这里要接收jsp/servlet的response, 否则它不运行
 // 根据时间得文件名
// 根据时间得文件名
 Calendar calendar = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
 String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
 fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
fileame = request.getRealPath("/")+fileame;//生成的html文件保存路径
 FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
FileOutputStream fileoutputstream = new FileOutputStream(fileame);//建立文件输出流
 byte tag_bytes[] = templateContent.getBytes();
byte tag_bytes[] = templateContent.getBytes();
 fileoutputstream.write(tag_bytes);
fileoutputstream.write(tag_bytes);
 fileoutputstream.close();
fileoutputstream.close();

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