简体   繁体   中英

Could not parse multipart servlet request;.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

I'm unable to upload an image to the server. I've tried many solutions to related problems here but none working yet for me. Here are my files:

SecurityApplicationInitializer:

        package com.App.config;

        import javax.servlet.ServletContext;

        import org.springframework.context.annotation.Configuration;
        import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
        import org.springframework.web.multipart.support.MultipartFilter;

        @Configuration
        public class AppSecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

            @Override
            protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
                insertFilters(servletContext, new MultipartFilter());
            }

        }

WebMVCConfigurerAdapter:

    package com.App.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScan.Filter;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    import org.springframework.web.servlet.view.JstlView;



     //mvc:annotation-driven
    @Configuration

    @ComponentScan(basePackages ={ "com.App.controller", "com.App.mapper",
            "com.App.JDBC","com.App.model"      
    })//, excludeFilters = { 
    //@Filter(type = FilterType.ANNOTATION, value = Configuration.class) })

    public class AppConfig extends WebMvcConfigurerAdapter {
        int IMAGE_MAX_UPLOAD_SIZE = 8000;

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        }
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

        }

      @Bean
          public CommonsMultipartResolver multipartResolver() {
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
            multipartResolver.setMaxUploadSize(IMAGE_MAX_UPLOAD_SIZE);
            multipartResolver.setDefaultEncoding("utf-8");
            return multipartResolver;
          }


        @Bean
        public InternalResourceViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setViewClass(JstlView.class);
            viewResolver.setPrefix("/WEB-INF/views/jsp/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }

        /* @Bean 
         public UserRepositoryUserDetailsService getUserRepositoryUserDetailsService(UserRepository userRepository){

            UserRepositoryUserDetailsService bean = new UserRepositoryUserDetailsService(userRepository);
             return bean;
         }*/
        @Bean
        public DriverManagerDataSource getDatasource(){
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setPassword("....");
            dataSource.setUrl("....");
            dataSource.setUsername("root");
            return dataSource;
        }
        ...
    }

ServletInit:

    package com.App.config;

    import javax.servlet.Filter;

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.filter.HiddenHttpMethodFilter;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    @EnableWebMvc
    @Configuration
    public class AppWebInit extends
            AbstractAnnotationConfigDispatcherServletInitializer {


        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] { AppConfig.class };
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/","/logout","/403", "/login" };
        }

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { RootConfiguration.class };
        }
       @Override
        protected Filter[] getServletFilters() {
            return new Filter[] { new HiddenHttpMethodFilter() };
        }
    }

RootConfig:

    package com.App.config;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    /**

     *
     * @author E
     *
     */
    @Configuration
    @ComponentScan
    public class RootConfiguration {

    }

SecurityConfig:

    package com.App.config;

    import javax.sql.DataSource;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.web.csrf.CsrfTokenRepository;
    import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled=true)
    //@ComponentScan(basePackageClasses = UserRepositoryUserDetailsService.class)
    public class SecurityConfigure extends WebSecurityConfigurerAdapter{

        //@Autowired
        //private UserDetailsService userDetailsService;


         @Autowired
        private CsrfTokenRepository csrfTokenRepository() 
        { 
            HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
            repository.setSessionAttributeName("_csrf");
            return repository; 
        }
         @Override
        protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf()
                .csrfTokenRepository(csrfTokenRepository())
                .and()
            .authorizeRequests()
                .antMatchers("/resources/**", "/signuPerson", "/login", "/", "/logout","viewBuyOrders").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
              //.loginProcessingUrl("/processLogin")
                //.failureUrl("login?error")
                //
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
               // .addLogoutHandler(new SecurityContextLogoutHandler())
               // .logoutSuccessUrl("/403")
                 //
                .permitAll();

        }

        @Autowired
        public void configureGlobal(DataSource dataSource , AuthenticationManagerBuilder auth) throws Exception {
        auth
            .jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("select login_username,  login_password, true from login where login_username = ?")
            .authoritiesByUsernameQuery("select login_username, 'ROLE_USER' from login where login_username = ? ");

        }

    }

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>App</groupId>
      <artifactId>App</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
       <dependencies>
       <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.3.1</version> 
    </dependency>
           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
       <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
                <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>3.0.5.RELEASE</version>
         </dependency>
        <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.2.RELEASE</version>
       </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency> 
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-web</artifactId>
       <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- 
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>1.3.0.RELEASE</version>
     </dependency>-->
             <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
      </dependency>

          </dependencies>

    </project>

pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>App</groupId>
      <artifactId>App</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
       <dependencies>
       <dependency>
       <groupId>commons-fileupload</groupId>
       <artifactId>commons-fileupload</artifactId>
       <version>1.3.1</version> 
    </dependency>
           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
       <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
                <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
        <version>3.0.5.RELEASE</version>
         </dependency>
        <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.2.RELEASE</version>
       </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>
        <dependency> 
       <groupId>org.springframework.security</groupId>
       <artifactId>spring-security-web</artifactId>
       <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- 
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
                <version>1.3.0.RELEASE</version>
     </dependency>-->
             <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.3.RELEASE</version>
      </dependency>

          </dependencies>

    </project>

Help will be appreciated.

I think the MultipartResolver bean name is wrong. It should be named filterMultipartResolver . So in your Java config:

@Bean public CommonsMultipartResolver filterMultipartResolver() { ... }

An option is to skip commons-fileupload and use the multi-part support in Servlet 3 API. You can enable it by adding in your WebAppInit class:

@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    registration.setMultipartConfig(new MultipartConfigElement(LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
}

The constants of your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM