Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Submit multipart and form data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Submit multipart and form data" Watch "Submit multipart and form data" New topic
Author

Submit multipart and form data

Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
Hi All,

When I submit multipart content with the form data , it does not seem to recognize
the form data. I am trying to upload the file using jquery file upload scripts, is there any other way I can
handle it ?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

When you submit a multi-part form, the request's getParameter() family of methods cannot parse the data (it's in the wrong format). Rather, the file upload library you are using should have an API to make the values available. If you are parsing the request data yourself, instead of using a file upload library, you need to do it yourself.

See the JSP FAQ for more info.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
Thanks for the reply.

I tried using org.apache.commons.fileupload.* file upload library.

But still i am not able to retrieve the form values.

How can I parse the values myself?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Justin Howard wrote:
I tried using org.apache.commons.fileupload.* file upload library.

But still i am not able to retrieve the form values.

Why not?

How can I parse the values myself?

You can't. The library has already read the stream. Answering the above question is more important than finding work-arounds.
Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
When the form gets submit this is the code I have in the servlet.


But I everytime I submit the value of ccMails is null. ccMails is the example I gave, all the other form values are null as well.




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

As I have already explained, getParameter() will not work. You need to use the API provided by the upload library.
Justin Howard
Ranch Hand

Joined: Feb 19, 2009
Posts: 162
You were absolutely correct. I changed the code to reflect the new apis.
However now, for the file content it gives me an exception.

If I cast it (((FileItem) item).write(uploadedFile);) gives me an exception if I dont it gives an error.
Exception: org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl cannot be cast

Thanks



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I haven't looked at the API documentation myself, but I see you create a FileItemFactory and then don't do anything with it. Shouldn't you use that to create a FileItem object?

I'm not surprised you can't cast a FileItemStream object to a FileItem variable, just based on the names of the types.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Submit multipart and form data
 
Similar Threads
Unable to upload file using Commons HTTPClient and Commons FileUpload
File Upload
form to upload files
file upload using oracle pdk
How to read a input type file in servlet and display the contents of a file in servlet