Spring upload file problems

15,530

Solution 1

I fixed this problem by adding the following to my spring config file:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

(The error I was getting was "org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myFile' is not present

Solution 2

i can do it with

@Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {

        MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/",100000, 200000, 50000);

        registration.setMultipartConfig(multipartConfigElement);

    }

Solution 3

@user64141 is right but if using Java config instead of xml, try

@Bean
public MultipartResolver multipartResolver() {
    return new CommonsMultipartResolver();
}
Share:
15,530
user2160696
Author by

user2160696

Java, C# coder. Like wrestling, MMA, Blues.

Updated on June 05, 2022

Comments

  • user2160696
    user2160696 almost 2 years

    I need to upload file from browser to server. I use spring 3.2 as my web framework.

    So i configured my app like this.

    1 - I got web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
        <context-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </context-param>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>racoonsoft.chaos.settings</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>MyServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>admin/library</welcome-file>
        </welcome-file-list>
    
    </web-app>
    

    2 - MainConfig class

    @Configuration
    @Import({WebConfig.class })
    public class MainConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
        @Bean
        public static ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor() {
            return new ScheduledAnnotationBeanPostProcessor();
        }
    
        @Bean
        public static StandardServletMultipartResolver multipartResolver() {
            StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
            return resolver;
        }
    }
    

    3 - Controller to handle multipart uploads

    @Controller
    @MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
            maxFileSize=1024*1024*10,      // 10MB
            maxRequestSize=1024*1024*50)
    public class FileUpload
    {
        public static final int UPLOAD_RESULT_OK = 100000;
        @Autowired
        BookDao book_dao;
    
        @RequestMapping(value = "/admin/library/upload_file", method = RequestMethod.POST)
        public String saveFiles(@RequestParam("file-file") MultipartFile file) throws IOException
        {
            if (!file.isEmpty())
            {
                byte[] bytes = file.getBytes();
                return "redirect:caps/total_fail";
            }
            else
            {
                return "redirect:caps/total_fail";
            }
        }
    }
    

    4 - jsp where i placed my form to submit files

    ...<form method="post" action="/admin/library/upload_file" enctype="multipart/form-data">
                    <input type="text" name="name"/>
                    <input type="file" name="file-file"/>
                    <input type="submit"/>
                </form>...
    

    When i submit my form i got an Exception

    org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present
        org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)
    

    I got no idea why. When i remove @RequestParam annotaion i got my method invoked but file parameter = null. What is my problem?

  • user2160696
    user2160696 about 11 years
    I`m not sure how do i configure it. Do i need to make changes to my web.xml? Or i must create my own Filter extending MultipartFilter and map it to /admin/library/upload_file??
  • user2160696
    user2160696 about 11 years
    Sadly it didn`t help. I added those two parts, but still got null as parameter.
  • mthmulders
    mthmulders about 11 years
    By default, this filter will look for a Spring bean called "filterMultipartResolver" that acts as a MultipartResolver. You can specify another bean name using the "multipartResolverBeanName" filter init-param in your web.xml. Can you either change the name of your "multipartResolver" bean, or fix the configuration of the filter, and see if that works?