PDA

View Full Version : CommonsMultipartResolver cleanes up multipart file too early


bowa
Jul 16th, 2007, 05:09 AM
I started from some code from
http://www.springframework.org/docs/reference/mvc.html#mvc-multipart

but when i want to copy the file i uploaded in the 'onSubmit' method of my FileUploadController (extending SimpleFormController) it looks from the log the CommonsMultipartResolver already cleaned up the multipart file ... it also seems strange the OpenSessionInViewInterceptor already closes the hibernate session ... while the controller is still working ?


16 jul 2007 10:48:40 DEBUG my.package.mvc.controller.FileUploadController - Creating new command of class [my.package.document.FileUploadBean]
16 jul 2007 10:48:40 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Getting BeanInfo for class [my.package.document.FileUploadBean]
16 jul 2007 10:48:40 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Caching PropertyDescriptors for class [my.package.document.FileUploadBean]
16 jul 2007 10:48:40 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Found bean property 'class' of type [java.lang.Class]
16 jul 2007 10:48:40 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Found bean property 'file' of type [[B]
16 jul 2007 10:48:40 DEBUG org.springframework.core.CollectionFactory - Creating [java.util.LinkedHashMap]
16 jul 2007 10:48:40 DEBUG my.package.mvc.controller.FileUploadController - No errors -> processing submit
16 jul 2007 10:48:40 DEBUG org.springframework.core.CollectionFactory - Creating [java.util.LinkedHashMap]
16 jul 2007 10:48:40 DEBUG org.springframework.transaction.support.Transactio nSynchronizationManager - Removed value [org.springframework.orm.hibernate3.SessionHolder@1 4e1705] for key [org.hibernate.impl.SessionFactoryImpl@56ff18] from thread [http-8080-Processor24]
16 jul 2007 10:48:40 DEBUG org.springframework.orm.hibernate3.support.OpenSes sionInViewInterceptor - Closing single Hibernate Session in OpenSessionInViewInterceptor
16 jul 2007 10:48:40 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Closing Hibernate Session
16 jul 2007 10:48:40 DEBUG org.hibernate.impl.SessionImpl - closing session
16 jul 2007 10:48:40 DEBUG org.hibernate.jdbc.ConnectionManager - connection already null in cleanup : no action
16 jul 2007 10:48:40 DEBUG org.springframework.web.multipart.commons.CommonsM ultipartResolver - Cleaning up multipart file [Filedata] with original filename [435148OIy.jpg], stored at [C:\projects\mywebapp\work\upload__1b6e95ee_113ce1b 37d7__7fff_00000000.tmp]
16 jul 2007 10:48:40 DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@4b5abc
16 jul 2007 10:48:40 DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException
at my.package.mvc.controller.FileUploadController.onS ubmit(FileUploadController.java:49)
at org.springframework.web.servlet.mvc.SimpleFormCont roller.processFormSubmission(SimpleFormController. java:267)

line 49 is where i get the original filename ...

code from my FileUploadController

MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest) request;
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("file");

String sep = System.getProperty("file.separator");
if (logger.isDebugEnabled()) {
logger.debug("uploading to: " + uploadDir + sep +
file.getOriginalFilename());
}

Marten Deinum
Jul 16th, 2007, 07:17 AM
The temp file gets removed as soon as the upload is over. After that the CommonsMultipartFile IS the file. You will have to read it with the inputstream or something else. But you cannot simply copy it, after the upload it is in memory.

Read the chapter covering file-upload in the reference guide, chapter 13.8 (http://static.springframework.org/docs/reference/mvc.html#mvc-multipart) is the one to read.

bowa
Jul 16th, 2007, 09:16 AM
i followed that actual example ... hence i referenced it in my post.

the problem was that the attribute posted was named "Filedata" and i was looking for an attribute "file".

so changing the name attribute of the input field in the html to "file" made it work.

i did use "Filedata" because that is what Flash fileupload is using. (flash 8 file upload docs : http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002225.html)

now what is the problem:

16 jul 2007 14:56:51 DEBUG org.springframework.web.servlet.DispatcherServlet - Bound request context to thread: org.apache.catalina.connector.RequestFacade@1660d2 2
16 jul 2007 14:56:51 DEBUG org.springframework.web.multipart.commons.CommonsM ultipartResolver - Found multipart file [Filedata] of size 56839 bytes with original filename [435130kFh.jpg], stored at [C:\projects\myproject\work\upload__2089efd6_113cf1 a4260__8000_00000001.tmp]

like mentioned in the flash docs, it used a hardcoded attributename "Filedata" that is not following beannaming conventions ...


16 jul 2007 14:56:51 DEBUG my.package.mvc.controller.FileUploadController - Creating new command of class [my.package.document.FileUploadBean]
16 jul 2007 14:56:51 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Getting BeanInfo for class [my.package.document.FileUploadBean]
16 jul 2007 14:56:51 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Caching PropertyDescriptors for class [my.package.document.FileUploadBean]
16 jul 2007 14:56:51 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Found bean property 'class' of type [java.lang.Class]
16 jul 2007 14:56:51 DEBUG org.springframework.beans.CachedIntrospectionResul ts - Found bean property 'filedata' of type [[B]

using introspection spring finds a property 'filedata', but no 'Filedata' so it doesnt set anything to my FileUploadBean

anyone sees a workaround on how to solve this stupid flash attribute naming ?

Marten Deinum
Jul 16th, 2007, 09:44 AM
Well the fact that you where trying to use flash upload wasn't mentioned in your first post, also I was set aback by the fact that you where using the request etc. directly instead of hidding it behind the Spring provided stuff.

You might try get/setFIledata (although that would probably result in a FIledata property) the next thing you could do is to supply your own BeanDescription. Check the javabeans spec on how to do that.

Marten Deinum
Jul 16th, 2007, 10:15 AM
Well the fact that you where trying to use flash upload wasn't mentioned in your first post, also I was set aback by the fact that you where using the request etc. directly instead of hidding it behind the Spring provided stuff.

You might try get/setFIledata (although that would probably result in a FIledata property) the next thing you could do is to supply your own BeanDescription. Check the javabeans spec on how to do that.

bowa
Jul 16th, 2007, 10:18 AM
i wasnt using flash at first, i try to start out easy and get that to work and then add more complexity, so i tried it with a html field to begin with.

i ll have a look at the BeanDescription thing, thanks.

bowa
Jul 16th, 2007, 11:41 AM
i was looking into the option of using the 'onBind' method in my FileUploadController (that is a SimpleFormController).

onBind

protected void onBind(HttpServletRequest request,
Object command)
throws Exception

Callback for custom post-processing in terms of binding. Called by the default implementation of the onBind version with all parameters, after standard binding but before validation.

but i have no idea how i would be able to get the actual byte array i need out of the request object.