struts2 文件上传

如下是配置文件上传的大小;

<struts>
<!-- 全局配置
  0.请求数据编码
 -->
<constant name="struts.il8n.encoding" value="utf-8"></constant>
<!--1.修改struts默认的访问后缀  -->
<constant name="struts.action.extension" value="action,do,"></constant>
    <!--2.修改xml自动重新加载  -->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!--3.开启动态方法调用(默认不开启) -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!--4.**修改上传文件的最大大小为30M**  -->
<constant name="struts.multipart.maxSize" value="30*1024*1024"></constant>

限制运行上传的文件的类型

    <action name="demo1" class="cn.aufe.action.FileuploadDemo1">
            <!--限制运行上传的文件的类型  -->
            <interceptor-ref name="defalutStack">
            <!--限制运行文件的扩展名  -->
            <param name="fileUpload.allowedExtensions">txt,jpg</param>
            </interceptor-ref>

下面是示例:

public class FileuploadDemo1 extends ActionSupport {

    private File file1;
    private String file1FileName;//命名规则不可变
    private String file1ContenType;

    public void setFile1(File file1) {
        this.file1 = file1;
    }
    public void setFile1FileName(String file1FileName) {
        this.file1FileName = file1FileName;
    }
    public void setFile1ContenType(String file1ContenType) {
        this.file1ContenType = file1ContenType;
    }
 @Override
public String execute() throws Exception {
     //huode 路径
     String path = ServletActionContext.getServletContext().getRealPath("/upload");
     //生成目标文件对象
     File file = new File(path, file1FileName);
     //生成目标文件    
     FileUtils.copyFile(file1, file);

     return "hello";
}

限制文件上传的类型:

    <action name="demo1" class="cn.aufe.action.FileuploadDemo1">
            <!-- 限制运行上传的文件的类型   -->
            <interceptor-ref name="defaultStack">
        <!--    限制运行文件的扩展名 -->
            <param name="fileUpload.allowedExtensions">txt,jpg,jar</param> 
            <!--限制运行的类型(与上面同时使用取交集)  -->
            <param name="fileUpload.allowedTypes">text/plain</param>
            </interceptor-ref>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值