Java Reference
In-Depth Information
9.4
Working with Files
Whew! Now that we've covered all that theory about indexed properties,
let's get back to uploading files. We'll also discuss streaming them back
to the user when viewing an email that contains attachments.
Uploading Files
Uploading files is somewhat problematic in any web application due
to a few quirks in the HTTP specification. The multipart/form-data MIME
type allows submitting a request that includes one or more files, but the
parts can arrive in any order. For example, a file may be sent before the
request parameters. This means the file must be received completely
before getting the rest of the request data and having all the information
needed to process the request. 3
Because of this limitation, all the request data must be processed before
being able to set properties and call an event handler on an action bean.
To be able to give the action bean access to the uploaded files, these are
saved in a temporary directory on the server's disk. After the request
data processing is complete, the event handler is called and is given
access to the files that were saved on disk.
Fortunately, Stripes shields us from most of these nasty details. We can
add the controls for uploading a file with the <s:file> tag in a JSP and
retrieve the file in an action bean with a property of type FileBean . We
can read the file and process it, or we can just save it to a directory and
filename of your choice.
Let's see how this works by adding support for uploading attachments
on the Message Compose page. First, we add the controls in the JSP
with the <s:file> tag:
Download email_19/web/WEB-INF/jsp/message_compose.jsp
<div> Attachments: </div>
<div><s:errors field="attachments"/></div>
<div class="left">
<c:forEach var="index" begin="0" end="3">
<div><s:file name="attachments[${index}]"/></div>
</c:forEach>
</div>
<div class="left">
<s:submit name="upload" value="Upload"/>
</div>
3. See http://tools.ietf.org/html/rfc2388 for more information on the multipart/form-data MIME
type.
 
 
 
Search WWH ::




Custom Search