webwork 文件上传

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

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

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

method="post" enctype="multipart/form-data" 一定要正确设置<o:p></o:p>

2Action配置:上传文件的action要配置拦截器<o:p></o:p>

<interceptor-ref name="fileUploadStack"></interceptor-ref><o:p></o:p>

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

//3个字段是必须的<o:p></o:p>

private File upload; //取得上传得文件<o:p></o:p>

    private String contentType;  //文件类型<o:p></o:p>

private String fileName;   //原始文件名<o:p></o:p>

public void setUpload(File file) {<o:p></o:p>

              this.upload = file;<o:p></o:p>

       }<o:p></o:p>

<o:p> </o:p>

       public String getContentType() {<o:p></o:p>

              return contentType;<o:p></o:p>

       }<o:p></o:p>

<o:p> </o:p>

       public void setContentType(String contentType) {<o:p></o:p>

              this.contentType = contentType;<o:p></o:p>

       }<o:p></o:p>

<o:p> </o:p>

       public String getUploadFileName() {<o:p></o:p>

              return fileName;<o:p></o:p>

       }<o:p></o:p>

<o:p> </o:p>

       public void setUploadFileName(String fileName) {<o:p></o:p>

              this.fileName = fileName;<o:p></o:p>

       }<o:p></o:p>

<o:p> </o:p>

<form name="form1" method="post" action="adSave.action"<o:p></o:p>

enctype="multipart/form-data"><o:p></o:p>

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

</form><o:p></o:p>

注意这儿的输入框<o:p></o:p>

name=’upload’ 对应UploadActionpublic void setUpload(File file)<o:p></o:p>

文件获取方法为public String getUploadFileName(),方法格式为getXXXFileName();<o:p></o:p>

<o:p> </o:p>

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

<o:p> </o:p>

处理程序为<o:p></o:p>

protected void uploadFile(File uploadFile, String savePath, String fileName)throws IOException{<o:p></o:p>

              if (null != uploadFile) {<o:p></o:p>

                     FileOutputStream outputStream = new FileOutputStream(savePath + fileName);<o:p></o:p>

                     FileInputStream fileIn = new FileInputStream(uploadFile);<o:p></o:p>

                     byte[] buffer = new byte[1024];<o:p></o:p>

                     int len;<o:p></o:p>

                     while ((len = fileIn.read(buffer)) > 0) {<o:p></o:p>

                            outputStream.write(buffer, 0, len);<o:p></o:p>

                     }<o:p></o:p>

                     fileIn.close();<o:p></o:p>

                     outputStream.close();<o:p></o:p>

              }<o:p></o:p>

       }<o:p></o:p>

       <o:p></o:p>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值