public void upload() throws Exception { MultipartRequest multipartRequest = (MultipartRequest) this.request; String currRelativeFolder = this.getFolder(this.relativeFolder); FileUtils.forceMkdir(new File(this.savePath + currRelativeFolder)); MultipartFile multipartFile = multipartRequest.getFile("upfile"); if (multipartFile == null) { this.state = ERROR_INFO.get("NOFILE"); return; } if (multipartFile.getSize() > this.maxSize) { this.state = ERROR_INFO.get("SIZE"); return; } this.originalName = multipartFile.getOriginalFilename(); if (!this.checkFileType(this.originalName)) { this.state = ERROR_INFO.get("TYPE"); return; } this.fileName = this.getName(this.originalName); this.type = this.getFileExt(this.originalName); this.url = currRelativeFolder + "/" + fileName; FileUtils.copyInputStreamToFile( multipartFile.getInputStream(), new File(savePath + currRelativeFolder + "/" + this.fileName)); this.state = ERROR_INFO.get("SUCCESS"); // UE中只会处理单张上传,完成后即退出 }
@SuppressWarnings("unchecked") private static Object resolveArray( Invocation inv, ParamMetaData metaData, Class<?> compnentType) { if (compnentType == MultipartFile.class) { String filterName = metaData.getParamName(); if (filterName == null) { filterName = ""; } if (inv.getRequest() instanceof MultipartRequest) { List<MultipartFile> files = new LinkedList<MultipartFile>(); MultipartRequest multipartRequest = (MultipartRequest) inv.getRequest(); Iterator<String> names = multipartRequest.getFileNames(); while (names.hasNext()) { String name = names.next(); if (name.startsWith(filterName)) { files.add(multipartRequest.getFile(name)); } } return files.toArray(new MultipartFile[0]); } else { if (logger.isDebugEnabled()) { logger.debug( "cann't " + "set MultipartFile param to method " + ", the request is not a MultipartRequest"); } } } else { Object toConvert = null; for (String paramName : metaData.getParamNames()) { if (paramName != null) { toConvert = inv.getRequest().getParameterValues(paramName); if (toConvert != null) { break; } } } if (toConvert != null) { if (((String[]) toConvert).length == 1) { toConvert = ((String[]) toConvert)[0].split(","); } Class<?> arrayType; if (metaData.getParamType().isArray()) { arrayType = metaData.getParamType(); } else { arrayType = arrayTypeMap.get(compnentType); if (arrayType == null) { arrayType = Array.newInstance((Class<?>) compnentType, 0).getClass(); } } TypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter(); Object array = typeConverter.convertIfNecessary(toConvert, arrayType); return array; } } return Array.newInstance((Class<?>) compnentType, 0); }
@Override public ModelMap handleMultipart( Do tempDo, ModelMap modelMap, HttpServletRequest request, MultipartRequest multipartRequest) throws Exception { String introductionId = request.getParameter("master.id"); MultipartFile multipartFile = multipartRequest.getFile("picurl"); XSaveOrUpdate xSaveOrUpdate = new XSaveOrUpdate(tempDo.getName(), request); HashMap<String, Object> paramMap = xSaveOrUpdate.getParamMap(); if (multipartFile.getOriginalFilename() != null && !multipartFile.getOriginalFilename().equals("")) { String url = "work/" + introductionId + "/" + multipartFile.getOriginalFilename(); aliOssUploadManager.uploadFile(multipartFile, "tenant", url); paramMap.put("pictureUrl", url); } paramMap.put("master.id", introductionId); Object object = baseManager.saveOrUpdate(xSaveOrUpdate); // 介绍 String introductionContent = request.getParameter("productDescription"); ProductDescription productDescription = new ProductDescription(); productDescription.setContent(introductionContent); Product product = new Product(); product.setId(((MasterWork) object).getId()); productDescription.setProduct(product); baseManager.saveOrUpdate(ProductDescription.class.getName(), productDescription); ((MasterWork) object).setProductDescription(productDescription); baseManager.saveOrUpdate(MasterWork.class.getName(), object); List<String> tagValueList = new ArrayList<>(); Enumeration<String> paramEnumeration = request.getParameterNames(); while (paramEnumeration.hasMoreElements()) { String paramKey = paramEnumeration.nextElement(); if (paramKey.startsWith("tag")) { String value = request.getParameter(paramKey); tagValueList.add(value); } } if (tagValueList.size() > 0) { for (String tagId : tagValueList) { ProjectTag projectTag = new ProjectTag(); projectTag.setId(tagId); MasterWorkTag masterWorkTag = new MasterWorkTag(); masterWorkTag.setProjectTag(projectTag); masterWorkTag.setMasterWork((MasterWork) object); masterWorkTag.setStatus("1"); baseManager.saveOrUpdate(MasterWorkTag.class.getName(), masterWorkTag); } } modelMap.put("object", object); return modelMap; }
@RequestMapping(value = "/add", method = RequestMethod.POST) public String postAddProblemFrom(MultipartRequest request) { List<MultipartFile> problems = request.getMultiFileMap().get("problems"); if (problems != null && !problems.isEmpty()) { for (MultipartFile file : problems) { problemUploadHelper.uploadProblem(file); } } return "redirect:list"; }
@SuppressWarnings("unchecked") private Object resolveRequestParam( String paramName, boolean required, String defaultValue, MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall) throws Exception { Class<?> paramType = methodParam.getParameterType(); if (Map.class.isAssignableFrom(paramType) && paramName.length() == 0) { return resolveRequestParamMap((Class<? extends Map<?, ?>>) paramType, webRequest); } if (paramName.length() == 0) { paramName = getRequiredParameterName(methodParam); } Object paramValue = null; MultipartRequest multipartRequest = webRequest.getNativeRequest(MultipartRequest.class); if (multipartRequest != null) { List<MultipartFile> files = multipartRequest.getFiles(paramName); if (!files.isEmpty()) { paramValue = (files.size() == 1 ? files.get(0) : files); } } if (paramValue == null) { String[] paramValues = webRequest.getParameterValues(paramName); if (paramValues != null) { paramValue = (paramValues.length == 1 ? paramValues[0] : paramValues); } } if (paramValue == null) { if (defaultValue != null) { paramValue = resolveDefaultValue(defaultValue); } else if (required) { raiseMissingParameterException(paramName, paramType); } paramValue = checkValue(paramName, paramValue, paramType); } WebDataBinder binder = createBinder(webRequest, null, paramName); initBinder(handlerForInitBinderCall, paramName, binder, webRequest); return binder.convertIfNecessary(paramValue, paramType, methodParam); }
public MultipartFile resolve(Invocation inv, ParamMetaData metaData) { MultipartFile multipartFile = null; if (inv.getRequest() instanceof MultipartRequest) { MultipartRequest multipartRequest = (MultipartRequest) inv.getRequest(); String fileName = metaData.getParamName(); if (StringUtils.isBlank(fileName)) { @SuppressWarnings("unchecked") Iterator<String> allFileNames = multipartRequest.getFileNames(); if (allFileNames.hasNext()) { fileName = allFileNames.next(); } } if (StringUtils.isNotBlank(fileName)) { multipartFile = multipartRequest.getFile(fileName); } if (multipartFile == null) { if (StringUtils.isNotBlank(fileName)) { if (logger.isDebugEnabled()) { logger.debug( "not found multipartFile named " + fileName + " in this request: " + inv.getRequestPath().getUri()); } } else { if (logger.isDebugEnabled()) { logger.debug( "not found MultipartFile named:" + metaData.getParamName() + " in this request: " + inv.getRequestPath().getUri()); } } } } else { if (logger.isDebugEnabled()) { logger.debug( "cann't set MultipartFile param to method " + ", the request is not a MultipartRequest"); } } return multipartFile; }
private ServletRequest prepareServletRequest( Object target, NativeWebRequest request, MethodParameter parameter) { String modelPrefixName = parameter.getParameterAnnotation(FormModel.class).value(); HttpServletRequest nativeRequest = (HttpServletRequest) request.getNativeRequest(); MultipartRequest multipartRequest = WebUtils.getNativeRequest(nativeRequest, MultipartRequest.class); MockHttpServletRequest mockRequest = null; if (multipartRequest != null) { MockMultipartHttpServletRequest mockMultipartRequest = new MockMultipartHttpServletRequest(); for (MultipartFile file : multipartRequest.getFileMap().values()) { mockMultipartRequest.addFile( new MultipartFileWrapper(getNewParameterName(file.getName(), modelPrefixName), file)); } mockRequest = mockMultipartRequest; } else { mockRequest = new MockHttpServletRequest(); } for (Entry<String, String> entry : getUriTemplateVariables(request).entrySet()) { String parameterName = entry.getKey(); String value = entry.getValue(); if (isFormModelAttribute(parameterName, modelPrefixName)) { mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value); } } for (Object parameterEntry : nativeRequest.getParameterMap().entrySet()) { Entry<String, String[]> entry = (Entry<String, String[]>) parameterEntry; String parameterName = entry.getKey(); String[] value = entry.getValue(); if (isFormModelAttribute(parameterName, modelPrefixName)) { mockRequest.setParameter(getNewParameterName(parameterName, modelPrefixName), value); } } return mockRequest; }