Search in sources :

Example 6 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveMultipartFile.

@Test
public void resolveMultipartFile() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultipartFile);
    assertEquals("Invalid result", expected, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 7 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) Test(org.junit.Test)

Example 8 with MultipartFile

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

the class RequestPartServletServerHttpRequestTests method getBodyWithWrappedRequest.

// SPR-13317
@Test
public void getBodyWithWrappedRequest() throws Exception {
    byte[] bytes = "content".getBytes("UTF-8");
    MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
    this.mockRequest.addFile(part);
    HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
    ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
    byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
    assertArrayEquals(bytes, result);
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) ServerHttpRequest(org.springframework.http.server.ServerHttpRequest) Test(org.junit.Test)

Example 9 with MultipartFile

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.

@Test
public void resolveOptionalMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertTrue(actualValue instanceof Optional);
    assertEquals("Invalid result", Collections.singletonList(expected), ((Optional) actualValue).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 10 with MultipartFile

use of org.springframework.web.multipart.MultipartFile in project disconf by knightliao.

the class FileUploadValidator method validateFiles.

/**
     * 验证文件大小,文件名,文件后缀
     *
     * @param files
     * @param maxLength
     * @param allowExtName
     *
     * @return
     */
public List<MultipartFile> validateFiles(List<MultipartFile> files, long maxLength, String[] allowExtName) {
    List<MultipartFile> retFiles = new ArrayList<MultipartFile>();
    for (MultipartFile multipartFile : files) {
        try {
            this.validateFile(multipartFile, maxLength, allowExtName);
            retFiles.add(multipartFile);
        } catch (Exception e) {
            LOG.warn(e.toString());
        }
    }
    return retFiles;
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ArrayList(java.util.ArrayList) FieldException(com.baidu.dsp.common.exception.FieldException)

Aggregations

MultipartFile (org.springframework.web.multipart.MultipartFile)53 Test (org.junit.Test)18 File (java.io.File)16 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 IOException (java.io.IOException)13 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)10 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)10 MethodParameter (org.springframework.core.MethodParameter)8 List (java.util.List)6 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)6 FileOutputStream (java.io.FileOutputStream)5 ActivitiException (org.activiti.engine.ActivitiException)5 RequestParam (org.springframework.web.bind.annotation.RequestParam)5 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Part (javax.servlet.http.Part)3