Struts2 File Upload is empty

Source: Internet
Author: User

Many friends, like me, get the file null

The properties are certainly true. Why is it.

is because struts2 upload to add intercept,

Such as:

(1) <action name= "file" class= "Fileaction" >
<!--Auto Register action:2010-07-07 14:22:37.281-->
<!--configuring FileUpload interceptors--
<interceptor-ref name= "FileUpload" >
<!--configuration allows file size to be uploaded--
<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>

After configuration, the file can be uploaded normally.

(2) must be post and enctype= "Multipart/form-data"

<form action= ". /console/processreleaseaction!uploadfile.action "method=" post "enctype=" Multipart/form-data ">
<input type= "file" value= "Upload" name= "Flowfile"/>
<input type= "Submit" value= "submitted" id= "Submission"/>
</form>

(3) The variable of action is the same as the name of the form, and there is a get and set method, which I will not write in detail.

/**
* @ Property Description: Receive client file information
**/
Private File Flowfile;

/**
* @ Property Description: Receive client file filename
**/
Private String Flowfilefilename;

Private String Flowfilecontenttype;

(4) Filter configuration for 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) The STRUTS2 configuration of Web. XML is added (this is a separate file upload filter added on the struts2 base configuration, details Struts2 book introduction)

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

(6) struts.xml add <constant name= "struts.multipart.maxSize" value= "1073741824"/> the complete struts.xml is

<?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"/>
<!--introduce relevant profiles--
<include file= "Cn/net/sinodata/sinoflow/config/struts/struts-common.xml"/>
<include file= "Cn/net/sinodata/ua/config/struts/user.xml"/>
</struts>

<7> no more, confirm the jar package is OK, please add qq654382144 ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.