webwork/src/docs/cookbook Handling_file_uploads.html,NONE,1.1 CookBook.html,1.1,1.2
[email protected] Wed, 12 Nov 2003 20:21:53 -0800
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/opensymphony/webwork/src/docs/cookbook
In directory sc8-pr-cvs1:/tmp/cvs-serv21830/cookbook
Modified Files:
CookBook.html
Added Files:
Handling_file_uploads.html
Log Message:
Added file upload howto
Fixed bad case on graphical submit button link
--- NEW FILE: Handling_file_uploads.html ---
<html><head><title>Handling file uploads</title></head><body>
<h3 class="heading-1">File upload using WebWork
</h3><p class="paragraph"></p>Webwork comes with built in file upload support. Uploading a file is simple. If a request contains multipart content, 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"></p>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. There are two special names for this property, <b class="bold">pell</b>, and <b class="bold">cos</b>. These special properties map to the Pell multiparser or to Jason Hunter's COS multiparser respectively.</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 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"></p>If you're happy with the defaults there is no need to put any of the properties in webwork.properties. Here is the relevant porion of webwork.properties
<div class="wikicode"><pre>webwork.multipart.parser=pell
webwork.multipart.saveDir=/tmp</pre></div><p class="paragraph"></p>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"></p><div class="wikicode"><pre><%@ taglib uri=<span class="java-quote">"webwork"</span> prefix=<span class="java-quote">"ww"</span> %><p class="paragraph"></p><html>
<head>
<title>File Upload Test</title>
</head>
<body>
<h1>File Upload</h1><p class="paragraph"></p> <form action=<span class="java-quote">"FileUpload.action"</span> method=<span class="java-quote">"POST"</span> enctype=<span class="java-quote">"multipart/form-data"</span>><p class="paragraph"></p> <center>
<table width=<span class="java-quote">"350"</span> border=<span class="java-quote">"0"</span> cellpadding=<span class="java-quote">"3"</span> cellspacing=<span class="java-quote">"0"</span>>
<tr>
<td colspan=<span class="java-quote">"2"</span>><input type=<span class="java-quote">"file"</span> name=<span class="java-quote">"FileName"</span> value=<span class="java-quote">"Browse..."</span> size=<span class="java-quote">"50"</span>/></td>
</tr>
<tr>
<td colspan=<span class="java-quote">"2"</span> align=<span class="java-quote">"center"</span>>
<input type=<span class="java-quote">"submit"</span> value=<span class="java-quote">"Submit"</span>>
</td>
</tr>
</table>
</center>
</form><p class="paragraph"></p></body>
</html></pre></div><p class="paragraph"></p>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"></p>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="wikicode"><pre>MultiPartRequestWrapper multiWrapper =
(MultiPartRequestWrapper) ServletActionContext.getRequest();</pre></div><p class="paragraph"></p>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.<p class="paragraph"></p><div class="wikicode"><pre><span class="java-keyword">if</span> (multiWrapper.hasErrors()) {
Collection errors = multiWrapper.getErrors();
Iterator i = errors.iterator();
<span class="java-keyword">while</span> (i.hasNext()) {
addActionError((<span class="java-object">String</span>) i.next());
}
<span class="java-keyword">return</span> ERROR;
}</pre></div><p class="paragraph"></p>
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="wikicode"><pre>Enumeration e = multiWrapper.getFileNames();<p class="paragraph"></p><span class="java-keyword">while</span> (e.hasMoreElements()) {
// get the value of <span class="java-keyword">this</span> input tag
<span class="java-object">String</span> inputValue = (<span class="java-object">String</span>) e.nextElement();<p class="paragraph"></p> // get the content type
<span class="java-object">String</span> contentType = multiWrapper.getContentType(inputValue);<p class="paragraph"></p> // get the name of the file from the input tag
<span class="java-object">String</span> fileName = multiWrapper.getFilesystemName(inputValue);<p class="paragraph"></p> // Get a File object <span class="java-keyword">for</span> the uploaded File
File file = multiWrapper.getFile(inputValue);<p class="paragraph"></p> // If it's <span class="java-keyword">null</span> the upload failed
<span class="java-keyword">if</span> (file == <span class="java-keyword">null</span>) {
addActionError(<span class="java-quote">"Error uploading: "</span> + multiWrapper.getFilesystemName(inputValue));
}<p class="paragraph"></p> // Do additional processing/logging...
}</pre></div>
</div></body></html>
Index: CookBook.html
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/docs/cookbook/CookBook.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- CookBook.html 11 Nov 2003 18:34:46 -0000 1.1
+++ CookBook.html 13 Nov 2003 04:21:51 -0000 1.2
@@ -17,12 +17,13 @@
<li><a href="Reloading_xml_files.html">Reloading xml files</a></li>
<li><a href="Populate_Form_Bean_and_access_its_value.html">Populate Form Bean and access its value</a></li>
<li><a href="Rules_of_the_Value_Stack.html">Rules of the Value Stack</a></li>
-<li><a href="Multiple_graphical_submit_buttons.html">Multiple graphical submit buttons</a> Howto (Updated)</li>
+<li><a href="Multiple_Graphical_Submit_Buttons.html">Multiple Graphical Submit Buttons</a> Howto (Updated)</li>
<li><a href="How_to_add_retrieve_from_HttpSession.html">How to add retrieve from HttpSession</a></li>
<li><a href="webwork-blank_app.html">webwork-blank app</a></li>
<li><a href="How_to_return_a_view_with_zero_code.html">How to return a view with zero code</a> aka Andre's Action Forwarding</li>
<li><a href="Running_WebWork_on_SunONE.html">Running WebWork on SunONE</a></li>
<li><a href="Using_JSTL_seamlessly_with_WebWork.html">Using JSTL seamlessly with WebWork</a></li>
+<li><a href="Handling_file_uploads.html">Handling file uploads</a></li>
<li><a href="Using_WebWork_Components.html">Using WebWork Components</a> (Webwork 2 only)</li>
</ul>
</div></body></html>
-------------------------------------------------------
This SF.Net email sponsored by: ApacheCon 2003,
16-19 November in Las Vegas. Learn firsthand the latest
developments in Apache, PHP, Perl, XML, Java, MySQL,
WebDAV, and more! http://www.apachecon.com/