正常上傳檔案前端頁面長這樣
<form action='fileupload.do' method='post' enctype='multipart/form-data'>
<input type="file" name='upload' id="upload" multiple/>
<input type='submit' value="上傳">
</form>
但struts2預設的檔案限制為2MB
若要加大有兩個方法
- 打開struts2-core-2.x.x.jar\org\apache\struts\default.properties,其中的struts.multipart.maxSize=2097152修改即可
- 再struts.xml中增加 <constant name="struts.multipart.maxSize" value="xxxxxxx" />
另外在struts.xml中可針對不同的action進行設定
<action name="fileupload" class="xxxx.xxxxxx" method="addFile">
<interceptor-ref name="fileUpload">
<param name="maximumSize">10000000</param>
<param name="allowedTypes">image/jpeg,image/gif</param>
<param name="allowedTypes">image/jpeg,image/gif</param>
</interceptor-ref>
<interceptor-ref name="basicStack"/>
<result name="success">/jsp/xxx.jsp</result>
<result name="error">/jsp/xxx.jsp</result>
</action>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
這兩項interceptor設定是必要的
因前端form enctype='multipart/form-data'
說明是以二進位的方式傳遞
若沒有設定後端會收不到
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
這兩項interceptor設定是必要的
因前端form enctype='multipart/form-data'
說明是以二進位的方式傳遞
若沒有設定後端會收不到
沒有留言:
張貼留言