Spring-boot: StandardMultipartFile不能转换为CommonsMultipartFile的问题

spring-boot web配置是标准配置,然后加了

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.2</version>
    </dependency>

代码如下:

    @RequestMapping(value="/uploadFile", method = RequestMethod.POST)
    public String tryone(@RequestParam("file") CommonsMultipartFile file){
    }

运行用curl

curl -F "file=@/home/temp/user.txt" "http://localhost:8080/MyApi/uploadFile"

出现错误

2017-10-19 14:48:50,281 ERROR - Failed to convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile': no matching editors or conversion strategy found [com.eversec.centertool.exception.GlobalControllerExceptionHandler.handleException(GlobalControllerExceptionHandler.java:37)] 
org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException: Failed to convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile': no matching editors or conversion strategy found
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:124)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

怎么回事?

阅读 33.1k
4 个回答

在 properties 文件添加:

application.yml

servlet:

multipart:
  enabled: true
  #location: roy-upload-tmp
  max-file-size: 100MB
  max-request-size: 100MB
 

然后替换 @RequestParam CommonsMultiPartFile 为 @RequestParam MultipartFile file

@PostMapping("/upload")

public String saveFile(@RequestParam MultipartFile file, @RequestParam String author) {
    System.out.println("osgn jsm ojmpdn");
    System.out.println(pic);
    System.out.println(author);
    return "ok";
} 

在你的项目中添加一个配置类 注册CommonsMultipartResolver
@Bean(name = "multipartResolver")

public CommonsMultipartResolver getCommonsMultipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20971520);   
    multipartResolver.setMaxInMemorySize(1048576);
    return multipartResolver;
}

如果是springboot项目在application.properties中加入multipart.enabled=true

请问这个问题最后是怎么解决的?

新手上路,请多包涵

可以在Post请求的映射方法上 将@RequestParam(“file”) MultipartFile file,改为@RequestParam MultipartFile file

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题