Spring MVC - File upload Example

 spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc   
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="myproject" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000" />
    </bean>

</beans>

Controller that handle file uploading
package myproject.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;



@Controller
public class MyController {
    @RequestMapping(value={"/"})
    public String home(){
        return "Home";
    }
    @RequestMapping(value="/upload", method = RequestMethod.POST)
    public String upload(@RequestParam("file") MultipartFile file,Model model) throws IOException{
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();//here you can write bytes to any file
            model.addAttribute("ContentType", file.getContentType());
            model.addAttribute("name", file.getName());
            model.addAttribute("OriginalName", file.getOriginalFilename());
            model.addAttribute("size", file.getSize());
   
            file.transferTo(new File("F:/temp/"+file.getOriginalFilename()));
  
            return "FileUploaded";
        }else {
            return "Home";
        }
    }
}

Form from where we upload the file

<html>
<head>
<title>Spring MVC - File Uploading Example</title>
</head>
<body>
    <h1>Spring MVC - File Uploading Example</h1>
    <form method="post" action="upload" enctype="multipart/form-data">
        <input type="file" name="file" />
        <input type="submit" />
    </form>
</body>
</html>

Result page 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>File Uploaded</h1>
<h2>Name: ${name}</h2>
<h2>OriginalName: ${OriginalName}</h2>
<h2>ContentType: ${ContentType}</h2>
<h2>Size: ${size} bytes</h2>
</body>
</html>

List of Jar files 

Popular Posts