I use the commons multipart file resolver (make sure you have commons-fileupload.jar in your classpath):-
Code:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
MyCommand object:-
Code:
import org.springframework.web.multipart.MultipartFile;
...snip...
/**
* Sets the image
*
* @param image The image
*/
public void setImage(MultipartFile image)
{
this.image = image;
}
/**
* Retrieves the image
*
* @return The image
*/
public MultipartFile getImage()
{
return (image);
}
MultipartFilePropertyEditor:-
Code:
/*
* MPSC-Spring - A library of common code to use with the Spring framework
*
* Copyright 2005 Matt Parker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.co.mpcontracting.modules.spring.support;
import java.beans.PropertyEditorSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
/**
* An implementation of <code>java.beans.PropertyEditor</code> that supports a Spring
* multipart file
*
* @author Matt Parker (matt@mpcontracting.co.uk)
*/
public class MultipartFilePropertyEditor extends PropertyEditorSupport
{
private static Log log = LogFactory.getLog(MultipartFilePropertyEditor.class);
/**
* Sets the multipart file into the property editor
*
* @param value The multipart file
*/
public void setValue(Object value)
{
if (log.isDebugEnabled())
{
if (value != null)
{
log.debug("Object = " + value.getClass().getName());
}
else
{
log.debug("Object = null");
}
}
if (value instanceof MultipartFile)
{
super.setValue(value);
}
else if (value != null)
{
log.error("Object is not a multipart file - " + value.getClass().getName());
}
}
/**
* Overrides the set as text method so no exceptions are thrown
*
* @param text The text
* @throws IllegalArgumentException If either the String is
* badly formatted or if this kind of property can't be expressed
* as text
*/
public void setAsText(String text) throws IllegalArgumentException
{
return;
}
/**
* Retrieves a textual representation of the multipart file from the property editor
*
* @return A textual representation of the multipart file
*/
public String getAsText()
{
Object value = getValue();
if (value != null)
{
return (((MultipartFile)value).getOriginalFilename());
}
return ("");
}
}
In initBinder:-
Code:
binder.registerCustomEditor(MultipartFile.class, new MultipartFilePropertyEditor());
In my form controller:-
Code:
MyCommand myCommand = (MyCommand)command;
MultipartFile image = myCommand.getImage();
String fileName = image.getOriginalFilename()
HTH,
Bob