Source

webwork / docs / fileupload.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>WebWork Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
</head>
<body>
  <div id="page-logo">
    <a href="index.html"><img src="logo-small.png" border="0"/></a>
  </div>
    <div class="snip-title">
	  <h1 class="snip-name">Webwork 2 file upload handling
  
  </h1>
  </div>
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 
 <h3 class="heading-1">File upload using WebWork2
</h3><p class="paragraph"/>Webwork comes with built in file upload support. Uploading a file is simple. When ServletDispatcher begins it checks to see if the request contains multipart content. If it does the dispatcher creates a MultipartWrapperRequest. This wrapper handles receiving the file and saving to disk. It is important for the action programmer to check to see if any errors occured during processing. Three properties can be set that effect file uploading. 
<h3 class="heading-1-1">Properties
</h3><p class="paragraph"/>Webwork properties can be set by putting a file 'webwork.properties' in WEB-INF/classes. Any property found there will override the default value.
<ol>
<li>webwork.multipart.parser - This should be set to a class that extends MultiPartRequest. Currently WebWork ships with two implementations. "com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest" and "com.opensymphony.webwork.dispatcher.multipart.CosMultiPartRequest" If the property is not found the Pell parser is used.</li>
<li>webwork.multipart.saveDir - The directory where the uploaded files will be placed. If this property is not set it defaults to javax.servlet.context.tempdir.</li>
<li>webwork.multipart.maxSize - The maximum file size in bytes to allow for upload. This helps prevent system abuse by someone uploading lots of large files. The default value is 2 Megabytes and can be set as high as 2 Gigabytes (higher if you want to edit the Pell multipart source but you really need to rethink things if you need to upload files larger then 2 Gigabytes!) If you are uploading more than one file on a form the maxSize applies to the combined total, not the individual file sizes.</li>
</ol><p class="paragraph"/>If you're happy with the defaults there is no need to put any of the properties in webwork.properties. Here is my current webwork.properties
<div class="code"><pre># don't really need to set <span class="java&#45;keyword">this</span> but I put it here <span class="java&#45;keyword">for</span> testing
# various values
webwork.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest<p class="paragraph"/># put the uploaded files in /tmp. My application will move them to their
# <span class="java&#45;keyword">final</span> destination
webwork.multipart.saveDir=/tmp</pre></div><p class="paragraph"/>Note, while you can set these properties to new values at runtime the MultiPartRequestWrapper is created and the file handled before your action code is called. So if you want to change values you must do so before this action.
<h3 class="heading-1-1">Sample form
</h3><p class="paragraph"/><div class="code"><pre>&#60;%@ taglib uri=<span class="java&#45;quote">"webwork"</span> prefix=<span class="java&#45;quote">"ww"</span> %&#62;<p class="paragraph"/>&#60;html&#62;
  &#60;head&#62;
   &#60;title&#62;File Upload Test&#60;/title&#62;
  &#60;/head&#62;
  &#60;body&#62;
    &#60;h1&#62;File Upload&#60;/h1&#62;<p class="paragraph"/>    &#60;form action=<span class="java&#45;quote">"FileUpload.action"</span> method=<span class="java&#45;quote">"POST"</span> enctype=<span class="java&#45;quote">"multipart/form&#45;data"</span>&#62;<p class="paragraph"/>    &#60;center&#62;
      &#60;table width=<span class="java&#45;quote">"350"</span> border=<span class="java&#45;quote">"0"</span> cellpadding=<span class="java&#45;quote">"3"</span> cellspacing=<span class="java&#45;quote">"0"</span>&#62;
      &#60;tr&#62;
        &#60;td colspan=<span class="java&#45;quote">"2"</span>&#62;&#60;input type=<span class="java&#45;quote">"file"</span> name=<span class="java&#45;quote">"FileName"</span> value=<span class="java&#45;quote">"Browse..."</span> size=<span class="java&#45;quote">"50"</span>/&#62;&#60;/td&#62;
      &#60;/tr&#62;
      &#60;tr&#62;
        &#60;td colspan=<span class="java&#45;quote">"2"</span> align=<span class="java&#45;quote">"center"</span>&#62;
          &#60;input type=<span class="java&#45;quote">"submit"</span> value=<span class="java&#45;quote">"Submit"</span>&#62;
        &#60;/td&#62;
      &#60;/tr&#62;
      &#60;/table&#62;
    &#60;/center&#62;
  &#60;/form&#62;<p class="paragraph"/>&#60;/body&#62;
&#60;/html&#62;</pre></div><p class="paragraph"/>That's all you have to do to upload a file. No coding required, the file will be placed in the default directory. However, that leaves us with no error checking among other things. So let's add some code to the Action.
<h3 class="heading-1-1">FileUploadAction.java
</h3><p class="paragraph"/>Before the action method is called the dispatcher will upload the file. Then we can get access to information about the file from MultiPartRequestWrapper.
<div class="code"><pre>MultiPartRequestWrapper multiWrapper = 
		(MultiPartRequestWrapper) ServletActionContext.getRequest();</pre></div><p class="paragraph"/>The first thing you should always do is check for errors. If there were any there's no point in continuing, most methods will return null. Unfortunately, currently there is no easy way to distinguish what error occured making it more difficult to route to different error pages. (I have improving error handling for file uploads on my stack of things I'd like to do sometime).<p class="paragraph"/><div class="code"><pre><span class="java&#45;keyword">if</span> (multiWrapper.hasErrors()) &#123;
  Collection errors = multiWrapper.getErrors();
  Iterator i = errors.iterator();
  <span class="java&#45;keyword">while</span> (i.hasNext()) &#123;
    addActionError((<span class="java&#45;object">String</span>) i.next());
  &#125;
  <span class="java&#45;keyword">return</span> ERROR;
&#125;</pre></div><p class="paragraph"/>
Now get the input tag name for the uploaded file and use that to get information on the transfer. Since you can upload multiple files (just add multiple input tags) at a time getFileNames returns an Enumeration of the names.
<div class="code"><pre>Enumeration e = multiWrapper.getFileNames();<p class="paragraph"/><span class="java&#45;keyword">while</span> (e.hasMoreElements()) &#123;
   // get the value of <span class="java&#45;keyword">this</span> input tag
   <span class="java&#45;object">String</span> inputValue = (<span class="java&#45;object">String</span>) e.nextElement();<p class="paragraph"/>   // get the content type
   <span class="java&#45;object">String</span> contentType = multiWrapper.getContentType(inputValue);<p class="paragraph"/>   // get the name of the file from the input tag
   <span class="java&#45;object">String</span> fileName = multiWrapper.getFilesystemName(inputValue);<p class="paragraph"/>   // Get a File object <span class="java&#45;keyword">for</span> the uploaded File
   File file = multiWrapper.getFile(inputValue);<p class="paragraph"/>   // If it's <span class="java&#45;keyword">null</span> the upload failed
   <span class="java&#45;keyword">if</span> (file == <span class="java&#45;keyword">null</span>) &#123;
      addActionError(<span class="java&#45;quote">"Error uploading: "</span> + multiWrapper.getFilesystemName(inputValue));
   &#125;<p class="paragraph"/>   // Do additional processing/logging...
&#125;</pre></div>
<h3 class="heading-1-1">Further improvements.
</h3><p class="paragraph"/>Code above may be packed into one nice reusable component (Interceptor) that handles 90% of all typical file upload tasks. And Action does not know anything about web-app and just gets its files. Neat. See <a href="http://wiki.opensymphony.com/space/File+Upload+Interceptor">File Upload Interceptor</a>
  </div>
</body>
</html>