@MultipartConfig在web.xml中覆盖

时间:2013-10-02 19:44:37

标签: servlets web.xml multipartconfig

所以我有这个servlet:

    @WebServlet(name = "StudentRegistrationUsn", urlPatterns = {"/university/student/registration"})
@MultipartConfig(maxFileSize = 10*1024*1024,maxRequestSize = 20*1024*1024,fileSizeThreshold = 5*1024*1024)
public class ActionRegistrationServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //handle file upload
    }

一切正常,文件正在上传。 然后我尝试覆盖web.xml中的fileSize和Threshold:

    <servlet>
        <servlet-name>StudentRegistrationUsn</servlet-name>
        <multipart-config>
            <max-file-size>10485760</max-file-size>
            <max-request-size>20971520</max-request-size>
            <file-size-threshold>5242880</file-size-threshold>
        </multipart-config>
    </servlet>

当我这样做时,tomcat崩溃,每当我尝试访问该servlet时,它都会出现以下异常:

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

exception

java.lang.NullPointerException
    sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:461)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1813)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:722)

1 个答案:

答案 0 :(得分:15)

<servlet-class>中缺少web.xml。这个异常基本上是因为要加载的类的名称是null

实际上,您需要重新定义所有内容,包括URL模式。

<servlet>
    <servlet-name>StudentRegistrationUsn</servlet-name>
    <servlet-class>com.example.StudentRegistrationUsn</servlet-class>
    <multipart-config>
        <max-file-size>10485760</max-file-size>
        <max-request-size>20971520</max-request-size>
        <file-size-threshold>5242880</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>StudentRegistrationUsn</servlet-name>
    <url-pattern>/university/student/registration</url-pattern>
</servlet-mapping>