Monthly Archives: March 2012

Spring Framework – file upload using ConversionService

Sometimes, when creating web application, we want to enable user to upload files to a server.

The uploaded file can be an image, document or any other regular file.
Developing our application using Spring should be relatively simple, so we need simple method for uploading files using this beautiful framework :-).

In our example I’ll use a Company entity, which among the others will contain an Image object with Data of the image and its name.

My Company entity looks like this:

@Entity
@Table
public class Company {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@ManyToOne(cascade = CascadeType.ALL)
	private Address address;
	@NotEmpty
	private String name;
	@OneToOne(cascade = CascadeType.ALL)
	private Image image;
... accessors ommitted
}

As you can see I need company Address, Name and the Image.
In this example image is identified by name and data (content of image file), but you can do whatever you want with uploaded data, during conversion process.

On Spring Framework’s  documentation sites we can see a usage of custom editor support to convert file uploaded using HTML form into regular Java object.

I went in a different way and used ConversionService to easily convert file from HTTP POST request, which is more comfortable for me.
If you want to use ConversionService you’ll need to create a converter, which implements org.springframework.core.convert.converter.Converter interface.

Let’s create converter then:

...
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.multipart.MultipartFile;
...
public class MultipartFileToImageConverter implements Converter<MultipartFile, Image> {
	@Override
	public Image convert(final MultipartFile source) {
		final Image image = new Image();
                image.setData(source.getBytes());
		image.setName(source.getOriginalFilename());
		return image;
	}
}

We need to register our converter in conversion service, so in application context we add:

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<property name="converters">
		<set>
			<bean class="pl.greenpath.converter.MultipartFileToImageConverter">
		</set>
	</property>
</bean>

I use annotation-based configuration of controllers, so configuration of controller is pretty straightforward:

        @Autowired
        CompanyService _companyService;
        ...
	@RequestMapping(value = "", method = RequestMethod.POST)
	public String doEdit(@Valid final Company company, final Errors errors) {
		if (errors.hasErrors()) {
			return "admin/companies/edit";
		} else {
			_companyService.save(company);
			return "redirect:/admin/companies/" + company.getId();
		}
	}

Going this way we have company object with filled data from HTML form.
To obtain image data we just need to invoke e.g. company.getImage().getName();

The main advantage of this solution is its simplicity. You don’t need to create configuration of editors in your controller. All you need is to create a converter and register it in ConversionService. Spring will do all other stuff for you.

I hope it will help simplifying your web app 😉