【问题标题】:Spring MVC Multi-part File - NoSuchMethodErrorSpring MVC 多部分文件 - NoSuchMethodError
【发布时间】:2013-12-11 22:46:42
【问题描述】:

我正在尝试使用 Spring 3 MVC 上传一些文件,但我总是遇到同样的异常:

java.lang.NoSuchMethodError: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:155)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:138)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:904)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:747)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我在依赖项中找到了这个方法:ServletFileUpload.parseRequest,所以它存在。我读到可能有一些其他库,例如在 Tomcat 的 lib 文件夹中,但我找不到任何东西。

这是我的控制器:

@Controller
@RequestMapping("/file")
public class FilesController {


@Autowired
EventFileUploadValidator eventValidator;

@Autowired
Configuration config;

@InitBinder("fileupload")
public void initEventFileBinde(WebDataBinder bind){
    bind.setValidator(eventValidator);
}

@RequestMapping(value = "/addEventFile", method= RequestMethod.POST)
public String uploadEventFile(@ModelAttribute("fileupload") FileUploadModel file, BindingResult results, HttpServletRequest request, HttpServletResponse response,  Model model, HttpSession session) throws IOException, NoConfigurationFoundException{

    if(!results.hasErrors()){
        MultipartFile uplodadFile = file.getFile();
        if(uplodadFile != null){

           // String path = config.getProperty("upload_file_directory").toString();
           // String filename = uplodadFile.getName();
           // uplodadFile.transferTo(new File(path+"/"+filename));

        }
    }

    return ViewHelper.EventsListView;
}


}

POM 中的依赖关系:

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-asm</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument-tomcat</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.2.RELEASE</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-struts</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.5.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.3.2.GA</version>
    </dependency>
    <dependency>
        <groupId>javax.sql</groupId>
        <artifactId>jdbc-stdext</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.0.1B</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>1.0.1.GA</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>
    <dependency>                
        <groupId>javax.validation</groupId>             
        <artifactId>validation-api</artifactId>             
        <version>1.0.0.GA</version>         
    </dependency>           
    <dependency>                
        <groupId>org.hibernate</groupId>                
        <artifactId>hibernate-validator</artifactId>                
        <version>4.3.0.Final</version>          
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>  
        <groupId>commons-io</groupId>  
        <artifactId>commons-io</artifactId>  
        <version>1.4</version>  
    </dependency>  
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

我不知道该怎么办...我在 Windows 上使用 Tomcat 7.0.42

【问题讨论】:

  • 您认为您可以运行mvn dependency:tree 命令并发布结果。这将更容易确定是否存在 commons-fileupload 的冲突版本
  • 看起来像是一些不兼容的公共文件上传版本
  • java.lang.NoSuchMethodError 是版本不匹配的典型案例。检查maven中的依赖树可能会得到一个清晰的图片
  • 我查过了。没有冲突。

标签: spring file tomcat tomcat7 nosuchmethoderror


【解决方案1】:

我发现问题出在哪里。当 maven 为部署构建目标路径时,它不会删除旧的库,但只会添加最新的库,所以当我解决了所有冲突(我更改了版本)时,Maven 使用新库的版本添加新的 jar 文件。我删除了不需要的文件,一切正常。

那些文件在 WEB-INF\target\lib 目录中。

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 2013-02-10
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2015-01-13
    • 2014-07-11
    相关资源
    最近更新 更多