In this article I’ll give simple example how to upload image and display it on SpringMVC. We need to add some libs to support this feature. There are:
- Apache common file upload
- Apache common io
First, you need to add multipartresolver bean to your servlet configuration. As for me, here is what I added to my dispatcher-servlet.xml
<mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/css/**" location="/css/"/> <mvc:resources mapping="/img/**" location="/img/" /> <mvc:resources mapping="/font/**" location="/font/" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean>
The key to display your uploaded image is put the image to folder that can be called from your view. From my dispatcher-servlet you can see that I will put the image to folder “img” and call it with url “[baseURL]/img/[nameOfImage]”. Here are some screenshot result of my code.