Search in sources :

Example 36 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class WebDataBinder method bindMultipart.

/**
	 * Bind all multipart files contained in the given request, if any
	 * (in case of a multipart request).
	 * <p>Multipart files will only be added to the property values if they
	 * are not empty or if we're configured to bind empty multipart files too.
	 * @param multipartFiles Map of field name String to MultipartFile object
	 * @param mpvs the property values to be bound (can be modified)
	 * @see org.springframework.web.multipart.MultipartFile
	 * @see #setBindEmptyMultipartFiles
	 */
protected void bindMultipart(Map<String, List<MultipartFile>> multipartFiles, MutablePropertyValues mpvs) {
    for (Map.Entry<String, List<MultipartFile>> entry : multipartFiles.entrySet()) {
        String key = entry.getKey();
        List<MultipartFile> values = entry.getValue();
        if (values.size() == 1) {
            MultipartFile value = values.get(0);
            if (isBindEmptyMultipartFiles() || !value.isEmpty()) {
                mpvs.add(key, value);
            }
        } else {
            mpvs.add(key, values);
        }
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) List(java.util.List) Map(java.util.Map)

Example 37 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class MockMultipartHttpServletRequestTests method doTestMultipartHttpServletRequest.

private void doTestMultipartHttpServletRequest(MultipartHttpServletRequest request) throws IOException {
    Set<String> fileNames = new HashSet<>();
    Iterator<String> fileIter = request.getFileNames();
    while (fileIter.hasNext()) {
        fileNames.add(fileIter.next());
    }
    assertEquals(2, fileNames.size());
    assertTrue(fileNames.contains("file1"));
    assertTrue(fileNames.contains("file2"));
    MultipartFile file1 = request.getFile("file1");
    MultipartFile file2 = request.getFile("file2");
    Map<String, MultipartFile> fileMap = request.getFileMap();
    List<String> fileMapKeys = new LinkedList<>(fileMap.keySet());
    assertEquals(2, fileMapKeys.size());
    assertEquals(file1, fileMap.get("file1"));
    assertEquals(file2, fileMap.get("file2"));
    assertEquals("file1", file1.getName());
    assertEquals("", file1.getOriginalFilename());
    assertNull(file1.getContentType());
    assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), file1.getBytes()));
    assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), FileCopyUtils.copyToByteArray(file1.getInputStream())));
    assertEquals("file2", file2.getName());
    assertEquals("myOrigFilename", file2.getOriginalFilename());
    assertEquals("text/plain", file2.getContentType());
    assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), file2.getBytes()));
    assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), FileCopyUtils.copyToByteArray(file2.getInputStream())));
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 38 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolver method resolveName.

@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        return mpArg;
    }
    Object arg = null;
    if (multipartRequest != null) {
        List<MultipartFile> files = multipartRequest.getFiles(name);
        if (!files.isEmpty()) {
            arg = (files.size() == 1 ? files.get(0) : files);
        }
    }
    if (arg == null) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues != null) {
            arg = (paramValues.length == 1 ? paramValues[0] : paramValues);
        }
    }
    return arg;
}
Also used : MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartFile(org.springframework.web.multipart.MultipartFile) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 39 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.

the class CommonsFileUploadSupport method cleanupFileItems.

/**
	 * Cleanup the Spring MultipartFiles created during multipart parsing,
	 * potentially holding temporary data on disk.
	 * <p>Deletes the underlying Commons FileItem instances.
	 * @param multipartFiles Collection of MultipartFile instances
	 * @see org.apache.commons.fileupload.FileItem#delete()
	 */
protected void cleanupFileItems(MultiValueMap<String, MultipartFile> multipartFiles) {
    for (List<MultipartFile> files : multipartFiles.values()) {
        for (MultipartFile file : files) {
            if (file instanceof CommonsMultipartFile) {
                CommonsMultipartFile cmf = (CommonsMultipartFile) file;
                cmf.getFileItem().delete();
                if (logger.isDebugEnabled()) {
                    logger.debug("Cleaning up multipart file [" + cmf.getName() + "] with original filename [" + cmf.getOriginalFilename() + "], stored " + cmf.getStorageDescription());
                }
            }
        }
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 40 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project spring-data-document-examples by spring-projects.

the class HandlerMethodInvoker method resolveRequestParam.

@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()) {
            if (files.size() == 1 && !paramType.isArray() && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = files.get(0);
            } else {
                paramValue = files;
            }
        }
    }
    if (paramValue == null) {
        String[] paramValues = webRequest.getParameterValues(paramName);
        if (paramValues != null) {
            if (paramValues.length == 1 && !paramType.isArray() && !Collection.class.isAssignableFrom(paramType)) {
                paramValue = paramValues[0];
            } else {
                paramValue = 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);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MultipartFile(org.springframework.web.multipart.MultipartFile) MultipartRequest(org.springframework.web.multipart.MultipartRequest) ExtendedModelMap(org.springframework.ui.ExtendedModelMap)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)76 File (java.io.File)26 IOException (java.io.IOException)23 Test (org.junit.Test)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)12 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)11 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 MethodParameter (org.springframework.core.MethodParameter)8 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 RequestParam (org.springframework.web.bind.annotation.RequestParam)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 FileOutputStream (java.io.FileOutputStream)5 LinkedList (java.util.LinkedList)5 ActivitiException (org.activiti.engine.ActivitiException)5