I have a form. it has file uploading part as well as several input Fields. i am using request.getParameter(
) to get values from the jsp into the servlet.
But when i add encrypt=multipart
, request.get parameter doesn't work. it returns null. i know multipart does not support for the request.getParameter()
. Is there any solution for upload files. I want to use request.get parameter also.

- 67,789
- 12
- 98
- 136

- 141
- 2
- 2
- 6
4 Answers
apache commons library will be useful for such requirement.
refer: http://javakart.blogspot.in/2012/11/file-upload-example-using-servlet.html http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//this will help you identify request is of type multipart or not.
once you check, parse the request and get the form fields and File Item using library.
Example:
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
// ... (do your job here)
} else {
// Process form file field (input type="file").
String fieldname = item.getFieldName();
String filename = FilenameUtils.getName(item.getName());
InputStream filecontent = item.getInputStream();
// ... (do your job here)
}
}

- 6,047
- 13
- 49
- 81
-
yes. i am using that. but when request.getparameter() doesnt work. – user2599229 Jul 30 '13 at 04:36
-
I have updated my answer, try to do like that. – Jayesh Jul 30 '13 at 05:04
-
i want to use request.getparameter also. so i use two servlets. one servlet for get all the text feild vales using request.getparameter. and aother servlet for handle file upload. now my jsp page contains two forms. but the problem is i want to use one submit button. when click on the submit button form data goes to the database. when click on the upload button files are save in the folder. how can i do this using one button?? – user2599229 Jul 30 '13 at 05:37
-
I am not getting why you are doing like that, one form is enough. I posted example code, where if (item.isFormField()) { here you will get fields which are of form and } else { here you will get your uploaded data } , what is your issue now. Why you want to use request.getparameter only, I think your purpose is to read form data and uploaded file, so code i posted will do both stuff. please try it once and understand the code instead of taking two forms. – Jayesh Jul 30 '13 at 05:41
-
iam getting text feild values like this String event_name = request.getParameter("event_name");String where = request.getParameter("where"); then iam checking mbuttons name using this if (request.getParameter("btn").trim().equals("Submit")) {//code } i dont know how to handle this using above example. that is why iam try to use two forms. – user2599229 Jul 30 '13 at 05:59
-
really can't understand you, have you read the code which I posted? where it is told to read like request.getParameter("where") this. you have this code and here you will get the same thing String fieldname = item.getFieldName(); String fieldvalue = item.getString(); . try printing fieldName and fieldValue once using System.out.println(fieldName + "=" + fieldValue ); and see you are getting any thing. – Jayesh Jul 30 '13 at 06:14
-
I have multiple submit buttons.ex ..That's why I have to use..request.getParameter("btn").trim().equals("Submit"){do this},request.getParameter("btn").trim().equals("Save as Draft"){do this}..So how can I handle this? – user2599229 Aug 02 '13 at 04:24
-
How can I check weather which value type of submit button send the request? – user2599229 Aug 02 '13 at 04:32
-
I got java.io.FileNotFoundException: when there is no file to upload.How to handle when there is no file to upload? – user2599229 Aug 02 '13 at 06:33
-
@user2599229 with `isMultipart` variable you have to decide whether to use code given in answer or not. – Amey Jadiye Mar 28 '17 at 14:00
request.getParameter()
and its related methods do not work with multi-part requests, and will always return null when dealing with multipart form data.
If you want to use request.getParameter()
then you can use commons FileUpload.

- 7,636
- 5
- 37
- 49
Annotate your servlet with @MultipartConfig
, then use the getParts()
method to access the parts. You are using Servlet 3.0, right?

- 265,237
- 58
- 395
- 493
When you use multipart then your form fields are included in request Stream. So you have to check whether they are form fields or not. Please see this answer.
-
I had to send post parameters with image as body using multipart form data.. could you please guide me for same.. – karan421 Dec 10 '16 at 20:19