温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

 

Struts2文件上传一些小细节

大家都知道struts2对文件上传做了很好的封装,使文件上传不再那么恐怖。这里面struts2的文件上传主要依赖的是org.apache.struts2.interceptor.FileUploadInterceptor这个拦截器

关于这个拦截器我不想做过多的研究,这里主要讨论的是该拦截器里面定义的几个关于文件上传的重要属性。

protected Long maximumSize;   //允许上传单个文件的大小单位为字节

protected String allowedTypes; //允许上传的文件类型详见tomcatweb.xml文件

protected Set allowedTypesSet;

//允许上传的文件类型Set集合详见tomcatweb.xml文件

allowedTypesallowedTypesSet属性如有多个值之间用逗号隔开

以上的属性主要配置在struts.xml中对应文件上传Action的拦截器中

示列:

<!-- 文件上传Action -->

         <action name="Upload" class="UploadAction">

             <result name="success">success.jsp</result>

             <result name="input">upload.jsp</result>

             <interceptor-ref name="fileUpload">

//设置允许上传单个文件的大小单位为字节

                 <param name="maximumSize">102400</param>

//允许上传的文件类型详见tomcatweb.xml文件

               <param name="allowedTypesSet">image/jpeg</param>

             </interceptor-ref>

             <interceptor-ref name="defaultStack"/>

         </action>

注意:要使用文件上传功能我们必须显实的在对应文件上传的Action中指定<interceptor-ref name="fileUpload">这个拦截器及在该拦截器中配置我们需要的参数,最后别忘了附加上<interceptor-ref name="defaultStack"/>这个默认的拦截器,这样才能真正发挥struts2的文件上传功能。

示列代码:

Action中的示列代码

publicclass FileUploadAction extends ActionSupport {

    private File file;

    private String fileContentType;

    private String fileFileName;

    private String memo;

    @Override

    public String execute() throws Exception {

        String path=ServletActionContext.getRequest().getRealPath("/upload");

        if(file==null)

        {

            this.addFieldError("file", "文件不能为空,请选择");

            returnINPUT;

        }else

        {

            InputStream is=new FileInputStream(this.getFile());

            OutputStream os=new FileOutputStream(new File(path,this.getFileFileName()));

            byte[] buf=newbyte[1024];

            int length=0;

            while((length=is.read(buf))>0)

            {

                os.write(buf, 0, length);

            }

            is.close();

            os.close();

        }

        returnSUCCESS;

    }

    public File getFile() {

        returnfile;

    }

   

    publicvoid setFile(File file) {

        this.file = file;

    }

   

    public String getFileContentType() {

        returnfileContentType;

    }

    public String getFileFileName() {

        returnfileFileName;

    }

    public String getMemo() {

        returnmemo;

    }

   

    publicvoid setFileContentType(String fileContentType) {

        this.fileContentType = fileContentType;

    }

    publicvoid setFileFileName(String fileFileName) {

        this.fileFileName = fileFileName;

    }

    publicvoid setMemo(String memo) {

        this.memo = memo;

    }

   

}

jsp中的示列代码

<s:form action="Upload" enctype="multipart/form-data" method="post">

    <s:file name="file"></s:file>

    <s:textfield name="memo"></s:textfield>

    <s:submit></s:submit>

</s:form>

struts.xml中的示列代码

<action name="Upload" class="UploadAction">

             <result name="success">/success.jsp</result>

             <result name="input">/upload.jsp</result>

             <interceptor-ref name="fileUpload">

                 <param name="maximumSize">102400</param>

                 <param name="allowedTypes">application/msword</param>

             </interceptor-ref>

             <interceptor-ref name="defaultStack"/>

</action>

注意:

a)       设置文件上传属性在Action中对应的类型的java.io.File;

b)       设置文件上传表单的enctype="multipart/form-data" method="post"

private File file;

private String fileContentType;

private String fileFileName;

c)       红色标注的与文件上传表单中文件上传属性的name一致

d)       蓝色的为固定写法

e)       对应Action中拦截器的配置

<interceptor-ref name="fileUpload">

                 <param name="maximumSize">102400</param>

                 <param name="allowedTypes">application/msword</param>

</interceptor-ref>

<interceptor-ref name="defaultStack"/>

posted on 2009-10-21 10:33 雪山飞鹄 阅读(583) 评论(0)  编辑  收藏 所属分类: struts2

只有注册用户登录后才能发表评论。


网站导航: