許多朋友和我一樣,取得的檔案為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。。。