Stack OverFlow
Stack OverFlow

Reputation: 19

How to pass a MultipartHttpServletRequest?

Before posting this, I searched on stack overflow and did the applicable answer. I tried putting enctype on my form tag,

<form name="something" method="post" enctype="multipart/form-data">

adding MultipartResolver on my bean,

<bean id="spring.RegularCommonsMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

but none of these solved my problem.

I want a method that accepts MultipartHttpServletRequest as the parameter. But the browser console gives me this error:

POST link.json 500 Internal Server Error

Eclipse on the other hand throws this error:

 Servlet.service() for servlet [action] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]

My controller code looks like this:

@RequestMapping("myjson/myLink")
public void myMethod(MultipartHttpServletRequest request)
{

}

My code accepts HttpServletRequest and not MultipartHttpServletRequest. Is there other way to this?

I badly need MultipartHttpServletRequest because of its getFile method. I need to get an image from the client and store it on the server.

Upvotes: 1

Views: 4519

Answers (1)

Ahmad
Ahmad

Reputation: 1474

This will surely work for you.

HTML:

<form action="myjson/myLink" id="fileForm" method="POST" enctype="multipart/form-data">
    <input type="file" name="File"/>
</form>

Controller:

@RequestMapping(value = "/myjson/myLink", method = RequestMethod.POST)
    public @ResponseBody String SaveFile(HttpServletRequest request, @RequestParam("File") MultipartFile file) {
if(!file.isEmpty())
    {
        try
        {

            File convFile = new File(//here file location+filename);
            convFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(convFile);
            fos.write( file.getBytes() );
            fos.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Spring Configuration:

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>

and in your Spring Initializer class Define MultipartConfigElement element

public class SpringInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

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

     @Override
        protected void customizeRegistration(ServletRegistration.Dynamic registration) {
            registration.setMultipartConfig(getMultipartConfigElement());
        }

        private MultipartConfigElement getMultipartConfigElement() {
            MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
            return multipartConfigElement;
        }

        private static final String LOCATION = System.getenv("TEMP").replace('\\', '/') + "/"; // Temporary location where files will be stored

        private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
                                                            // Beyond that size spring will throw exception.
        private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.

        private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk

} 

Upvotes: 1

Related Questions