private String getPartName(MethodParameter parameter) { RequestPart annot = parameter.getParameterAnnotation(RequestPart.class); String partName = (annot != null ? annot.value() : ""); if (partName.length() == 0) { partName = parameter.getParameterName(); Assert.notNull( partName, "Request part name for argument type [" + parameter.getParameterType().getName() + "] not specified, and parameter name information not found in class file either."); } return partName; }
@Override public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); assertIsMultipartRequest(servletRequest); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); String partName = getPartName(parameter); Object arg; if (MultipartFile.class.equals(parameter.getParameterType())) { Assert.notNull( multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); arg = multipartRequest.getFile(partName); } else if (isMultipartFileCollection(parameter)) { Assert.notNull( multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); arg = multipartRequest.getFiles(partName); } else if (isMultipartFileArray(parameter)) { Assert.notNull( multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); List<MultipartFile> files = multipartRequest.getFiles(partName); arg = files.toArray(new MultipartFile[files.size()]); } else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName())) { assertIsMultipartRequest(servletRequest); arg = servletRequest.getPart(partName); } else if (isPartCollection(parameter)) { assertIsMultipartRequest(servletRequest); arg = new ArrayList<Object>(servletRequest.getParts()); } else if (isPartArray(parameter)) { assertIsMultipartRequest(servletRequest); arg = RequestPartResolver.resolvePart(servletRequest); } else { try { HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName); arg = readWithMessageConverters(inputMessage, parameter, parameter.getParameterType()); WebDataBinder binder = binderFactory.createBinder(request, arg, partName); if (arg != null) { validate(binder, parameter); } mavContainer.addAttribute( BindingResult.MODEL_KEY_PREFIX + partName, binder.getBindingResult()); } catch (MissingServletRequestPartException ex) { // handled below arg = null; } } RequestPart annot = parameter.getParameterAnnotation(RequestPart.class); boolean isRequired = (annot == null || annot.required()); if (arg == null && isRequired) { throw new MissingServletRequestPartException(partName); } return arg; }