4

I am trying to upload a file to the server directory from client machine. I used the following codes :

FileUpload.jsp

<form:form commandName="fileUpload" action="upload.action" method="post"  enctype="multipart/form-data">
<form:label path="fileData">Upload a File</form:label> <br />
<form:input type="file"  path="fileData" />
<input type="submit" value="upload" >
</form:form>

In my Controller:

@RequestMapping("/upload.action")
public String upload(@ModelAttribute("fileUpload") FileUpload fileUpload,HttpServletResponse response,Model model)
{
    CommonsMultipartFile multipartFile = fileUpload.getFileData();
    String orginalName = multipartFile.getOriginalFilename();
    String filePath = "/my_uploads/"+orginalName;
    File destination = new File(filePath);
    String status ="success";
    try {
        multipartFile.transferTo(destination);
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        status="failure";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        status="iofailure";
    }
    model.addAttribute("status", status);
    return "home";
}

FileUpload.java :

{
   private CommonsMultipartFile fileData;
   ....
}

NullPointerException is thrown at the line String orginalName = multipartFile.getOriginalFilename(); .. what wrong thing i have done??

2 Answers 2

6

Try adding the MultipartFile as a parameter in your requesthandler.

@RequestMapping("/upload.action")
public String upload(@RequestParam(value = "file") MultipartFile file,
        HttpServletResponse response,Model model)
{
    //Controller logic...
}

This will require you to register a new bean in your dispatcher's configuration.

<bean id="multipartResolver"
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="5000000"/>
</bean>
Sign up to request clarification or add additional context in comments.

7 Comments

Exception thrown as org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface
@user1849556 see updated controller, sorry about that. It should have used @RequestParam instead of @ModelAttribute
Thank you Kevin! now the Exception is java.io.FileNotFoundException: \my_uploads\xxx.pdf (The system cannot find the path specified) .. do i have to manually create my_uploads folder?
The directory should exist whether you manually create it or programatically create it.
You need to place it in the folder specified in the system property user.dir. I usually just print out this environment variable then adjust: System.out.println(System.getProperty("user.dir"));
|
0
@RequestMapping("/upload.action")
public String upload(@RequestParam("fileData") MultipartFile file,
        HttpServletResponse response,Model model)
{
    //Controller logic...
}

you should have the same name in the parameter for your request handler method ,whatever you given in the FileUpload Pojo for multipartFile ("fileData") it should be in the parameter

Thanks,

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.