spring boot上传文件报错:Expected MultipartHttpServletRequest

时间:2022-12-13 17:09:47
spring boot写的接口处理application/form-data类型的输入,

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/aps/mpserver", method = RequestMethod.POST)
public class FileUploader {
    private  static Logger logger = Logger.getLogger(ModelMetaInfoGetter.class);

    @RequestMapping(value = "/fileupload", method = RequestMethod.POST)
    public FileUploadResult modelPredict(@RequestParam("file") MultipartFile file)
    {
        if (!file.isEmpty()) {
            try
            {
                System.out.println(file.getName());
                //File modelFile = new File(file.getOriginalFilename());

                //BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(modelFile));
                /*out.write(file.getBytes());
                out.flush();
                out.close();*/
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        FileUploadResult fileUploadResult = new FileUploadResult();
        fileUploadResult.setCode(0);
        fileUploadResult.setMessage("文件上传接口调用成功");
        fileUploadResult.setModelFilePath("a path");

        return fileUploadResult;
    }
}

但是接口测试的时候,发现报错,错误信息如下:
2018-02-01 12:03:33.045 ERROR 10796 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?] with root cause

java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:161)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:86)
....

2 个解决方案

#1


你是不是少了@ResponseBody这个?

#2


异常信息提示 Expected MultipartHttpServletRequest: is a MultipartResolver configured?,表明是解析请求时遇到了错误,不是响应时异常(所以跟加不加@ResponseBody没关系):
判断是没有配置 CommonMutipartResolver (MultipartResolver 的实现)~

#1


你是不是少了@ResponseBody这个?

#2


异常信息提示 Expected MultipartHttpServletRequest: is a MultipartResolver configured?,表明是解析请求时遇到了错误,不是响应时异常(所以跟加不加@ResponseBody没关系):
判断是没有配置 CommonMutipartResolver (MultipartResolver 的实现)~