MultipartHttpServletRequest BeanInstantiationException根本原因java.lang.InstantiationException

时间:2018-07-27 05:48:42

标签: spring

我正在使用MultipartHttpServletRequest从客户端将图像上传到浏览器

因此,我下载了以下依赖项

<dependency>
    <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.3</version>
    </dependency>
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

并使用基于Java的配置添加到bean中

@Bean(name = "multipartResolver")
    public CommonsMultipartResolver createMultipartResolver() {
        CommonsMultipartResolver resolver=new CommonsMultipartResolver();
        resolver.setDefaultEncoding("utf-8");
        return resolver;
    }

这是我上传文件的请求映射方法

@RequestMapping(value = "/rest/ModifyUser", method = RequestMethod.POST)
    public ServiceResponse modifyUser(MultipartHttpServletRequest request,HttpServlet response)
    {
        Iterator<String> itr = request.getFileNames();

        String id=request.getParameter("id");
        String firstName=request.getParameter("fname");
        String lastName=request.getParameter("lname");
        String password=request.getParameter("pass");
        String email=request.getParameter("email");

        MultipartFile multipartFileImage = request.getFile(itr.next());

        try {
            File saveImage = new File("C://"+"("+id+")"+multipartFileImage.getOriginalFilename());

        }catch(Exception e) {
            e.printStackTrace();
        }
        return response(abc(user));
    }

但是当我遇到以上@RequestMapping时,我会得到

  

严重[http-nio-8080-exec-8]   org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()   路径为[/ Fiserv]的上下文中用于servlet [dispatcher]的方法   异常[请求处理失败;嵌套异常为   org.springframework.beans.BeanInstantiationException:失败   实例化[javax.servlet.http.HttpServlet]:是抽象的吗   类?;嵌套的异常是java.lang.InstantiationException],其中   根本原因java.lang.InstantiationException在   sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)     在java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)     在   org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:102)     在   org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:141)     在   org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)     在   org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:101)     在   org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)     在   org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)     在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)     在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)     在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)     在   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)处   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)     在   org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)     在   org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)     在   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)     在   org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)     在   org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)     在   org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)     在   org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)     在   org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)     在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)     在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1459)     在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

您不应使用HttpServlet作为方法参数,因为该参数现在将被解释为常规对象,Spring会尝试实例化它。 Spring创建了一个实例,因为它不是supported arguments之一。现在,它被解释为任何其他对象(表中的最后一项)。它不是简单的对象(Stringint等),因此它将尝试构造实例。创建实例将失败,因为HttpServlet并不是那样使用的。

您可能想改用HttpServletResponse。但是,由于根本没有在该方法中使用该参数,因此最好将其删除。