struts2 檔案上傳為空白

來源:互聯網
上載者:User

許多朋友和我一樣,取得的檔案為null

屬性當然也是對的。為什麼呢。

是因為struts2上傳時要加上傳攔截,

如:

(1)   <action name="file" class="fileAction">
             <!--Auto Register Action:2010-07-07 14:22:37.281-->
             <!-- 配置fileUpload的攔截器 -->
          <interceptor-ref name="fileUpload">
           <!-- 配置允許上傳的檔案大小 -->
           <param name="maximumSize">4000000</param>
         </interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref> 
        <param name="save_path">/fileout</param>
            <result name="fileinfo" type="json">
              file/file.jsp
            </result>
          </action>

     配置後可正常上傳檔案。。

(2)一定要是post的而且enctype="multipart/form-data"

                                        <form  action="../console/processReleaseAction!upLoadFile.action" method="post" enctype="multipart/form-data">
                                            <input type="file" value="上傳" name="flowFile" />
                                            <input type="submit" value= "提交12" id="submit"/>
                                        </form>

(3)Action的變數和form的名稱一致,並且有get和set方法,我這就不詳細寫了

  /**
     * @屬性說明:接收用戶端檔案資訊
     **/
    private File flowFile;  
    
    /**
     * @屬性說明:接收用戶端檔案檔案名稱
     **/
    private String flowFileFileName;
    
    private String flowFileContentType;

(4)struts2的過濾器配置

      <action name="processReleaseAction" class="cn.net.sinodata.sinoflow.console.processrelease.action.ProcessReleaseAction">
           <interceptor-ref name="fileUpload">
                <param name="maximumSize">4000000</param>
           </interceptor-ref>
           <interceptor-ref name="defaultStack"></interceptor-ref>
           <result name="success">/WEB-INF/jsp/console/processrelease/processRelease.jsp</result>
        </action>

(5)web.xml的struts2配置添加(這個是在struts2基礎配置上添加的單獨的檔案上傳過濾,詳情可見struts2書籍介紹)

   <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
    </filter>

(6)struts.xml添加 <constant name="struts.multipart.maxSize" value="1073741824" />,完整的struts.xml為

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.ui.theme" value="simple" />
 <constant name="struts.i18n.encoding" value="GBK" />
 <constant name="struts.custom.i18n.resources" value="globalMessages" />
 <constant name="struts.multipart.maxSize" value="1073741824" />
 <!-- 引入相關設定檔 -->
 <include file="cn/net/sinodata/sinoflow/config/struts/struts-common.xml"/>
  <include file="cn/net/sinodata/ua/config/struts/user.xml" />
</struts>

<7>再不行,確認jar包沒問題後,請加qq654382144。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.