Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:

Update formatting

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

Ask the ServletActionContext

Code Block

MultipartRequestWrapper multipartRequest = 

...

((MultipartRequestWrapper)ServletActionContext.getRequest())

The MultipartRequestWrapper provideds With multipartRequest, one could access methods such as getFiles(...), getFile(...), getContentType(...), hasErrors(), getErrors() etc to handle , getErrors, and so forth, so that you can process the file uploaded. Method B (Recommended)
Add a 'fileUpload' interceptor to the action. For example, in the following case:

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.
Code Block
xml
xml
  <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.
Code Block
java
java
public void setMyDoc(File myDoc) { ...}
public void setMyDocContentType(String contentType) { .... }
public void setMyDocFileName(String filename) { .... }

  • The Action may also provide the corresponding accessor methods.
Code Block
java
java

public File getMyDoc()
public ContentType setMyDocContentType()
public String setMyDocFileName()

Handling multiple files

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

Givenwith these methods, one could do whatever is needed with the uploaded file. If multiple files are uploaded as in following:

Code Block
xml
xml
   <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 needs only make the corresponding method an array, orders followed such that getMyDoc()0 will have its content type as getMyDoc()0 and its file name as getMyDoc()1Action class can define file handling methods that accept an array.

Code Block
java
java
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

action.multipart.parser

...

Commons FileUpload

action.multipart.saveDir

...

javax.servlet.context.tempdir as defined by container

...

...

action.multipart.maxSize

...

Approximately 2M

@see webworkaction.properties
@see comorg.apache.opensymphonystruts.webworkaction2.dispatcher.FilterDispatcher#doFilter(SerlvetRequest, ServletRepsonse, FilterChain)
@see comorg.apache.opensymphonystruts.webworkaction2.dispatcher.DispatcherUtil#wrapRequest(HttpServletRequest, SerlvetContext)
@see comorg.apache.opensymphonystruts.webworkaction2.dispatcher.multipart.MultipartRequestWrapper
@see comorg.apache.opensymphonystruts.webworkaction2.interceptor.FileUploadInterceptor