Friday, February 6, 2009

Spring MultipartFile transferTo error


java.lang.IllegalStateException: File has already been moved - cannot be transferred again
at org.springframework.web.multipart.commons.CommonsMultipartFile.transferTo(CommonsMultipartFile.java:128)



If you are using Spring MVC, this is caused by either 1) calling multipartFile.transferTo(destFile) more then once, or 2) calling transferTo outside of the request lifecycle.

It seems the multipartFile is only valid within the confines of the request it was posted in. If you save it in session and attempt to use it later, or process it in a separate thread, the multipartFile will no longer be valid.