Home
Minggo
Recent Entries 

Advertisement

Customize
28th-Feb-2006 02:42 pm - The Jenkov file uploader handler
HTTP Multipart - Introduction

With HTTP Multipart uploading files to a Java Web app is quite easy. All you need to do is:

  • Download the compiled classes for HTTP Multipart and put them in your web app's WEB-INF/lib directory.
  • Configure the MultipartFilter (a servlet filter), in your web app's web.xml.
Configuring the MultipartFilter is done by adding these lines to your web app's web.xml:

<filter>   <filter-name>multipartFilter</filter-name>   <filter-class>     com.jenkov.servlet.multipart.MultipartFilter   </filter-class> </filter> <filter-mapping>   <filter-name>multipartFilter</filter-name>   <url-pattern>*</url-pattern> </filter-mapping>

Being a servlet filter the MultipartFilter works transparently with both servlets and JSP's, and thus also with other Java web frameworks like Spring and Struts.

All http requests to the web app will go through the MultipartFilter first. All normal (non-file-upload) requests pass through the filter untouched. All parameters are available via the request.getParameter(paramName) as usual, no matter if the request is a file upload or a normal request.

If the request is a file upload request (the form has enctype="multipart/form-data" or enctype="multipart/mixed") then the files will be received transparently and stored in a temp folder on the server. A java.util.Map of MultipartEntry instances will be attached to the request under the key "multipart.entries".

Example When uploading two files by submitting this form

<form method="post" enctype="multipart/form-data" >   <input type="text" name="comment"/>   <input type="file" name="file1" />   <input type="file" name="file2" />   <input type="submit" value="Upload" /> </form>

... the text field named "comment" can be read like this:

String comment =     request.getParameter("comment");

... and the uploaded files can be accessed like this, inside either a servlet, JSP, Struts action, Spring Controller etc. :

Map multipartEntries = (Map)     request.getAttribute("multipart.entries"); MultipartEntry file1 = (MultipartEntry)     multipartEntries.get("file1"); MultipartEntry file2 = (MultipartEntry)     multipartEntries.get("file2"); FileInputStream fileInput1 =     new FileInputStream(file1.getTempFile()); //...do stuff with the file fileInput1.close(); FileInputStream fileInput2 =     new FileInputStream(file2.getTempFile()); //...do stuff with the file fileInput2.close();

You do not have to delete the temp-files. The MultipartFilter does that automatically after your servlet, JSP, Struts action, Spring controller etc. has finished.

The MultiparEntry objects also have the following information available:

entry.getFileName()The file name as sent by the browser
entry.getFileNameOnly()The file name without directories. Internet Explorer sends full file path for file name. This methods cuts the directories off
entry.getTempFileName() The name of the temp file where the file is stored temporarily.
entry.getTempFile()The java.io.File instance pointing to the temp file. The File.length() can tell you the length of the file.

------------------------------------ link

Advertisement

Customize
This page was loaded Dec 7th 2009, 4:15 pm GMT.