下面是文件接收的方法,其中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的配置文件中有这样一段代码:
<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的文件上传方式,只需要后台接收的方法这么写:
@RequestMapping(value = "imagedetectufile", method = { RequestMethod.POST }) @ResponseBody public Object detectPictureTask( @RequestParam(value = "image", required = false) MultipartFile file[], ModelMap model, HttpServletResponse response) throws IOException { //业务逻辑。。。 //............ }
亲测好使。