Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload in Spring 3 MVC - Null Pointer Exception

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

like image 533
user866190 Avatar asked Feb 13 '12 15:02

user866190


People also ask

How would you handle the situation where a user uploads a very large file through a form in your Spring MVC application?

A new attribute "maxSwallowSize" is the key to deal this situation. It should happen when you upload a file which is larger than 2M. Because the 2M is the default value of this new attribute .

In which case the NullPointerException will be thrown?

NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object. Accessing or modifying a null object's field.

What is the use of MultipartResolver?

Interface MultipartResolver. A strategy interface for multipart file upload resolution in accordance with RFC 1867. Implementations are typically usable both within an application context and standalone.


2 Answers

do you need to add the enctype to the form declaration?

enctype="multipart/form-data"
like image 135
digitaljoel Avatar answered Oct 07 '22 18:10

digitaljoel


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>
like image 38
gauravmunjal Avatar answered Oct 07 '22 16:10

gauravmunjal