@Bean public MultipartResolver multipartResolver() { return new StandardServletMultipartResolver() { @Override public boolean isMultipart(HttpServletRequest request) { String method = request.getMethod().toLowerCase(); // By default, only POST is allowed. Since this is an 'update' // we should accept PUT. if (!Arrays.asList("put", "post").contains(method)) { return false; } String contentType = request.getContentType(); return (contentType != null && contentType.toLowerCase().startsWith("multipart/")); } }; }
/** * Initialize the MultipartResolver used by this class. * <p>If no bean is defined with the given name in the BeanFactory for this namespace, * no multipart handling is provided. */ private void initMultipartResolver(ApplicationContext context) { try { this.multipartResolver = context.getBean(MULTIPART_RESOLVER_BEAN_NAME, MultipartResolver.class); if (logger.isDebugEnabled()) { logger.debug("Using MultipartResolver [" + this.multipartResolver + "]"); } } catch (NoSuchBeanDefinitionException ex) { // Default is no multipart resolver. this.multipartResolver = null; if (logger.isDebugEnabled()) { logger.debug("Unable to locate MultipartResolver with name '" + MULTIPART_RESOLVER_BEAN_NAME + "': no multipart request handling provided"); } } }
@Bean public MultipartResolver multipartResolver(WikiConfig wikiConfig) { final String UPLOAD_TEMP_DIR = wikiConfig.getUploadTempDir(); // CommonsMultipartResolver mr = new CommonsMultipartResolver(); mr.setMaxUploadSize(MAX_UPLOAD_SIZE); mr.setDefaultEncoding(DEFAULT_ENCODING); try { Resource uploadTempDir = resourceLoader .getResource(UPLOAD_TEMP_DIR); mr.setUploadTempDir(uploadTempDir); LOG.debug(String.format("UPLOAD TEMP DIR = %s", uploadTempDir)); } catch (IOException e) { LOG.error(String.format("#getResource(%s) FAIL, FALLBACK.", UPLOAD_TEMP_DIR), e); } return mr; }
@Bean(name = "multipartResolver") public MultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setDefaultEncoding("UTF-8"); resolver.setMaxInMemorySize(40960); resolver.setMaxUploadSize(50 * 1024 * 1024);//上传文件大小 50M 50*1024*1024 return resolver; }
/** * Check for a multipart request via this filter's MultipartResolver, * and wrap the original request with a MultipartHttpServletRequest if appropriate. * <p>All later elements in the filter chain, most importantly servlets, benefit * from proper parameter extraction in the multipart case, and are able to cast to * MultipartHttpServletRequest if they need to. */ @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { MultipartResolver multipartResolver = lookupMultipartResolver(request); HttpServletRequest processedRequest = request; if (multipartResolver.isMultipart(processedRequest)) { if (logger.isDebugEnabled()) { logger.debug("Resolving multipart request [" + processedRequest.getRequestURI() + "] with MultipartFilter"); } processedRequest = multipartResolver.resolveMultipart(processedRequest); } else { if (logger.isDebugEnabled()) { logger.debug("Request [" + processedRequest.getRequestURI() + "] is not a multipart request"); } } try { filterChain.doFilter(processedRequest, response); } finally { if (processedRequest instanceof MultipartHttpServletRequest) { multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest); } } }
/** * Look for a MultipartResolver bean in the root web application context. * Supports a "multipartResolverBeanName" filter init param; the default * bean name is "filterMultipartResolver". * <p>This can be overridden to use a custom MultipartResolver instance, * for example if not using a Spring web application context. * @return the MultipartResolver instance, or {@code null} if none found */ protected MultipartResolver lookupMultipartResolver() { WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); String beanName = getMultipartResolverBeanName(); if (wac != null && wac.containsBean(beanName)) { if (logger.isDebugEnabled()) { logger.debug("Using MultipartResolver '" + beanName + "' for MultipartFilter"); } return wac.getBean(beanName, MultipartResolver.class); } else { return this.defaultMultipartResolver; } }
@Test public void dispatcherServletRefresh() throws ServletException { MockServletContext servletContext = new MockServletContext("org/springframework/web/context"); DispatcherServlet servlet = new DispatcherServlet(); servlet.init(new MockServletConfig(servletContext, "empty")); ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean.getServletContext()); assertSame(servlet.getServletConfig(), configBean.getServletConfig()); MultipartResolver multipartResolver = servlet.getMultipartResolver(); assertNotNull(multipartResolver); servlet.refresh(); ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean2.getServletContext()); assertSame(servlet.getServletConfig(), configBean2.getServletConfig()); assertNotSame(contextBean, contextBean2); assertNotSame(configBean, configBean2); MultipartResolver multipartResolver2 = servlet.getMultipartResolver(); assertNotSame(multipartResolver, multipartResolver2); servlet.destroy(); }
@Test public void dispatcherServletContextRefresh() throws ServletException { MockServletContext servletContext = new MockServletContext("org/springframework/web/context"); DispatcherServlet servlet = new DispatcherServlet(); servlet.init(new MockServletConfig(servletContext, "empty")); ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean.getServletContext()); assertSame(servlet.getServletConfig(), configBean.getServletConfig()); MultipartResolver multipartResolver = servlet.getMultipartResolver(); assertNotNull(multipartResolver); ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh(); ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean2.getServletContext()); assertSame(servlet.getServletConfig(), configBean2.getServletConfig()); assertTrue(contextBean != contextBean2); assertTrue(configBean != configBean2); MultipartResolver multipartResolver2 = servlet.getMultipartResolver(); assertTrue(multipartResolver != multipartResolver2); servlet.destroy(); }
/** * Check for a multipart request via this filter's MultipartResolver, * and wrap the original request with a MultipartHttpServletRequest if appropriate. * <p>All later elements in the filter chain, most importantly servlets, benefit * from proper parameter extraction in the multipart case, and are able to cast to * MultipartHttpServletRequest if they need to. */ @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { MultipartResolver multipartResolver = lookupMultipartResolver(request); HttpServletRequest processedRequest = request; if (multipartResolver.isMultipart(processedRequest)) { if (logger.isDebugEnabled()) { logger.debug("Resolving multipart request [" + processedRequest.getRequestURI() + "] with MultipartFilter"); } processedRequest = multipartResolver.resolveMultipart(processedRequest); } else { // A regular request... if (logger.isDebugEnabled()) { logger.debug("Request [" + processedRequest.getRequestURI() + "] is not a multipart request"); } } try { filterChain.doFilter(processedRequest, response); } finally { if (processedRequest instanceof MultipartHttpServletRequest) { multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest); } } }
@Bean @ConditionalOnBean(MultipartResolver.class) @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) public MultipartResolver multipartResolver(MultipartResolver resolver) { // Detect if the user has created a MultipartResolver but named it incorrectly return resolver; }
@Test public void containerWithNothing() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNothing.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); verify404(); assertThat(servlet.getMultipartResolver()).isNotNull(); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)) .hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); }
@Test public void containerWithNoMultipartJettyConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartJetty.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertThat(servlet.getMultipartResolver()).isNotNull(); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)) .hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); verifyServletWorks(); }
@Test public void containerWithNoMultipartUndertowConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartUndertow.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); verifyServletWorks(); assertThat(servlet.getMultipartResolver()).isNotNull(); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)) .hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); }
@Test public void containerWithNoMultipartTomcatConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartTomcat.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertThat(servlet.getMultipartResolver()).isNull(); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class)) .hasSize(1); assertThat(this.context.getBeansOfType(MultipartResolver.class)).hasSize(1); verifyServletWorks(); }
@Test public void containerWithCustomMultipartResolver() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithCustomMultipartResolver.class, BaseConfiguration.class); MultipartResolver multipartResolver = this.context .getBean(MultipartResolver.class); assertThat(multipartResolver) .isNotInstanceOf(StandardServletMultipartResolver.class); }
@Test public void containerWithNothing() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNothing.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); verify404(); assertNotNull(servlet.getMultipartResolver()); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) .size(), equalTo(1)); assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), equalTo(1)); }
@Test public void containerWithNoMultipartJettyConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartJetty.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNotNull(servlet.getMultipartResolver()); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) .size(), equalTo(1)); assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), equalTo(1)); verifyServletWorks(); }
@Test public void containerWithNoMultipartUndertowConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartUndertow.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); verifyServletWorks(); assertNotNull(servlet.getMultipartResolver()); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) .size(), equalTo(1)); assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), equalTo(1)); }
@Test public void containerWithNoMultipartTomcatConfiguration() { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithNoMultipartTomcat.class, BaseConfiguration.class); DispatcherServlet servlet = this.context.getBean(DispatcherServlet.class); assertNull(servlet.getMultipartResolver()); assertThat(this.context.getBeansOfType(StandardServletMultipartResolver.class) .size(), equalTo(1)); assertThat(this.context.getBeansOfType(MultipartResolver.class).size(), equalTo(1)); verifyServletWorks(); }
@Test public void containerWithCustomMultipartResolver() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext( ContainerWithCustomMultipartResolver.class, BaseConfiguration.class); MultipartResolver multipartResolver = this.context .getBean(MultipartResolver.class); assertThat(multipartResolver, not(instanceOf(StandardServletMultipartResolver.class))); }
@Bean(name=MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME) @ConditionalOnMissingBean(name={MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME}) public MultipartResolver filterMultipartResolver(){ BootStandardServletMultipartResolver resolver = new BootStandardServletMultipartResolver(); resolver.setMaxUploadSize(FileUtils.parseSize(multipartProperties.getMaxRequestSize())); return resolver; }
@Bean(name=MultipartFilter.DEFAULT_MULTIPART_RESOLVER_BEAN_NAME) // @ConditionalOnMissingBean(MultipartResolver.class) public MultipartResolver filterMultipartResolver(){ BootStandardServletMultipartResolver resolver = new BootStandardServletMultipartResolver(); resolver.setMaxUploadSize(FileUtils.parseSize(multipartProperties.getMaxRequestSize())); return resolver; }
public MultipartResolver getMultipartResolver() throws Exception { String indicator = (String) tsh.getTokenState(HelperHandlerHookBean.IN_HELPER_INDICATOR); if (indicator != null && indicator.equals(HelperHandlerHookBean.IN_HELPER_INDICATOR)) return blankMultipartResolver; else return commonsMultipartResolver; }
public void testDispatcherServletRefresh() throws ServletException { MockServletContext servletContext = new MockServletContext("org/springframework/web/context"); DispatcherServlet servlet = new DispatcherServlet(); servlet.init(new MockServletConfig(servletContext, "empty")); ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean.getServletContext()); assertSame(servlet.getServletConfig(), configBean.getServletConfig()); MultipartResolver multipartResolver = servlet.getMultipartResolver(); assertNotNull(multipartResolver); servlet.refresh(); ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean2.getServletContext()); assertSame(servlet.getServletConfig(), configBean2.getServletConfig()); assertNotSame(contextBean, contextBean2); assertNotSame(configBean, configBean2); MultipartResolver multipartResolver2 = servlet.getMultipartResolver(); assertNotSame(multipartResolver, multipartResolver2); servlet.destroy(); }
public void testDispatcherServletContextRefresh() throws ServletException { MockServletContext servletContext = new MockServletContext("org/springframework/web/context"); DispatcherServlet servlet = new DispatcherServlet(); servlet.init(new MockServletConfig(servletContext, "empty")); ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean.getServletContext()); assertSame(servlet.getServletConfig(), configBean.getServletConfig()); MultipartResolver multipartResolver = servlet.getMultipartResolver(); assertNotNull(multipartResolver); ((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh(); ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean"); ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean"); assertSame(servletContext, contextBean2.getServletContext()); assertSame(servlet.getServletConfig(), configBean2.getServletConfig()); assertTrue(contextBean != contextBean2); assertTrue(configBean != configBean2); MultipartResolver multipartResolver2 = servlet.getMultipartResolver(); assertTrue(multipartResolver != multipartResolver2); servlet.destroy(); }
@Bean public MultipartResolver multipartResolver () { final long DEFAULT_MAX_UPLOAD_SIZE = 1024L * 1024L * 200; // 200Mb //TODO: find a better way to bypass Spring controllers for OpenRefine CommonsMultipartResolver resolver = new CommonsMultipartResolver (){ @Override public boolean isMultipart(HttpServletRequest request){ if(request.getServletPath().startsWith ("/annotations")){ if(request.getServletPath().endsWith("/row/import") || request.getServletPath().endsWith("/column/import")) //annotation import is handled by AnnotationConroller return super.isMultipart (request); else //Let OpenRefine handle parsing of the multipart request return false; }else{ return super.isMultipart (request); } } }; // StandardServletMultipartResolver resolver = new StandardServletMultipartResolver (); resolver.setMaxUploadSize (environment.getProperty ("multipart.upload.max.size", Long.class, DEFAULT_MAX_UPLOAD_SIZE)); resolver.setResolveLazily (true); return resolver; }
/** * Look for a MultipartResolver bean in the root web application context. * Supports a "multipartResolverBeanName" filter init param; the default * bean name is "filterMultipartResolver". * <p>This can be overridden to use a custom MultipartResolver instance, * for example if not using a Spring web application context. * @return the MultipartResolver instance, or {@code null} if none found */ @Override protected MultipartResolver lookupMultipartResolver() { WebApplicationContext wac = (WebApplicationContext)RythmConfigurer.getInstance().getApplicationContext(); String beanName = getMultipartResolverBeanName(); if (wac != null && wac.containsBean(beanName)) { if (logger.isDebugEnabled()) { logger.debug("Using MultipartResolver '" + beanName + "' for MultipartFilter"); } return wac.getBean(beanName, MultipartResolver.class); } else { return this.defaultMultipartResolver; } }
@Bean public MultipartResolver multipartResolver() { final CommonsMultipartResolver resolver = new CommonsMultipartResolver(); // Max upload size: 1 MB resolver.setMaxUploadSize(1048576); resolver.setDefaultEncoding(StandardCharsets.UTF_8.name()); resolver.setResolveLazily(true); return resolver; }