user3469203
user3469203

Reputation: 547

Spring MVC - Multipart form data

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 ?

Upvotes: 2

Views: 8784

Answers (2)

QHuy
QHuy

Reputation: 51

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

Upvotes: 1

yashan
yashan

Reputation: 177

you can set controller parameter as

@RequestParam("file") MultipartFile file

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

file.getOriginalFilename()

Upvotes: 0

Related Questions