MultipartFile每次都返回null [英] MultipartFile returns null every time

查看:1603
本文介绍了MultipartFile每次都返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码将图像文件发布到我的控制器,但我总是得到文件正文部分的空值。

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;
}

任何人都可以帮忙吗?

推荐答案

当您使用 multipart 时,您的表单字段包含在请求流。所以你必须检查它们是否是表单字段

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.

这是我在servlet中使用的,您可以对其进行适当的更改,以便在 Spring-MVC 中工作。

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 
                    }
                }
            }

这篇关于MultipartFile每次都返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆