Search in sources :

Example 26 with MultipartFile

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

the class CommonsMultipartResolverTests method doTestFiles.

private void doTestFiles(MultipartHttpServletRequest request) throws IOException {
    Set<String> fileNames = new HashSet<>();
    Iterator<String> fileIter = request.getFileNames();
    while (fileIter.hasNext()) {
        fileNames.add(fileIter.next());
    }
    assertEquals(3, fileNames.size());
    assertTrue(fileNames.contains("field1"));
    assertTrue(fileNames.contains("field2"));
    assertTrue(fileNames.contains("field2x"));
    CommonsMultipartFile file1 = (CommonsMultipartFile) request.getFile("field1");
    CommonsMultipartFile file2 = (CommonsMultipartFile) request.getFile("field2");
    CommonsMultipartFile file2x = (CommonsMultipartFile) request.getFile("field2x");
    Map<String, MultipartFile> fileMap = request.getFileMap();
    assertEquals(3, fileMap.size());
    assertTrue(fileMap.containsKey("field1"));
    assertTrue(fileMap.containsKey("field2"));
    assertTrue(fileMap.containsKey("field2x"));
    assertEquals(file1, fileMap.get("field1"));
    assertEquals(file2, fileMap.get("field2"));
    assertEquals(file2x, fileMap.get("field2x"));
    MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();
    assertEquals(3, multiFileMap.size());
    assertTrue(multiFileMap.containsKey("field1"));
    assertTrue(multiFileMap.containsKey("field2"));
    assertTrue(multiFileMap.containsKey("field2x"));
    List<MultipartFile> field1Files = multiFileMap.get("field1");
    assertEquals(2, field1Files.size());
    assertTrue(field1Files.contains(file1));
    assertEquals(file1, multiFileMap.getFirst("field1"));
    assertEquals(file2, multiFileMap.getFirst("field2"));
    assertEquals(file2x, multiFileMap.getFirst("field2x"));
    assertEquals("type1", file1.getContentType());
    assertEquals("type2", file2.getContentType());
    assertEquals("type2", file2x.getContentType());
    assertEquals("field1.txt", file1.getOriginalFilename());
    assertEquals("field2.txt", file2.getOriginalFilename());
    assertEquals("field2x.txt", file2x.getOriginalFilename());
    assertEquals("text1", new String(file1.getBytes()));
    assertEquals("text2", new String(file2.getBytes()));
    assertEquals(5, file1.getSize());
    assertEquals(5, file2.getSize());
    assertTrue(file1.getInputStream() instanceof ByteArrayInputStream);
    assertTrue(file2.getInputStream() instanceof ByteArrayInputStream);
    File transfer1 = new File("C:/transfer1");
    file1.transferTo(transfer1);
    File transfer2 = new File("C:/transfer2");
    file2.transferTo(transfer2);
    assertEquals(transfer1, ((MockFileItem) file1.getFileItem()).writtenFile);
    assertEquals(transfer2, ((MockFileItem) file2.getFileItem()).writtenFile);
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) HashSet(java.util.HashSet)

Example 27 with MultipartFile

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

the class CommonsMultipartResolverTests method withServletContextAndFilterWithCustomBeanName.

@Test
public void withServletContextAndFilterWithCustomBeanName() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    wac.registerSingleton("myMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("multipartResolverBeanName", "myMultipartResolver");
    final List<MultipartFile> files = new ArrayList<>();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest originalRequest, ServletResponse response) {
            if (originalRequest instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest request = (MultipartHttpServletRequest) originalRequest;
                files.addAll(request.getFileMap().values());
            }
        }
    };
    MultipartFilter filter = new MultipartFilter() {

        private boolean invoked = false;

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            super.doFilterInternal(request, response, filterChain);
            super.doFilterInternal(request, response, filterChain);
            if (invoked) {
                throw new ServletException("Should not have been invoked twice");
            }
            invoked = true;
        }
    };
    filter.init(filterConfig);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    HttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(originalRequest, response, filterChain);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletException(javax.servlet.ServletException) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 28 with MultipartFile

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

the class CommonsMultipartResolverTests method withServletContextAndFilter.

@Test
public void withServletContextAndFilter() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class, new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("class", "notWritable");
    filterConfig.addInitParameter("unknownParam", "someValue");
    final MultipartFilter filter = new MultipartFilter();
    filter.init(filterConfig);
    final List<MultipartFile> files = new ArrayList<>();
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
            files.addAll(request.getFileMap().values());
        }
    };
    FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    filter.doFilter(originalRequest, response, filterChain2);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}
Also used : MultipartFilter(org.springframework.web.multipart.support.MultipartFilter) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) ArrayList(java.util.ArrayList) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) MultipartFile(org.springframework.web.multipart.MultipartFile) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.Test)

Example 29 with MultipartFile

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

the class ByteArrayMultipartFileEditorTests method setValueAsMultipartFile.

@Test
public void setValueAsMultipartFile() throws Exception {
    String expectedValue = "That is comforting to know";
    MultipartFile file = mock(MultipartFile.class);
    given(file.getBytes()).willReturn(expectedValue.getBytes());
    editor.setValue(file);
    assertEquals(expectedValue, editor.getAsText());
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) Test(org.junit.Test)

Example 30 with MultipartFile

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.

@Test
public void resolveOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    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(Optional.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertTrue(result instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) 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) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

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