JavaOnTracks: File Uploading / MultiPart Form API


What is it ?


Often a web application will allow the user to upload files, unfortunately standrda Java servlets do not support handling this (multipart form).
There does not exist many free/opensource java implementation to do this.
So by using the multipart form spec i wrote my own implementation.

It does not support the whole RFC 2833 Spec, but enough to support forms with mixed regular HTML fields and mutliple file uplaod fields
JOTMultiPartParser is the main class to look at to handle this.
More details are available in the JavaDoc.

How to use / Example


Basic concept:
You need request to be of type JOTFlowRequest for thsi to work, you can do this by either:
  • If you use JavaOnTracks as your framework, your request is automatically of type JOTFlowRequest
  • Otherwise, you can “wrap” your regular HttpServletRequest in a JOTFlowRequest: JOTFlowRequest newRequest = new JOTFlowRequest(request);

Example
// parsing the request, temp file data will go into /tmp, 5MB size max.
                request.parseMultiPartContent("/tmp", 50000000);
 		String someField=request.getParameter("htmlField1");
// get one of the files (htmlFile1 is the name of the file upload HTML field)
                JOTMultiPartItem item=request.getFile("htmlFile1");
		File f=new File("/tmp",item.getFileName());
		FileOutputStream fos=new FileOutputStream(f);
                //saving the user file somewhere
		item.copyDataTo(fos);
		fos.flush();
		fos.close();


Example HTML: DO NOT FORGET enctype=“multipart/form-data”
example Form page HTML
<html>
<!-- DO NOT FORGET enctype="multipart/form-data" !! -->
<form action="?" enctype="multipart/form-data"  method="POST">
What is your name:<input type="text" name="htmlField1"><br>
Upload File(s): <input type="file" size="20" name="htmlFile1"><br>
<input type="submit">
</form>
</html>


JavaDoc


http://www.colar.net/jotdoc/javaontracks/net/jot/web/multipart/package-summary.html




Last modified: Wed Dec 10 19:04:07 EST 2008 by Thibaut Colar