RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

我的代码

package com.etc.controller;

import com.etc.service.UpLoadService;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "/common/fileManage")
public class UpLoadController {

    @Autowired
    private UpLoadService upLoadService;

    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST)
    @ResponseBody
    public String fileUpload(HttpServletRequest request, HttpServletResponse response){
        String fileMapString = "";
        //判断enctype属性是否为multipart/form-data
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map<String,MultipartFile> fileMap = multipartRequest.getFileMap();

            fileMapString = upLoadService.uploadFileByWebUpload(fileMap);
        }
        return fileMapString;
    }
}

结果会报错:

严重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest] with root cause
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
	at com.etc.controller.UpLoadController.fileUpload(UpLoadController.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

只要将

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

改为

MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);

报错就没了

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这个错误是因为在使用Spring框架时,将一个org.apache.catalina.connector.requestfacade对象强制转换为org.springframework.web.multipart.MultipartHttpServletRequest对象时出错。可能是因为你的代码中使用了错误的类型或者没有正确地配置Spring框架。建议检查代码和配置文件,确保正确使用了Spring框架。 ### 回答2: 这个错误是因为在应用程序中使用了两个不同的servlet API,一个是Tomcat中的org.apache.catalina.connector.requestfacade,另一个是Spring框架中的org.springframework.web.multipart.MultipartHttpServletRequest。这两个API使用的是不同的类和方法来处理HTTP请求。 在代码中,当尝试将org.apache.catalina.connector.requestfacade强制转换为org.springframework.web.multipart.MultipartHttpServletRequest时,会出现这个错误。这可能是因为应用程序中同时引用了Tomcat和Spring框架,但是在代码中将它们混淆了。 要解决这个问题,首先要去除其中一个API的依赖,并且在代码中只使用一个API。如果应用程序需要使用Tomcat和Spring框架,则需要确保在代码中正确使用它们。例如,对于文件上传,需要使用Spring框架的MultipartResolver解析器。 另外,还可以尝试在代码中使用Java Servlet API提供的Part接口来处理文件上传。 Part接口可以处理文本和二进制数据,而且它适用于所有的servlet容器,包括Tomcat和其他Java EE应用程序服务器。 总之,要避免org.apache.catalina.connector.requestfacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest错误,需要仔细检查应用程序的代码,确保只使用一个servlet API,并且在处理文件上传时,正确使用相应的解析器或接口。 ### 回答3: 这个错误通常会在Spring应用程序中遇到,其中一个类被错误地强制转换为另一个类的实例。在这种情况下,org.apache.catalina.connector.requestfacade被错误地强制转换为org.springframework.web.multipart.multiparthttpservletrequest。 这个问题的解决方法是确保在使用Spring的时候,你的项目使用正确的依赖包。在这种情况下,你需要确保你在你的项目中添加了正确的Spring依赖项,以便支持文件上传。你需要添加的依赖项是spring-web和spring-webmvc。你可以通过Maven或Gradle等构建工具来将它们添加到你的项目中。 另外,你需要确保你的servlet版本与你的Spring版本兼容。如果你使用的是Tomcat服务器,则需要确保你使用的是Tomcat 7.0.33或更高版本。 最后,如果你已经添加了正确的依赖项和版本,并且仍然遇到这个问题,那么你需要检查你的代码以查看是否有其他地方不正确的强制类型转换。你可以使用调试程序来进一步排查问题。 总之,当你在使用Spring进行文件上传时,这个错误可能会出现,但你可以通过检查你的依赖项和版本、排查代码中的强制类型转换等方式来解决它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值