2

I got this jsp file :

<form:form class="main-section" modelAttribute="demandeForm" commandName="demandeForm" 
name="demandeForm" method="post" enctype="multipart/form-data;charset=UTF-8" action="/validate.do?valider=valider">

<!-- Some input text -->

<ul>
   <li>
      <input type="file" name="file" >
   </li>
   <li>
      <input type="file" name="file" >
   </li>
   <li>
      <input type="file" name="file" >
   </li>
</ul>

Here is my form class :

public class DemandeForm {

    private MultipartFile[] file;

    public MultipartFile[] getFile() {
        return file;
    }

    public void setFile(final MultipartFile[] pFile) {
        this.file = pFile;
    }
}

And my controller :

@RequestMapping(method = RequestMethod.POST, params = "valider")
public String valider(@Valid final DemandeForm pForm, final BindingResult pResult, final Model pModel, final HttpServletRequest pRequest) {

    // do things

    if (pResult.hasErrors()) {
        return MY_VUE;
    }

    // do things

}

pResulthas binding error, and here is the error :

Failed to convert property value of type java.lang.String[] 
to required type org.springframework.web.multipart.MultipartFile[] 
for property file; nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String] to required type 
[org.springframework.web.multipart.MultipartFile] for property file[0]: 
no matching editors or conversion strategy found 

The error has appeared since I've added charse=UTF-8 on the enctype value. I need this because I have files this accents and UTF-8 characters.

How can I deal with this ?

2 Answers 2

1

Did you try with:

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="defaultEncoding"><value>utf-8</value></property>
</bean>

And don't use:

enctype="multipart/form-data;charset=UTF-8"

in your form

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

Comments

0

you can set controller parameter as

@RequestParam("file") MultipartFile file

then you can get file name,size,etc... like

file.getOriginalFilename()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.