File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to get the request parameters while the enctype is multipart/form-data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get the request parameters while the enctype is multipart/form-data" Watch "How to get the request parameters while the enctype is multipart/form-data" New topic
Author

How to get the request parameters while the enctype is multipart/form-data

Suresh Chandra sekaran
Greenhorn

Joined: Jul 29, 2009
Posts: 3
Hi All,

I have the requirement that the user can upload an image along with his other details. So, I created simple html form with enctype of multipart/form-data. I am done with my file upload part. But, when i am trying to get the other values using request.getParameter(fieldName), it is returning null. I have found another way to get the values. And i am able to retrive those values when the enctype is normal one.

I just wanted to know the reason for that. Why request.getParameter is always returning null when the enctype is multipart/form-data is null.

Thanks
Suresh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

getParameter() and its related methods will not work for a multi-part request.

Whatever upload library you are using will have mechanisms available to get the submitted data. If you are parsing the multi-part request yourself (not recommended) then you aren't doing it right if you haven't located the submitted data.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Suresh Chandra sekaran
Greenhorn

Joined: Jul 29, 2009
Posts: 3
Thanks bibeault. Actually, I am using the parse method available in upload library only. I would like to know, why getParameter() and its related methods are not getting the values from request. Can anyone explain it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The HttpServletRequest instance will only parse the body of the request if the enctype is "application/x-www-form-urlencoded", which is the default for the <form> tag.

If it went ahead and read the request body, it would no longer be available for your upload library to fetch and parse.

If you let us know which upload library you are using, perhaps someone can suggest how to use it to obtain the extra parameters.
Suresh Chandra sekaran
Greenhorn

Joined: Jul 29, 2009
Posts: 3
Thank you. I am using apache fileupload package. I could retrieve the extra parameters also using the parse method.
yang erwei
Greenhorn

Joined: Mar 05, 2012
Posts: 2
Hi, "you say you have found another way to get the values." Nowdays I met the same problem ==========Why request.getParameter is always returning null when the enctype is multipart/form-data is null. I wonder how to get the parameters , thank you!!

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

It has already been explained. When the enctype is multipart, the servlet container will not parse it (at least not prior to Servlets 3.0).

That's why the 3rd party libraries are popular. Parsing a multipart request is complicated. See the JspFaq for more info.
yang erwei
Greenhorn

Joined: Mar 05, 2012
Posts: 2
Actually ,my main attention is your another way .....You know ..........

Thanks !!!
J Srinivas rao
Greenhorn

Joined: Feb 25, 2011
Posts: 9
Use Apache Common 's File Upload
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the request parameters while the enctype is multipart/form-data