You can obtain the MultipartRequestWrapper from the ServletActionContext or by utilizing the fileUpload interceptor. The fileUpload interceptor is preferred.

See the file upload page for more examples and advanced configuration

Ask the ServletActionContext

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

(star) _Preferred_

  • Ensure that {{fileUpload }} Interceptor is included in the Action's stack.
    • (info) The default stack already includes {{fileUpload }}.
  • Ensure that the HTML form sets the enctype and specifies on or more file type inputs.
<form name="myForm" enctype="multipart/form-data">
     <input type="file" name="myDoc" value="Browse ..." />
     <input type="submit" />
  </form>
  • Ensure that the Action provides one or more fileUpload mutator methods, with names that correspond to name of the file type input.
public void setMyDoc(File myDoc)
public void setMyDocContentType(String contentType)
public void setMyDocFileName(String filename)
  • The Action may also provide the corresponding accessor methods.
public File getMyDoc()
public String getMyDocContentType()
public String getMyDocFileName()

Handling multiple files

When multiple files are uploaded by a form, the files are represented by an array.

Given:

<form name="myForm" enctype="multipart/form-data">
      <input type="file" name="myDoc" value="Browse File A ..." />
      <input type="file" name="myDoc" value="Browse File B ..." />
      <input type="file" name="myDoc" value="Browse File C ..." />
      <input type="submit" />
   </form>

The Action class can define file handling methods that accept an array.

public void setMyDoc(File[] myDocs)
public void setMyDocContentType(String[] contentTypes)
public void setMyDocFileName(String[] fileNames)

The uploaded files can be handled by iterating through the appropriate array.

Extra Information

Property

Default

struts.multipart.parser

Commons FileUpload

struts.multipart.saveDir

javax.servlet.context.tempdir as defined by container

struts.multipart.maxSize

Approximately 2M

@see struts.properties
@see org.apache.struts2.dispatcher.FilterDispatcher#doFilter(SerlvetRequest, ServletRepsonse, FilterChain)
@see org.apache.struts2.dispatcher.DispatcherUtil#wrapRequest(HttpServletRequest, SerlvetContext)
@see org.apache.struts2.dispatcher.multipart.MultipartRequestWrapper
@see org.apache.struts2.interceptor.FileUploadInterceptor

  • No labels

4 Comments

  1. The MultipartRequestWrapper is found in the Struts1 core JAR. Apparently this is needed in order to use the file upload interceptor

  2. You also need Jakarta Commons FileUpload (of course) and IO libraries (which versions?)

  3. Anonymous

    Hi I am newbie in struts 2, I am triying to upload a file and handle error, but when a error occurs Struts send me FreeMarker template error!
    It seems that $

    Unknown macro: {error}

    is null and freemaker does not what to do.
    I tried to make a struts-messages..properties but It does work neither.
    Can anybody help me???

  4. If you wanted to have other fields in the form besides just the file uploads, then you would need to append "[n]" to the ends of the input names, e.g.

       <input type="file" name="myDoc[0]" value="Browse File A...">
       <input type="text" name="myDocRelated[0]">
       <input type="file" name="myDoc[1]" value="Browse File A...">
       <input type="text" name="myDocRelated[1]">