3

I'm trying to get the request parameters that are sent by a form enctype "multipart/form-data". I am using apache commons fileupload.

My code is below.

FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(req); Iterator uploadIterator = items.iterator(); while(uploadIterator.hasNext()){ FileItem uploadedItem = (FileItem) uploadIterator.next(); if (uploadedItem.isFormField()){ if (uploadedItem.getFieldName().equals("name")){ name = uploadedItem.getString(); } }else{ //Uploaded files comes here } 

The HTML code of the form is:

<form name="form" id="form" method="post" action="ServletIncluirEvento" enctype="multipart/form-data"> ... //Here comes a lot of inputs (I changed the name of the attribute because I am from Brazil and the names are in portuguese) <select size="9" id="idOpcoesSelecionadas" name="opcoesSelecionadas" multiple style="width: 100%;"> <% it = colecaoUsuarioSelecionado.iterator(); String name= ""; while (it.hasNext()) { usuario = (Usuario) it.next(); name += usuario.getName() + "/"; %> <option value="<%=usuario.getLogin()%>"> <%=usuario.getName()%> </option> <% } %></select> <input type="hidden" value="<%=name%>" name="name" /> 

Even so the parameter is coming null.

Someone knows how to solve?

Thank you in advance

6
  • Which parameter ? Please provide more details ! Commented Aug 14, 2012 at 19:41
  • I am trying to get the 'name' parameter that is passed by a input type="hidden" in the form. So when I run the code, the String name is coming null Commented Aug 14, 2012 at 19:45
  • add the form-related html code Commented Aug 14, 2012 at 19:50
  • I edited the post to put the code. Commented Aug 14, 2012 at 20:05
  • Are you doing the processing in the doPost ? is there a submit button in the form ? Commented Aug 14, 2012 at 20:06

2 Answers 2

4

Modify calling the method equals:

"name".equals(uploadedItem.getFieldName()); 

And generally speaking I would rewrite your code more clearly:

FileItemFactory factory = new DiskFileItemFactory(); FileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(req); for (FileItem uploadedItem : items) { if (uploadedItem.isFormField()) { String fieldName = uploadedItem.getFieldName(); if ("name".equals(fieldName)){ name = uploadedItem.getString(); } } else { // process file field } } 

This allows to the code become a more comprehensible. It makes no sense to call twice method getFieldName(). And use Generic. It add stability to your code by making check types at compile time. There's no need for casting in the time getting current object.

Sign up to request clarification or add additional context in comments.

8 Comments

How will that fix the parameter being null?
I guess the loop doesn't reach the desired parameter with name name. Perhaps the form contains other input fields which doesn't have attribute name. In this case the NullPointerException may be thrown due to the fact that the traversing of the list was designed incorrectly.
It's part of how the library works; it iterates over the fields and you can check for the field name. It doesn't matter if there are other fields with a name of something other than "name", the loop would just skip over it (unless there's other code not shown, of course).
Hmm, I think you said that. In any case, I'm not sure the OP is getting an NPE, rather, the value of the field is null.
This loop would just skip over item only when the method equals doesn't throw an exception.
|
0

To resolve the problem, I redid the jsp page with a tag form enctype "multipart/form-data" for the images that I must upload and, for the other data, a normal form, wich I can get the request parameters normally.

I also did a refactoring to improve the logic.

Thanks for everyone for the tips.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.