File Upload in Spring 3 MVC - Null Pointer Exception
11,794
Solution 1
do you need to add the enctype to the form declaration?
enctype="multipart/form-data"
Solution 2
Also make sure you have the resolver for Multipart file in your dispatcher servlet
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
Author by
user866190
Updated on July 29, 2022Comments
-
user866190 almost 2 years
I am using spring MVC 3. I have been trying to access the attributes of the uploaded file but I keep getting the following error message. I can access the other fields of the form that is posted but I can't access the uploaded file.
nullhandleForm - Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile] for property 'file': no matching editors or conversion strategy found
HTML/JSP file
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <p>${Response}</p> <h1>Upload Songs</h1> <table> <form:form action="" commandName="handleForm"> <tr> <td>Song Name :</td> <td><form:input path="songName"/></td> </tr> <tr> <td>Artist Name :</td> <td><form:input path="artistName"/></td> </tr> <tr> <td>Gendre :</td> <td><form:input path="gendre"/></td> </tr> <tr> <td>description :</td> <td><form:textarea path="description"/></td> </tr> <tr> <td>Browse File :</td> <td><form:input type="file" path="file" /></td> </tr> <tr> <td colspan="2" style="text-align: center"><input type="submit" value="submit" /></td> </tr> </form:form> </table> </body>
The form handlerclass
public class HandleForm { private String songName; private String artistName; private String gendre; private String description; private CommonsMultipartFile file; public CommonsMultipartFile getFile() { return file; } public void setFile(CommonsMultipartFile file) { this.file = file; } public String getArtistName() { return artistName; } public void setArtistName(String artistName) { this.artistName = artistName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getGendre() { return gendre; } public void setGendre(String gendre) { this.gendre = gendre; } public String getSongName() { return songName; } public void setSongName(String songName) { this.songName = songName; } }
The controller
@Controller public class AdminController{ @RequestMapping(value = "/admin", method = RequestMethod.GET) public String showAdmin(){ return "admin/index"; } @RequestMapping(value = "/admin/upload-songs", method = RequestMethod.GET) public String showContacts(Model model) { model.addAttribute(new HandleForm()); return "/admin/upload"; } @RequestMapping(value = "/admin/upload-songs", method = RequestMethod.POST) public String doForm(@ModelAttribute(value = "handleForm") HandleForm handleForm, BindingResult result, Model model){ if(result.hasErrors()){ String stringList = null; List<FieldError> errors = result.getFieldErrors(); for (FieldError error : errors ) { stringList += error.getObjectName() + " - " + error.getDefaultMessage() + "\n"; } model.addAttribute("Response", stringList); //model.addAttribute("songname", handleForm.getSongName()); works fine //model.addAttribute("filename", handleForm.getFile().getOriginalFilename()); throws an error..? } return "/admin/upload"; } }
Any help would be much appreciated. Thanks in advance