Struts2的文件上传

1. 测试文件上传页面upload.jsp

    此处使用Struts2标签来生成上传文件的表单,为了让浏览器采用二进制数据的方式把数据发送到服务器,我们必须把表单的method设置为POST,将enctype设置为multipart/form-data。jsp代码如下:

<body>
<s:form action="uploadPro" method="post" enctype="multipart/form-data">
    <s:file name="upload" label="选择文件"/><br />
    <s:submit value="上传"/>
</s:form>
</body>

2. 实现文件上传的Action(UploadAction.java)

    Action实现类其实非常简单,只需要定义三个属性接收文件参数即可,一是类型为File的xxx封装文件内容,二是String类型的xxxFileName封装文件名,三是String类型的xxxContentType封装文件类型。代码如下:

package test;

import com.opensymphony.xwork2.ActionSupport;
import java.io.*;

public class UploadAction extends ActionSupport{
    //封装上传文件域
    private File upload;
    //封装上传文件类型
    private String uploadContentType;
    //封装上传文件名
    private String uploadFileName;
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public File getUpload(){
        return (this.upload);
    }
    public void setUploadContentType(String uploadContentType){
        this.uploadContentType = uploadContentType;
    }
    public String getUploadContentType(){
        return (this.uploadContentType);
    }
    public void setUploadFileName(String uploadFileName){
        this.uploadFileName = uploadFileName;
    }
    public String getUploadFileName(){
        return (this.uploadFileName);
    }
    @Override
    public String execute() throws Exception{
        //将文件写入服务器,没有此文件时新建
        File file = new File("h:\\AAA\\"+getUploadFileName());
        FileOutputStream fos = new FileOutputStream(file);
        FileInputStream fis = new FileInputStream(getUpload());
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0){
            fos.write(buffer , 0 , len);
        }
        fos.close();
        return SUCCESS;
    }
}

3. 配置文件上传的Action(struts.xml)

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">    
<struts>
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <!-- 设置该应用使用的解码集 -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <!-- 配置处理文件上传的Action -->
        <action name="uploadPro" class="test.UploadAction">
            <!-- 配置Struts 2默认的视图页面 -->
            <result>/upload.jsp</result>    
        </action>
    </package>
</struts>

4. 实现文件过滤

    方法一:直接在Action中判断文件的类型是否为允许类型

    方法二:拦截器实现文件过滤,如下:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">    
<struts>
    <constant name="struts.custom.i18n.resources" value="mess"/>
    <!-- 设置该应用使用的解码集 -->
    <constant name="struts.i18n.encoding" value="GBK"/>
    <package name="lee" extends="struts-default">
        <!-- 配置处理文件上传的Action -->
        <action name="uploadPro" class="org.crazyit.app.action.UploadAction">
            <!-- 配置fileUpload的拦截器 -->
            <interceptor-ref name="fileUpload">
                <!-- 配置允许上传的文件类型 -->
                <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
                <!-- 配置允许上传的文件大小 -->
                <param name="maximumSize">2000</param>
            </interceptor-ref>
            <!-- 配置系统默认的拦截器 -->
            <interceptor-ref name="defaultStack"/>
            <!-- 配置Struts 2默认的视图页面 -->
            <result>/WEB-INF/content/succ.jsp</result>
        </action>
    </package>
</struts>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值