File Upload
File Upload
File Upload
Google Search
Apache Struts 2 Documentation > Home > Guides > C ore Developers Guide > Interceptors > File Upload
Apache Struts 2 Docum e ntation
File Upload
Edit Page
Browse Space
Add Page
Add News
The Struts 2 framework provides built-in support for processing file uploads that conform to RFC 1867, "Form-based File Upload in HTML". When correctly configured the framework will pass uploaded file(s) into your Action class. Support for individual and multiple file uploads are provided. When a file is uploaded it will typically be stored in a temporary directory. Uploaded files should be processed or moved by your Action class to ensure the data is not lost. Be aware that servers may have a security policy in place that prohibits you from writing to directories other than the temporary directory and the directories that belong to your web application. Dependencies Basic Usage Uploading Multiple Files Uploading Multiple Files using Arrays Uploading Multiple Files using Lists Advanced C onfiguration File Size Limits File Types Error Messages Temporary Directories Alternate Libraries
Dependencies
The Struts 2 framework leverages add-on libraries to handle the parsing of uploaded files. These libraries are not included in the Struts distribution, you must add them into your project. The libraries needed are: Library URL Struts 2.0.x Struts 2.1.x 1.2.1 1.3.2
If you are using Maven then you can add these libraries as dependencies in your project's pom.xml. Srt 20xFl Ula Dpnece tus .. ie pod eednis <eedny dpnec> <ruI>omn-iepod/ruI> gopdcmosflula<gopd <riatdcmosflula<atfcI> atfcI>omn-iepod/riatd <eso>..<vrin vrin111/eso> <dpnec> /eedny <eedny dpnec> <ruI>omn-o/ruI> gopdcmosi<gopd <riatdcmosi<atfcI> atfcI>omn-o/riatd <eso>.<vrin vrin10/eso> <dpnec> /eedny Srt 21xFl Ula Dpnece tus .. ie pod eednis <eedny dpnec> <ruI>omn-iepod/ruI> gopdcmosflula<gopd <riatdcmosflula<atfcI> atfcI>omn-iepod/riatd <eso>..<vrin vrin121/eso> <dpnec> /eedny <eedny dpnec> <ruI>omn-o/ruI> gopdcmosi<gopd <riatdcmosi<atfcI> atfcI>omn-o/riatd <eso>..<vrin vrin132/eso> <dpnec> /eedny
Basic Usage
The o g a a h . t u s . n e c p o . i e p o d n e c p o class is included as part of the d f u t t c . As long as the required libraries r.pcesrt2itretrFlUlaItretr ealSak are added to your project you will be able to take advantage of of the Struts 2 fileUpload capability. C onfigure an Action mapping for your Action class as you typically would. Example action mapping:
A form must be create with a form field of type file, < N U t p = f l " n m = u l a " . The form used to upload the file must have its IPT ye"ie ae"pod> encoding type set to multipart/form-data, < O M a t o = d U l a " e c y e " u t p r / o m d t " m t o = p s " . The standard FR cin"opod ntp=mliatfr-aa ehd"ot>
struts.apache.org/release/2.3.x/docs/file-upload.html
1/6
08/03/13
File Upload
procedure for adding these elements is by using the Struts 2 tag libraries as shown in the following example: Example JSP form tags:
<:omato=dUla"mto=ps"ecye"utpr/omdt" sfr cin"opod ehd"ot ntp=mliatfr-aa> <:ienm=ula"lbl"ie/ sfl ae"pod ae=Fl"> <:umt> ssbi/ <sfr> /:om
The fileUpload interceptor will use setter injection to insert the uploaded file and related data into your Action class. For a form field named upload you would provide the three setter methods shown in the following example: Example Action class:
pcaecmeape akg o.xml; ipr jv.oFl; mot aai.ie ipr cmoesmhn.wr2AtoSpot mot o.pnypoyxok.cinupr; pbi casUlaAto etnsAtoSpot{ ulc ls podcin xed cinupr piaeFl fl; rvt ie ie piaeSrn cnetye rvt tig otnTp; piaeSrn flnm; rvt tig ieae pbi vi stpodFl fl){ ulc od eUla(ie ie ti.ie=fl; hsfl ie } pbi vi stpodotnTp(tigcnetye { ulc od eUlaCnetyeSrn otnTp) ti.otnTp =cnetye hscnetye otnTp; } pbi vi stpodieaeSrn flnm){ ulc od eUlaFlNm(tig ieae ti.ieae=flnm; hsflnm ieae } pbi Srn eeue){ ulc tig xct( /.. /. rtr SCES eun UCS; } }
The purpose of each one of these methods is described in the table below. Notice that if you have multiple file form elements with different names you would be required to have another corresponding set of these methods for each file uploaded. Method Signature setX(File file) setXC ontentType(String contentType) setXFileName(String fileName) Description The file that contains the content of the uploaded file. This is a temporary file and f l . e N m ( will not iegtae) return the original name of the file The mime type of the uploaded file The actual file name of the uploaded file (not the HTML name)
<@pg % ae lnug=jv" agae"aa cnetye"ethm;castUF8 otnTp=tx/tl hre=T-" pgEcdn=UF8% aenoig"T-"> <@tgi pei=s ui"srt-as % % alb rfx"" r=/tustg" > <tl hm> <ed ha> <il>hwae-flula -mlil flula uigLs<tte tteSocs iepod utpe iepod sn it/il> <ha> /ed <oy bd> <:omato=dMlilUlaUigra"mto=PS"ecye"utpr/omdt" sfr cin"outpepodsnAry ehd"OT ntp=mliatfr-aa> <:ielbl"ie() nm=ula"/ sfl ae=Fl 1" ae"pod > <:ielbl"ie() nm=ula"/ sfl ae=Fl 2" ae"pod > <:ielbl"Ie() nm=ula"/ sfl ae=Fl 3" ae"pod >
struts.apache.org/release/2.3.x/docs/file-upload.html
2/6
08/03/13
<:umt/ ssbi > <sfr> /:om <bd> /oy <hm> /tl
File Upload
MultipleFileUploadUsingArrayAction.java
pcaeogaah.tus.hwaeflula; akg r.pcesrt2socs.iepod ipr jv.oFl; mot aai.ie ipr cmoesmhn.wr2AtoSpot mot o.pnypoyxok.cinupr; /* * *Socs ato -mtpefl ula uigary hwae cin uil ie pod sn ra. * *@eso $ae $d vrin Dt$ I$ * / pbi casMlilFlUlaUigraAto etnsAtoSpot{ ulc ls utpeiepodsnArycin xed cinupr piaeFl[ ulas rvt ie] pod; piaeSrn[ ulaFlNms rvt tig] podieae; piaeSrn[ ulaCnetye; rvt tig] podotnTps pbi Fl[ gtpod){rtr ti.pod;} ulc ie] eUla( eun hsulas pbi vi stpodFl[ ula){ti.pod =ula;} ulc od eUla(ie] pod hsulas pod pbi Srn[ gtpodieae){rtr ti.podieae;} ulc tig] eUlaFlNm( eun hsulaFlNms pbi vi stpodieaeSrn[ ulaFlNm){ti.podieae =ulaFlNm;} ulc od eUlaFlNm(tig] podieae hsulaFlNms podieae pbi Srn[ gtpodotnTp( {rtr ti.podotnTps } ulc tig] eUlaCnetye) eun hsulaCnetye; pbi vi stpodotnTp(tig]ulaCnetye {ti.podotnTps=ulaCnetye } ulc od eUlaCnetyeSrn[ podotnTp) hsulaCnetye podotnTp; pbi Srn ula( trw Ecpin{ ulc tig pod) hos xeto Sse.u.rnl(\\ ula2) ytmotpitn"nn pod"; Sse.u.rnl(fls"; ytmotpitn"ie:) fr(ieu ulas { o Fl : pod) Sse.u.rnl(**"u"t+.egh); ytmotpitn"* ++\"ulnt() } Sse.u.rnl(flnms"; ytmotpitn"ieae:) fr(tign ulaFlNms { o Srn : podieae) Sse.u.rnl(**"n; ytmotpitn"* +) } Sse.u.rnl(cnettps"; ytmotpitn"otn ye:) fr(tigc ulaCnetye){ o Srn : podotnTps Sse.u.rnl(**"c; ytmotpitn"* +) } Sse.u.rnl(\\"; ytmotpitn"nn) rtr SCES eun UCS; } }
<@pg % ae lnug=jv" agae"aa cnetye"ethm;castUF8 otnTp=tx/tl hre=T-" pgEcdn=UF8% aenoig"T-"> <@tgi pei=s ui"srt-as % % alb rfx"" r=/tustg" > <tl hm> <ed ha> <il>hwae-flula -mlil flula uigLs<tte tteSocs iepod utpe iepod sn it/il> <ha> /ed <oy bd> <:omato=dMlilUlaUigit mto=PS"ecye"utpr/omdt" sfr cin"outpepodsnLs" ehd"OT ntp=mliatfr-aa> <:ielbl"ie() nm=ula"/ sfl ae=Fl 1" ae"pod > <:ielbl"ie() nm=ula"/ sfl ae=Fl 2" ae"pod > <:ielbl"Ie() nm=ula"/ sfl ae=Fl 3" ae"pod > <:umt/ ssbi > <sfr> /:om <bd> /oy <hm> /tl
MultipleFileUploadUsingListAction.java
pcaeogaah.tus.hwaeflula; akg r.pcesrt2socs.iepod ipr jv.oFl; mot aai.ie ipr jv.tlAryit mot aaui.raLs; ipr jv.tlLs; mot aaui.it ipr cmoesmhn.wr2AtoSpot mot o.pnypoyxok.cinupr; /* *
struts.apache.org/release/2.3.x/docs/file-upload.html
3/6
08/03/13
File Upload
*Socs ato -mlil fl ula uigLs hwae cin utpe ie pod sn it *@eso $ae $d vrin Dt$ I$ * / pbi casMlilFlUlaUigitcinetnsAtoSpot{ ulc ls utpeiepodsnLsAto xed cinupr piaeLs<ie ulas=nwAryitFl>) rvt itFl> pod e raLs<ie(; piaeLs<tig ulaFlNms=nwAryitSrn>) rvt itSrn> podieae e raLs<tig(; piaeLs<tig ulaCnetye =nwAryitSrn>) rvt itSrn> podotnTps e raLs<tig(; pbi Ls<ie gtpod){ ulc itFl> eUla( rtr ti.pod; eun hsulas } pbi vi stpodLs<ie ulas { ulc od eUla(itFl> pod) ti.pod =ulas hsulas pod; } pbi Ls<tig gtpodieae){ ulc itSrn> eUlaFlNm( rtr ti.podieae; eun hsulaFlNms } pbi vi stpodieaeLs<tig ulaFlNms { ulc od eUlaFlNm(itSrn> podieae) ti.podieae =ulaFlNms hsulaFlNms podieae; } pbi Ls<tig gtpodotnTp( { ulc itSrn> eUlaCnetye) rtr ti.podotnTps eun hsulaCnetye; } pbi vi stpodotnTp(itSrn>cnetye){ ulc od eUlaCnetyeLs<tig otnTps ti.podotnTps=cnetye; hsulaCnetye otnTps } pbi Srn ula( trw Ecpin{ ulc tig pod) hos xeto Sse.u.rnl(\\ ula1) ytmotpitn"nn pod"; Sse.u.rnl(fls"; ytmotpitn"ie:) fr(ieu ulas { o Fl : pod) Sse.u.rnl(**"u"t+.egh); ytmotpitn"* ++\"ulnt() } Sse.u.rnl(flnms"; ytmotpitn"ieae:) fr(tign ulaFlNms { o Srn : podieae) Sse.u.rnl(**"n; ytmotpitn"* +) } Sse.u.rnl(cnettps"; ytmotpitn"otn ye:) fr(tigc ulaCnetye){ o Srn : podotnTps Sse.u.rnl(**"c; ytmotpitn"* +) } Sse.u.rnl(\\"; ytmotpitn"nn) rtr SCES eun UCS; } }
Advanced Configuration
The Struts 2 d f u t p o e t e file defines several settings that affect the behavior of file uploading. You may find in necessary to change these eal.rpris values. The names and default values are:
Please remember that the struts.multipart.maxSize is the size limit of the whole request, which means when you uploading multiple files, the sum of their size must be below the struts.multipart.maxSize!
In order to change theses settings you define a constant in your applications s r t . m file like so: tusxl
<xlvrin"."ecdn=UF8? ?m eso=10 noig"T-"> <DCYEsrt PBI !OTP tus ULC "/Aah Sfwr Fudto/DDSrt Cniuain20/N -/pce otae onain/T tus ofgrto ./E" "tp/srt.pceogdd/tus20dd> ht:/tusaah.r/tssrt-..t" <tus srt> <osatnm=srt.utpr.aSz"vle"000"/ cntn ae"tusmliatmxie au=1000 > .. . <srt> /tus
Additionally the f l U l a interceptor has settings that can be put in place for individual action mappings by customizing your interceptor stack. iepod
<cinnm=dUla"cas"o.xml.podcin> ato ae"opod ls=cmeapeUlaAto" <necpo-e nm=bsctc"> itretrrf ae"aiSak/ <necpo-e nm=flUla" itretrrf ae"iepod> <aa nm=alwdye"tx/li<prm prm ae"loeTps>etpan/aa> <itretrrf /necpo-e> <necpo-e nm=vldto"> itretrrf ae"aiain/ <necpo-e nm=wrfo"> itretrrf ae"oklw/ <eutnm=sces>odrsl.s<rsl> rsl ae"ucs"go_eutjp/eut
struts.apache.org/release/2.3.x/docs/file-upload.html
4/6
08/03/13
<ato> /cin
File Upload
<xlvrin"."ecdn=UF8? ?m eso=10 noig"T-"> <DCYEsrt PBI !OTP tus ULC "/Aah Sfwr Fudto/DDSrt Cniuain20/N -/pce otae onain/T tus ofgrto ./E" "tp/srt.pceogdd/tus20dd> ht:/tusaah.r/tssrt-..t" <tus srt> <osatnm=srt.utpr.aSz"vle"000"/ cntn ae"tusmliatmxie au=1000 > <cinnm=dUla"cas"o.xml.podcin> ato ae"opod ls=cmeapeUlaAto" <necpo-e nm=bsctc"> itretrrf ae"aiSak/ <necpo-e nm=flUla" itretrrf ae"iepod> <aa nm=mxmmie>000/aa> prm ae"aiuSz"500<prm <itretrrf /necpo-e> <necpo-e nm=vldto"> itretrrf ae"aiain/ <necpo-e nm=wrfo"> itretrrf ae"oklw/ <eutnm=sces>odrsl.s<rsl> rsl ae"ucs"go_eutjp/eut <ato> /cin <srt> /tus
File Types
There are two ways to limit the uploaded file type, declaratively and programmatically. To declaratively limit the file type a comma separated list of allowedTypes can be specified as a fileUpload interceptor param as shown in the following example:
<cinnm=dUla"cas"o.xml.podcin> ato ae"opod ls=cmeapeUlaAto" <necpo-e nm=bsctc"> itretrrf ae"aiSak/ <necpo-e nm=flUla" itretrrf ae"iepod> <aa nm=alwdye"iaeje,mg/i<prm prm ae"loeTps>mg/pgiaegf/aa> <itretrrf /necpo-e> <necpo-e nm=vldto"> itretrrf ae"aiain/ <necpo-e nm=wrfo"> itretrrf ae"oklw/ <eutnm=sces>odrsl.s<rsl> rsl ae"ucs"go_eutjp/eut <ato> /cin
When the uploaded file type does not match one of the MIME types specified a field error will be created as described in the next section entitled Error Messages. Programmatically limiting the file type means using the information passed in to your Action class via the s t C n e t y e S r n c n e t y e method. The benefit to this type of approach would be that it's more flexible and no interceptor eXotnTp(tig otnTp) configuration would be needed if file sizes are keep under 2 megs.
Error Messages
If an error occurs several field errors will be added assuming that the action implements c m o e s m h n . w r 2 V l d t o A a eor o.pnypoyxok.aiainwr extends c m o e s m h n . w r 2 A t o S p o t These error messages are based on several i18n values stored in strutso.pnypoyxok.cinupr. messages.properties, a default i18n file processed for all i18n requests. You can override the text of these messages by providing text for the following keys: Error Key struts.messages.error.uploading struts.messages.error.file.too.large struts.messages.error.content.type.not.allowed struts.messages.error.file.extension.not.allowed struts.messages.upload.error.SizeLimitExceededException struts.messages.upload.error.<Exception class SimpleName> Description A general error that occurs when the file could not be uploaded Occurs when the uploaded file is too large as specified by maximumSize. Occurs when the uploaded file does not match the expected content types specified Occurs when uploaded file has disallowed extension Occurs when the upload request (as a whole) exceed configured struts.multipart.maxSize Occurs when any other exception took place during file upload process
Temporary Directories
All uploaded files are saved to a temporary directory by the framework before being passed in to an Action. Depending on the allowed file sizes it may be necessary to have the framework store these temporary files in an alternate location. To do this change s r t . u t p r . a e i to tusmliatsvDr the directory where the uploaded files will be placed. If this property is not set it defaults to j v x s r l t c n e t t m d r Keep in mind that aa.eve.otx.epi. on some operating systems, like Solaris, / m is memory based and files stored in that directory would consume an amount of RAM approximately tp equal to the size of the uploaded file.
struts.apache.org/release/2.3.x/docs/file-upload.html
5/6
08/03/13
File Upload
Alternate Libraries
The s r t . u t p r . a s rused by the fileUpload interceptor to handle HTTP POST requests, encoded using the MIME-type multipart/formtusmliatpre data, can be changed out. C urrently there are two choices, jakarta and pell. The jakarta parser is a standard part of the Struts 2 framework needing only its required libraries added to a project. The pell parser uses Jason Pell's multipart parser instead of the C ommons-FileUpload library. The pell parser is a Struts 2 plugin, for more details see: http://cwiki.apache.org/S2PLUGINS/pell-multipart-plugin.html. There was a third alternative, cos, but it was removed due to licensing incompatibilities.
Generated by Atlassian C onfluence (Version: 3.4.9 Build: 2042 Feb 14, 2011) Auto Export Plugin (Version: 1.0.0-dkulp)
struts.apache.org/release/2.3.x/docs/file-upload.html
6/6