views:

302

answers:

2

I have a JSP/HTML form where there are 2 elements.One is a select dropdown and another is a File upload box(input type="file").I use POST method and enctype as form-multipart.Now I am able to access both the dropdown list and file using MultipartRequest object.No problem in that.

But when I don't upload any file and when I use a code in the reciecing form like

MultipartRequest multipartRequest = new MultipartRequest(request,".",5*1024*1024);
String dummySelect= (String) multipartRequest.getParameter("dummy");
out.println("<BR>select is "+dummySelect);
Enumeration files = multipartRequest.getFileNames();

Now ideally if I don't upload any file I should get an empty enumerator.Meaning

while(files.hasMoreElements()) should evaluate to false which is not happening.Can anyone tell me why? This results in an nullPointerException.

A: 

Please make sure that the content type you send is (exactly) one of the two

  • application/x-www-form-urlencoded
  • multipart/form-data

According to the API you linked, the class handles only the latter type.

Alternatively, you can try the Apache commons FileUpload library.

David Rabinowitz
I was originally using FileUpload.But faced lot of issues so moved to MultiPartRequest.
A: 

Just look on the javadoc for the getFileNames() method - it says, that in case of any file input left empty on the page, the result is null, empty enumeration is returned only when there are no file inputs at all on the page. Don't know why it's implemented this strange, but the documentation says so...

EDIT: I think I got it all wrong - there should be something in the enumeration even for file input that's left empty (or should you get empty enum in that case...don't know, the doc isn't very clear to me).

schmeedy