You can obtain the MultipartRequestWrapper from the ServletActionContext or by utilizing the fileUpload
interceptor. The fileUpload
interceptor is preferred.
Note |
---|
See the file upload page for more examples and advanced configuration |
Ask the ServletActionContext
Code Block |
---|
MultipartRequestWrapper multipartRequest = ((MultipartRequestWrapper)ServletActionContext.getRequest()) |
The MultipartRequestWrapper provideds access methods such as getFiles
, getFile
, getContentType
, hasErrors
, getErrors
, and so forth, so that you can process the file uploaded.
Utilize the fileUpload Interceptor
_Preferred_
- Ensure that {{fileUpload }} Interceptor is included in the Action's stack.
The default stack already includes {{fileUpload }}.
- Ensure that the HTML form sets the
enctype
and specifies on or more file type inputs.
...
Code Block | ||||
---|---|---|---|---|
| ||||
public File getMyDoc() public ContentTypeString setMyDocContentTypegetMyDocContentType() public String setMyDocFileNamegetMyDocFileName() |
Handling multiple files
When multiple files are uploaded by a form, the files are represented by an array.
...
The uploaded files can be handled by iterating through the appropriate array.
Extra Information
Property | Default |
---|---|
action struts.multipart.parser | Commons FileUpload |
action struts.multipart.saveDir | |
action struts.multipart.maxSize | Approximately 2M |
@see actionstruts.properties
@see org.apache.strutsstruts2.action2.dispatcher.FilterDispatcher#doFilter(SerlvetRequest, ServletRepsonse, FilterChain)
@see org.apache.struts.action2struts2.dispatcher.DispatcherUtil#wrapRequest(HttpServletRequest, SerlvetContext)
@see org.apache.strutsstruts2.action2.dispatcher.multipart.MultipartRequestWrapper
@see org.apache.struts.action2struts2.interceptor.FileUploadInterceptor