zoukankan      html  css  js  c++  java
  • SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题

    下面是文件接收的方法,其中List<FileItem> items为空,获取不到上传文件。

     
    @RequestMapping(value = "/imagedetect", method = { RequestMethod.POST})
    @ResponseBody
    public Object imagedetect(ModelMap model, HttpServletResponse response) throws IOException {
            logger.info("检测任务开始====" + DateUtil.getCurrentTime());
            HttpServletRequest request=CommonUtil.getRequest();
            String access_token = request.getParameter("access_token");
            String url = request.getParameter("image");
            String tag = request.getParameter("tag");
            Object obj = new Object();
            //判断该请求是否有上传文件
            if(ServletFileUpload.isMultipartContent(request)) 
            {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                try {
                    List<FileItem> items = upload.parseRequest(request);
                    for(FileItem item: items)
                    {
                                    }
                } catch (FileUploadException e) {
                    e.printStackTrace();
                }
                        }else{
                
            }
    }
     

    经过查找资料,分析后,发现在springmvc的配置文件中有这样一段代码:

    1
    2
    3
    4
    5
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="UTF-8" />
    <property name="maxUploadSize" value="104857600"/>
    <property name="maxInMemorySize" value="4096"/>
    </bean>

     这两种分别是两种上传文件的设置方式,但是不能同时使用。切记。

    把这个配置删除测试发现问题解决。

    如果你想使用springmvc的文件上传方式,只需要后台接收的方法这么写:

    1
    2
    3
    4
    5
    6
    7
    8
    @RequestMapping(value = "imagedetectufile", method = { RequestMethod.POST })
        @ResponseBody
        public Object detectPictureTask(
                @RequestParam(value = "image", required = false) MultipartFile file[],
                ModelMap model, HttpServletResponse response) throws IOException {
                    //业务逻辑。。。
                    //............      
    }

    亲测好使。

  • 相关阅读:
    常用模块(time,os,sys,collections,random,序列化模块,re)
    python-函数篇
    内置函数——filter和map
    python杂七杂八的用法
    计算机硬件
    操作系统简介
    Django入门
    linux下查看cpu物理个数和逻辑个数
    python反射的妙用
    Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
  • 原文地址:https://www.cnblogs.com/tanzq/p/8798710.html
Copyright © 2011-2022 走看看