kooyee ‘s blog

开源软件, 众人努力的结晶, 全人类的共同财富
posts - 103, comments - 55, trackbacks - 0, articles - 66
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

在Applet中读取,写入文件内容

Posted on 2008-03-15 00:51 kooyee 阅读(1021) 评论(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();
= 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程序在两种系统中调试均通过,两种系统的配置分别为:

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

写入
//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, 否则它不运行

在服务器端用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");

评论

# re: 在Applet中读取,写入文件内容  回复  更多评论   

2008-03-20 22:14 by AIJUN
哇塞 这个你都懂啊,我研究了多年,被你一篇文章就点醒,你就是我的偶像了.好崇拜你哦~~~~

只有注册用户登录后才能发表评论。


网站导航: