0

I had an issue while working,

Let's say I had to get a client's file uploaded, so I was using

<form action='UploadFile.jsp' method='post' enctype='multiform/form-data'>
  <input type='file' name='data' id='filechooser'>
</form>

Things were fine till I needed to add some more details to the form

now the form contained 2 more input tags,

<form action='UploadFile.jsp' method='post' enctype='multiform/form-data'>
  <input type='file' name='data' id='filechooser'>
  <input type='number' name='no_of_days'/>
  <input type='date' name='dat'/>
</form>

Now the other two elements are showing null when i access them using request.getParameter() method.

I was using UploadFile from this tutorial

What I have tried?

I checked this question that was not similar but had a seemingly correct answer and tried the answer but i keep getting NullReferenceException for this line

BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));

This line of code is the part of answer in the above linked question

Community
  • 1
  • 1
nj-ath
  • 3,028
  • 2
  • 25
  • 41
  • http://stackoverflow.com/questions/3337056/convenient-way-to-parse-incoming-multipart-form-data-parameters-in-a-servlet check this possibly the duplicate – Susheel Singh May 20 '14 at 07:15

1 Answers1

1

If you're using the Commons library (which I believe you are because of the link you've mentioned in your question) - You can get the simple parameters simply:

the FileItem class has a method called isFormField which will return true if this Item represents a simple form field. Once you know a specific FileItem is a simple form field, you can use getFieldName and getString to get the name/value for this field.

Basically, if you used the code from the example you've mentioned, you can do the following:

if (!fi.isFormField ()) {
    // Get the uploaded file parameters
    ..... code for file field ....
} else {
    //it's a simple form field - get it's name and value and do whatever you need    
    String fieldName = fi.getFieldName();
    String fieldValue = fi.getString();
}
Susheel Singh
  • 3,824
  • 5
  • 31
  • 66
Shay Elkayam
  • 4,128
  • 1
  • 22
  • 19