aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multipart parsing...Help!!!!!" Watch "Multipart parsing...Help!!!!!" New topic
Author

Multipart parsing...Help!!!!!

Raul Vieira
Greenhorn

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();

out.println("<table>");

while ((lineNumber = lr.readLine()) != null) {
out.println("<tr>");
out.println("<td>");
out.println(lineNumber);
out.println("</td>");
out.println("</tr>");


}

out.println("</table>");

Here's the output:

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

�THIS WOULD REPRESENT THE FILE PART (not shown)�



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

test
-----------------------------7d4155f1c02a4--

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
Hi,

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

--------
A T U L
Raul Vieira
Greenhorn

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="http://forums.hotjoe.com/forums/list.page" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multipart parsing...Help!!!!!