SourceForge.net Logo
Version: 0.27 - Last update: 2004-05-30 Home Page - SourceForge Project Page - Contact

Jodd
  Overview
  News
  Download
  License
  References

Development
  Using Jodd
  Javadoc
  JUnit report

Community
  Contribute
  Report a bug

More reading
  Milestone 0.30



Top 25%

BSD
 

Upload

MultipartRequest class handles multipart requests, i.e. file uploads. It reads http multipart request and extract both files and parameters.

Usage is simple as it can get: just create a new instance of MultipartRequest by passing current http request to its constructor:

Multipart request
 
MultipartRequest mrequest = new MultipartRequest(request);

and that's it!!! From here, a MultipartRequest object may return either parameter values either uploaded files in a similar way as it would be done for HttpServletRequest object. Here are some code snippets examples:

Using multipart request object
 
mrequest.getParameter("field");				// read form's parameter
mrequest.getParameterValues("list");			// read all selected values of form's parameter
Enumeration names = mrequest.getParameterNames();	// obtain all posted parameter names

names = mrequest.getUploadedFileNames();		// obtain all uploaded file form field names
UploadedFile uf = mrequest.getUploadedFile(paramName);	// get uploaded file

An UploadedFile object holds data about the uploaded file and may be used for retrieving various information about the uploaded content, as well as files raw data. Moreover, it contains a method for storing uploaded file data directly to a file.

Non-multipart requests

MultipartRequest can also handle common, non-multipart http requests. In this case, MultipartRequest object will hold information only for parameter values, naturally.

Form bean mapping

MultipartRequest object may be used in the very same way as HttpServletRequest to load data from request and to populate some form bean, by using jodd.bean.BeanUtil.load() method. As it is desribed here, it is very easy to load from multipart request and to populate form bean:

Population of form beans
 
UploadFormBean ufb = new UploadFormBean();
BeanUtil.load(ufb, mrequest);

And that is all! Now all request parameters will be populated into the bean. Uploaded files may be populated only into the properties of UploadedFile type.

Using encoding

MultipartRequest also can handle multipart forms that uses specific character encoding. Here is the example of the HTML form definition in this case:

Multipart form with encoding
 
<form enctype="multipart/form-data" accept-charset="encoding" ...

Next is the example of creating multipart request that supports encoding:

Multipart request with encoding
 
MultipartRequest mrequest = new MultipartRequest(request, "encoding");

Everything else stays the same.


http://jodd.sourceforge.net
najgor at users.sourceforge.net