struts2 文件上传

来源:互联网 发布:通达信原油看盘软件 编辑:程序博客网 时间:2024/06/06 05:58

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

<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;    } @Overridepublic 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>
原创粉丝点击