• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Suresh Chandra sekaran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Suresh Chandra sekaran
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I am using apache fileupload package. I could retrieve the extra parameters also using the parse method.
 
yang erwei
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually ,my main attention is your another way .....You know ..........

Thanks !!!
 
J Srinivas rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Apache Common 's File Upload
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic