2007-10-08

webwork 文件上传

关键字: webwork 文件上传

Webwork的文件上传是采用拦截器来实现的

下面我们按照从客户端到服务器端的顺序讲讲如何配置:

1.第一点要注意的是form表单的两个属性

method="post" enctype="multipart/form-data" 一定要正确设置

2Action配置:上传文件的action要配置拦截器

<interceptor-ref name="fileUploadStack"></interceptor-ref>

UploadAction类:这个类中的方法名字跟form表单中文件上传输入筐关系很密切

//3个字段是必须的

private File upload; //取得上传得文件

    private String contentType;  //文件类型

private String fileName;   //原始文件名

public void setUpload(File file) {

              this.upload = file;

       }

 

       public String getContentType() {

              return contentType;

       }

 

       public void setContentType(String contentType) {

              this.contentType = contentType;

       }

 

       public String getUploadFileName() {

              return fileName;

       }

 

       public void setUploadFileName(String fileName) {

              this.fileName = fileName;

       }

 

<form name="form1" method="post" action="adSave.action"

enctype="multipart/form-data">

上传文件:<input type="file" name="upload" >

</form>

注意这儿的输入框

name=’upload’ 对应UploadActionpublic void setUpload(File file)

文件获取方法为public String getUploadFileName(),方法格式为getXXXFileName();

 

Webwork默认的是用commons fileupload工具上传,它需要一个临时目录 webwork.properties中可以配置webwork.multipart.saveDir=/temp

 

处理程序为

protected void uploadFile(File uploadFile, String savePath, String fileName)throws IOException{

              if (null != uploadFile) {

                     FileOutputStream outputStream = new FileOutputStream(savePath + fileName);

                     FileInputStream fileIn = new FileInputStream(uploadFile);

                     byte[] buffer = new byte[1024];

                     int len;

                     while ((len = fileIn.read(buffer)) > 0) {

                            outputStream.write(buffer, 0, len);

                     }

                     fileIn.close();

                     outputStream.close();

              }

       }

      

评论
发表评论

您还没有登录,请登录后发表评论

xly_971223
搜索本博客
博客分类
我的相册
C5b0e206-307c-3f61-aa60-9cfd71c61bb3-thumb
u=3528569133,1587051000&gp=38.jpg
共 2 张
最近加入圈子
存档
最新评论