1

Im trying to write a controller and a form that can handle a multipart file upload and some other data passing. First i made the basic form like this:

<form:form method="POST" commandName="myForm"> 

then everything fine, but no multipart handling of course. Then i add the enctype part like this:

<form:form method="POST" commandName="myForm" enctype="multipart/form-data"> 

Then my whole form is messed up and all the attribuets gives NullPointers. Not even a simple String name attribute working. Also i added:

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

So i really got no idea whats the problem. Any comment would help a lot. Thnaks in advance.

1 Answer 1

5

We are using CommonsMultipartResolver in our project. It goes like this. In your applicationContext.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maxUploadSize" value="1048576000"/> <property name="defaultEncoding" value="UTF-8" /> </bean> 

Then cast yout request to MultipartHttpServletRequest:

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { if (!(req instanceof MultipartHttpServletRequest)) { error(resp, "Invalid request (multipart request expected)"); return null; } Map<String, MultipartFile> files = ((MultipartHttpServletRequest)req).getFileMap(); ... do thomething with the files 
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting ClassCastException now, but i think my problem is mainly that when i add enctype then my fomr goes crazy and spring cant handle it somehow, but dont know why, i added the same bean as you did and not working...
enctype is essential for file uploads, you can't get rid of them. The error must be trivial: you edited the wrong file, or didn't add "implements Controller" or something
true thing. of course i edited the wrong xml... hate it when i get these little errors and debugging them for hours... Anyway, thanks for the help ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.