四种思路java.lang.ClassCastException:org.apache.catalina.connector.RequestFacade cannot be cast.... 2017-01-22 17:51:01

接思路一:直替换参数为需要的request,因为

public interface MultipartHttpServletRequest extends HttpServletRequest, MultipartRequest,

上一篇文章在开始,大家可能注意到和大多数博客中写的有一点不同:大多数文章是: HttpServletRequest参数,而不是MultipartRequest请求,原因就是在开始用httpServerleteRequest的时候遇到下面的堆栈错误.

  1. @RequestMapping (value= "/batch/upload" , method= RequestMethod.POST)
  2. public @ResponseBody
  3. String handleFileUpload(HttpServletRequest request){
  4. List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles( "file" );
  5. '''''//和上一篇文章类似
  6. }

检查jar包是否完整也就是上篇博客的commons-fileupload和commons-io这两个依赖

是否有配置文件,配置文件大小,上篇文件的
@Component
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory=new MultipartConfigFactory();
        factory.setMaxFileSize("128KB");
        factory.setMaxRequestSize("128KB");
        return factory.createMultipartConfig();
前端的调用时候传递的参数是否为form-data的形式:enctype="multipart/form-data"
直接在自己的代码中判断multipartResolver.isMultipart(request),从而调过程序自己的判断,然后再转成CommonsMultipartResolver 的形式,就不会产生强制转化的错误了.
    //多文件上传(也包含单文件)
    @RequestMapping(value = "/multiFile", method = RequestMethod.POST)
    @ResponseBody
    public List<String> multiFileUpload1(HttpServletRequest request, @RequestParam("orderCode") String orderCode) throws IllegalStateException, IOException {
    //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求
        if(multipartResolver.isMultipart(request)) {
            //转换成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files =  multiRequest.getFiles("file");
            return uploadUtil.multiFileUpload(files, orderCode);
        return null;
Registering type handler against HashMap causes ClassCastException. #1089
Unable to register TypeHandler once TypeHandlerRegistry.hasTypeHandler is called. #1177
Serializing and deserializing cached objects causes NullPointerException. #1084
Invalid error message 'Two methods with same method signature but not providing classes assignable?' in System.err. #929
                                    对“org.apache.catalina.connector.ClientAbortException: java.io.IOException: 断开的管道”异常的分析和处理
                                    Load canal adapter: es7 failed java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassCastException: com.alibaba.druid.pool.DruidDataSource cannot be cast to com.alibaba.druid.pool.DruidDataSource
canal-adapter1.1.5报错, 将client-adapter.es7x-1.1.5-SNAPSHOT-jar-with-dependencies.jar 替换掉release版本的plugin文件的 client-adapter.es7x-1.1.5-jar-with-dependencies.jar
                                    org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
最近尝试表单提交上传文件时强制类型转换报错,报错位置
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
首先检查了spring配置:
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
解决办法:
MultipartResolver resolver = new CommonsMultipartResolver(...
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
写错成了:
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpS..