Apache commons -> File Upload -> parseRequest() error

◇◆丶佛笑我妖孽 提交于 2019-12-22 18:00:27

问题


Apache returns this error while trying to upload a file (I only kept the first lines of the stacktrace and root causes):

HTTP Status 500 - 
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /upload.jsp:40

    37:      
    38:        try {
    39: 
    40:            items = upload.parseRequest(request);
    41:        } catch (FileUploadException e) {
    42:            out.println(e);
    43:        }

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:451)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

root cause

javax.servlet.ServletException: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;

root cause

java.lang.NoSuchMethodError: org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(Lorg/apache/commons/fileupload/RequestContext;)Ljava/util/List;
    org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)

Here is my code:

if(ServletFileUpload.isMultipartContent(request)){
 FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;


   try {

       items = upload.parseRequest(request);
   } catch (FileUploadException e) {
       out.println(e);
   }
}

I dont get it, it looks like it can't find the parseRequest() method, but the ServletFileUpload instanciation works fine, so it seems like the package is there but...

Any idea? All suggestions help appreciated! :)


回答1:


This is indeed a sign of classpath pollution. You have different versions of the commons fileupload JAR file spreading over the classpath. You need to clean up the classpath by removing or replacing the older-versioned ones. In case of a JSP/Servlet webapplication, the default paths which are covered by the classpath are usually the Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/lib and the JRE/lib.

That said, the stacktrace also indicates that you wrote raw Java code inside JSP files using the old fashioned scriptlets. I would strongly recommend not to do so, but just to use a real Java class (in this case a Servlet) to handle the file upload.




回答2:


noSuchMethod error could be caused by a mismatch between the version of the jar you compiled against and the jar in the runtime classpath. This is something to double-check.



来源:https://stackoverflow.com/questions/2153488/apache-commons-file-upload-parserequest-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!