Apache commons - >文件上传 - > parseRequest()错误

时间:2010-01-28 09:35:51

标签: jsp upload apache-commons

Apache在尝试上传文件时返回此错误(我只保留了堆栈跟踪的第一行和根本原因):

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)

这是我的代码:

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

我没有得到它,看起来它找不到parseRequest()方法,但是ServletFileUpload instanciation工作正常,所以看起来包就在那里但是......

有什么想法吗? 所有建议都有帮助! :)

2 个答案:

答案 0 :(得分:6)

这确实是阶级污染的标志。您有不同版本的commons fileupload JAR文件遍布类路径。您需要通过删除或替换旧版本的类路径来清理类路径。对于JSP / Servlet Web应用程序,类路径覆盖的默认路径通常是Webapp/WEB-INF/libWebapp/WEB-INF/classesAppserver/libJRE/lib

也就是说,堆栈跟踪还表明您使用old fashioned scriptlets在JSP文件中编写了原始Java代码。我强烈建议不要这样做,而只是使用真正的Java类(在本例中为Servlet)来处理文件上传。

答案 1 :(得分:0)

noSuchMethod错误可能是由您编译的jar版本与运行时类路径中的jar不匹配引起的。这是需要仔细检查的。