关于Struts2的上传,在文件类型或大小错误后,就不能继续上传的问题

这个问题的主要原因是:

因为spring的bean 默认是单态singleton
的。这样导致一个问题:就是当我们如果上传了一个不允许的类型文件或超出大小范围的文件,之后再上传正常的文件也上传不了。这时候需要把bean 设置为非单态模式  scope="prototype":

<bean id="IssueTeaAction" class="zjnu.soft.action.issue.IssueTeaAction" scope="prototype">
           <property name="issueService" ref="issueService"></property>
   </bean>

加入 scope="prototype"能解决问题;

Struts2文件上传步骤:

1、在jsp页页面

                 <s:form method="post" enctype="multipart/form-data"
                        action="uploadMaterials" onsubmit="return checkfm(this)">

                    <s:file name="file" theme="simple" id="file" />

                   </s:form>

   注意method="post" enctype="multipart/form-data"

表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:

a 、   application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里的value属性值,采用这种编码方式的表单会将表单域的值处理成URL编码方式。

b 、   multipart/form-data:这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里。

c、     text/plain:这种方式主要适用于直接通过表单发送邮件的方式。



2、在Struts.xml中

        <action name="uploadMaterials" class="IssueTeaAction"
            method="uploadMaterials">
            <result>
                /WEB-INF/FrontstageManagement/issueManager/share/message.jsp
            </result>
              <result name="input">
                /WEB-INF/FrontstageManagement/issueManager/share/errorupload.jsp
            </result>
           
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">4096000</param><!-- 单个文件的大小 单位4000kb -->
                <param name="allowedTypes">
                    application/powerpoint,application/pdf,application/vnd.ms-word,text/plain,application/xslt+xml
                    ,application/zip,application/rar,application/octet-stream
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />
        </action>

3、在action就略大家的都不一样嘛!
http://item.taobao.com/item.htm?id=15656343226

猜你喜欢

转载自330967688-qq-com.iteye.com/blog/1616734
今日推荐