我使用Struts上传文件.
使用的是org.apache.struts.action.ActionForm.Tomcat 5服务器.
现象:
但是经测试,使用324K的JPG图片和743K的New Stories (Highway Blues).wma文件可以正常上传.
只要上传的单个文件超过1M左右,就不能取到页面数据.如image = null, voice = null.(在调试状态下测出)
上传2.85M的JPG文件或者2.54M的avi文件则出错, image = null, voice = null.(在调试状态下测出)
请问为什么?!
代码如下:
JSP
<html:form action="/createQuestion.do?method=createEssayQuestion" method="post" enctype="multipart/form-data" onsubmit="return checkSubmit(this)">
<html:file property="imagePath"/>
<html:file property="voicePath"/>
</html:form> Action
public class CreateQuestionAction extends DispatchAction
{
......
public ActionForward createEssayQuestion(ActionMapping mapping, ActionForm createQuestionForm, HttpServletRequest request, HttpServletResponse response)
{
CreateQuestionForm form = (CreateQuestionForm) createQuestionForm;
// 取得页面数据
......
FormFile image = form.getImagePath();
FormFile voice = form.getVoicePath();
......
struts-config.xml
<action
      attribute="createQuestionForm"
      name="createQuestionForm"
      parameter="method"
      path="/createQuestion"
      scope="request"
      type="com.hthk.iisz.etest.action.CreateQuestionAction"
      validate="false">
      <forward name="failBlank" path="/question/createBlankQuestion.jsp" />
      <forward name="failMulti" path="/question/createMultiQuestion.jsp" />
      <forward name="show" path="/question/create_question.jsp" />
      <forward name="failEssay" path="/question/createEssayQuestion.jsp" />
      <forward name="success" path="/queryQuestion.do?method=query" />
      <forward name="failSingle" path="/question/createSingleQuestion.jsp" />
</action> web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/Gavin-config.xml,/WEB-INF/Vince-config.xml,/WEB-INF/Alexz-config.xml,
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>init</servlet-name>
<servlet-class>
com.hthk.iisz.etest.servlet.InitServlet
</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
com.hthk.iisz.common.filters.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>
com.hthk.iisz.etest.servlet.SessionFilter
</filter-class>
<init-param>
<param-name>freePages</param-name>
<param-value>
/login.jsp;/Loginuser.do;/loginout.do;/index.jsp;/playMedia.jsp
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

解决方案 »

  1.   

    月,贴就贴完整吗
    你这贴的是action?
    你再config里面+上parameter=createEssayQuestion
    然后再action里面去设置下如何读取上传的文件,我不知道你原来是怎么写的
    顺便问下,你不会就用标签,acton里面没写吧?
      

  2.   

    是的,的确没有贴完整的代码,我只是把觉得相关的重要的代码贴上来了.
    我使用的是public class CreateQuestionAction extends DispatchAction
    struts-config.xml中的
    parameter="method
    是和JSP页中的
    <html:form action="/createQuestion.do?method=createEssayQuestion" method="post" enctype="multipart/form-data" onsubmit="return checkSubmit(this)"> 
    相关我用断点跟踪到
    FormFile image = form.getImagePath(); 
    FormFile voice = form.getVoicePath();
    就得不到对象了.image=null  voice=null
    接下去的代码都不用执行了.
      

  3.   

    关键是我上传小文件(<1M)的都是成功的.
    一旦上传稍大一点的文件(>2M)的就失败,在Form中取FormFile都是null.
    而且,Form的 validate="false",没有做校验.为什么会上传大文件就得不到对象呢?
      

  4.   

    web.xml 
    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <servlet> 
    <servlet-name>action </servlet-name> 
    <servlet-class> 
    org.apache.struts.action.ActionServlet 
    </servlet-class> 
    <init-param> 
    <param-name>config </param-name> 
    <param-value> 
    /WEB-INF/Gavin-config.xml,/WEB-INF/Vince-config.xml,/WEB-INF/Alexz-config.xml, 
    </param-value> 
    </init-param> 
    <init-param> 
    <param-name>debug </param-name> 
    <param-value>3 </param-value> 
    </init-param> 
    <init-param> 
    <param-name>detail </param-name> 
    <param-value>3 </param-value> 
      

  5.   

    估计是上传组件的配置的问题,里面有控制允许上传大小的参数。超过这个大小那么上传的内容就null了
      

  6.   

    <action 
          attribute="createQuestionForm" 
          name="createQuestionForm" 
          parameter="method" 
          path="/createQuestion" 
          scope="request" 
          type="com.hthk.iisz.etest.action.CreateQuestionAction" 
          validate="false"> 
           <forward name="failBlank" path="/question/createBlankQuestion.jsp" /> 
           <forward name="failMulti" path="/question/createMultiQuestion.jsp" /> 
           <forward name="show" path="/question/create_question.jsp" /> 
           <forward name="failEssay" path="/question/createEssayQuestion.jsp" /> 
           <forward name="success" path="/queryQuestion.do?method=query" /> 
           <forward name="failSingle" path="/question/createSingleQuestion.jsp" /> 
    </action> 
      

  7.   

    找到原因了.
    在另一个Struts-config.xml中有定义
    <controller maxFileSize="2M" />
    所以只能上传2M的文件.
      

  8.   

    #upload
    struts.multipart.parser=jakarta
    struts.multipart.saveDir=C:\\tmp
    #2M=2097152 byte
    #5M=5242880 byte
    struts.multipart.maxSize=5242880