File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Multipart parsing...Help!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multipart parsing...Help!!!!!" Watch "Multipart parsing...Help!!!!!" New topic

Multipart parsing...Help!!!!!

Raul Vieira

Joined: Feb 12, 2004
Posts: 6
Hello all

I was wondering if someone could help me with a multipart/ form data design issue.

Here's some background on my requirements.

I'm developing a new feature for an application where users are able to create employee profiles, which will consist of all of the employee's details as well as an employee image. A servlet will receive the request; create the employee image on the filesystem, and save the employee details (with the path to the image) to the database. The image is not required but if it is it will need to be of specific type (ie., jpeg, gif), size, and dimensions.

I've considered using the orielly's Multipartparser and Multipartrequest class to do the job, and while they do perform a lot of what I need they don't meet all of my design needs. The reason being that all of the submitted information will have to be validated before proceding to create the image on the file system. So, I've decided to create my own parser.

I've printed out all of the "stuff" so I now I'm able to see that I'll have to parse the "Content-Disposition: form-data" to look for both request parameters and file parts, but I'm not sure what to do with the portion that represents a file. I need to perform validation on the file part: size, dimension, etc.

Here's the way I've set my "testing" method up:

BufferedReader br= request.getReader();
LineNumberReader lr = new LineNumberReader(br);
String lineNumber = null;
PrintWriter out = response.getWriter();


while ((lineNumber = lr.readLine()) != null) {



Here's the output:

Content-Disposition: form-data; name="upload"; filename="C:\surf.jpg"
Content-Type: image/pjpeg


Content-Disposition: form-data; name="dummy"


Is there a class that I can use to pump file data into that will give me an image representation where I can later call methods like getHieght(), getWidth()? Am I living in a fantasy world?

Thanks in advance for any insight you may be able to provide me.
Atul Prabhu
Ranch Hand

Joined: Dec 17, 2002
Posts: 60

I suppose u r using file upload facility to send the image file.
I think u can try javax.mail.internet.MimeMultiPart through which you can get the InputStream of the Image at the server.After you get the Input Stream you can use the Java Advanced Imaging package

Java Advanced Imaging

Raul Vieira

Joined: Feb 12, 2004
Posts: 6
Thanks Atul I'll have a look.
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
I'd encourage you to move this to a struts environment. In this way you can use an ActionForm on the server side to receive the file. Your ActionForm will override the validate() method and do the appropriate validation. On the JSP side you can use the <html:file> tag to manage your upload.

I do recognize that if you don't already have a struts environment that this is not the best solution.

<a href="" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
I agree. Here's the link:
subject: Multipart parsing...Help!!!!!
It's not a secret anymore!