zoukankan      html  css  js  c++  java
  • Spring接收文件示例 并解决org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest问题

    前端代码示例

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="http://127.0.0.1:8093/eos/api/test/gotojsp.do" method="post"
            enctype="multipart/form-data">
            <select>
                <option value="0">请选择</option>
                <option value="1">店铺数据</option>
                <option value="2">产品数据</option>
    
            </select> <input type="file" name="uploadFile" />
    
            <button type="submit">提交</button>
        </form>
    </body>
    </html>

    Spring 中需要增加如下配置, 否则报错org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

        <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize">
                <value>5120000</value>
            </property>
            <property name="maxInMemorySize">
                <value>1024</value>
            </property>
        </bean>

    后端代码示例

       //亲测 如下代码可以运行成功
    @RequestMapping(value = "/gotojsp",method = RequestMethod.POST) public String gotojsp(HttpServletRequest request) { MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); MultipartHttpServletRequest params = resolver.resolveMultipart(request); System.out.println("enter");
    // 如果使用下面的方式怎会报错转换失败 不要问我为什么,我只是一个搬运工
    // MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request); MultipartFile file = ((MultipartHttpServletRequest) request).getFile("uploadFile"); if (file == null) { System.out.println("file is empty"); return "file is empty"; } List<List<String>> list = ExcelUtil.getExcelData(file); System.out.println("list==" + JSON.toJSONString(list)); // request.setAttribute("testSetAttribute", me.getName()); return "/test/testgogo.jsp"; }
  • 相关阅读:
    深入浅出之正则表达式(一)
    Windows平台下构建 Android 开发环境( Android SDK 下载及安装教程)
    Eclipse 安装配置总结
    JDK安装配置教程
    关于磁力链接(Magnet URI)的简单介绍
    C# ASP.NET里的@妙用,字符串换行
    颜色中英文对照表 CSS
    使用git 工具下载android.jar Source Code
    ASP.NET表单提交之Get和Post的区别
    深入浅出之正则表达式(二)
  • 原文地址:https://www.cnblogs.com/xiangtingsulinlin/p/9641232.html
Copyright © 2011-2022 走看看