MultipartFile returns null every time
Asked Answered
S

3

1

I am using this code to post an image file to my controller but I always get a null value for the file body part.

@RequestMapping(value = "/updateprofile", method = RequestMethod.POST)
public @ResponseBody
ResponseMsg updateProfile(
        @RequestHeader(value = "userid", required = false) String userid,
        @RequestHeader(value = "name", required = false) String name,
        @RequestHeader(value = "phone", required = false) int phone,
        @RequestParam(value = "file", required = false) MultipartFile file) {

    ResponseMsg responseMsg = CommonUtils.checkParam(userid, name, phone,
            file);
    if (responseMsg.getStatus().equalsIgnoreCase("True"))
        responseMsg = userService.login(name, userid);
    return responseMsg;
}

Can anyone help with this?

Starstudded answered 14/3, 2013 at 11:16 Comment(1)
Why don't you populate the command bean with the file object? Which Spring framework are you using? The version 3 or lower has a problem while uploading multiple files.Camey
N
4

When you use multipart then your form fields are included in request Stream. So you have to check whether they are form fields or not.

This is what I use in a servlet, you can make appropriate changes in it to work in Spring-MVC.

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart)
        {
            try 
            {
                List items = upload.parseRequest(request);
                Iterator iterator = items.iterator();
                while (iterator.hasNext()) 
                {
                    FileItem item = (FileItem) iterator.next();

                    if (item.isFormField()) //your code for getting form fields
                    {
                        String name = item.getFieldName();
                        String value = item.getString();
                        System.out.println(name+value);
                    }

                    if (!item.isFormField()) 
                    {
                       //your code for getting multipart 
                    }
                }
            }
Nicaea answered 14/3, 2013 at 11:34 Comment(0)
D
0

firstly please post more code then we can find more, secondly I think the problem is your form. If you use Spring mvc upload file, your form should be like this: <form action="your url" method="post" enctype="multipart/form-data"> pay attention to enctype, it lets the Spring DispatchServlet know that you want to upload a file. and also you should check did you config MutilPartFileResovler in the config file.

Dorsiventral answered 14/3, 2013 at 12:38 Comment(0)
A
0

For those who are still struggling with this problem, here's what worked for me. Previously my input field was defined as,

<input type="file" />

I was getting null file with the above line but when I added the name="file" everything worked fine!

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

Hope this helps!

Aguila answered 1/3, 2016 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.