Spring MVC 4 File Upload Example using Commons fileupload

In this post we will implement Single and Multiple Fileupload functionality using Spring MultipartResolver. Spring provides build-in multipart support to handle file uploads in a web application.

Spring4MVCFileUploadCommonsExample_img8

Spring4MVCFileUploadCommonsExample_img3

NOTE: A multipart content is the content with enctype=”multipart/form-data”.

Short Overview

Spring provides file upload support using MultiPartResolver interface and provides two out-of-box implementations for that.

  • 1. To use with Apache Commons . Spring CommonsMultipartResolver is a MultipartResolver implementation for use with Apache Commons FileUpload. It requires apache commons-fileupload.jar to be present on classpath. It’s not specific to Servlet 3 environment but it works equally well with Servlet 3.x containers.

    		<dependency>
    			<groupId>commons-fileupload</groupId>
    			<artifactId>commons-fileupload</artifactId>
    			<version>1.3.1</version>
    		</dependency>
    
  • 2. To use with Servlet 3.0 multipart request. Depends on which setup you are using[XML or JavaConfig].

    For XML setup, you need to mark the DispatcherServlet with a "multipart-config" section in web.xml.
    For Annoataion/JavaConfig setup, registering javax.servlet.MultipartConfigElement with DispatcherServlet. OR using @MultipartConfig on a custom servlet.

This post focuses only on CommonsMultipartResolver . Next post shows same example using Servlet 3.0 specific implementations.

Basically we need to do following :

  • Create a Bean of Type CommonsMultipartResolver , specifying few properties related to file uploading.
  • Include Apache Commons commons-fileupload.jar in classpath.
  • Form which contains file upload functionality should specify enctype attribute with multipart content [enctype=”multipart/form-data”]
  • To handle file input, input type must be ‘file’

Something like this:

		<form method="POST" enctype="multipart/form-data" >
		....
			<input type="file" id="file" />
		....
			<input type="submit" value="Upload">
		</form>

Complete Example is discussed below.


Complete Example


Following technologies being used:

  • Spring 4.2.0.RELEASE
  • Apache Commons fille-upload 1.3.1
  • validation-api 1.1.0.Final
  • Bootstrap v3.3.2
  • Maven 3
  • JDK 1.7
  • Tomcat 8.0.21
  • Eclipse JUNO Service Release 2

Let’s begin.

Project Structure

Spring4MVCFileUploadCommonsExample_img1

Declare Dependencies in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.websystique.springmvc</groupId>
  <artifactId>Spring4MVCFileUploadCommonsExample</artifactId>
  <packaging>war</packaging>
  <version>1.0.0</version>
  <name>Spring4MVCFileUploadCommonsExample Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  
    <properties>
		<springframework.version>4.2.0.RELEASE</springframework.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- For user input validation -->
		<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>


	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.2</version>
					<configuration>
						<source>1.7</source>
						<target>1.7</target>
					</configuration>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>2.4</version>
					<configuration>
						<warSourceDirectory>src/main/webapp</warSourceDirectory>
						<warName>Spring4MVCFileUploadCommonsExample</warName>
						<failOnMissingWebXml>false</failOnMissingWebXml>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>

		<finalName>Spring4MVCFileUploadCommonsExample</finalName>
	</build>
</project>

Create Configuration Class

package com.websystique.springmvc.configuration;

import java.io.IOException;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.websystique.springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter{

	@Bean(name="multipartResolver") 
	public CommonsMultipartResolver getResolver() throws IOException{
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		
		//Set the maximum allowed size (in bytes) for each individual file.
		resolver.setMaxUploadSizePerFile(5242880);//5MB
		
		//You may also set other available properties.
		
		return resolver;
	}

	@Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }
    
	@Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
	
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

}

Highlight of this Configuration class is CommonsMultipartResolver Bean. It’s a MultipartResolver implementation for Apache Commons FileUpload. You can set properties like max filesize, size Threshold, headerEncoding etc. In our case we have just specified the max allowed file size in bytes (set to 5 MB).

This Configuration class in XML format will be:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <context:component-scan base-package="com.websystique.springmvc" />
    <mvc:annotation-driven />
 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 
    	<!-- one of the several properties available; the maximum file size in bytes -->
        <property name="maxUploadSizePerFile" value="5242880"/>
    </bean>

    <mvc:resources mapping="/static/**" location="/static/" />
    <mvc:default-servlet-handler />


    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>messages</value>
        </property>
    </bean>


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

Create Model classes

Spring provides org.springframework.web.multipart.MultipartFile which is a representation of an uploaded file received in a multipart request. It provides handy methods like getName(), getContentType(), getBytes(), getInputStream() etc.. which make life bit easier while retrieving information about file being uploaded.

Let’s write a wrapper class to further simply it’s usage in our application


package com.websystique.springmvc.model;

import org.springframework.web.multipart.MultipartFile;

public class FileBucket {

	MultipartFile file;
	
	public MultipartFile getFile() {
		return file;
	}

	public void setFile(MultipartFile file) {
		this.file = file;
	}
}

To demonstrate Multiple uploads example as well, let’s create one more wrapper class.

package com.websystique.springmvc.model;

import java.util.ArrayList;
import java.util.List;

public class MultiFileBucket {

	List<FileBucket> files = new ArrayList<FileBucket>();
	
	public MultiFileBucket(){
		files.add(new FileBucket());
		files.add(new FileBucket());
		files.add(new FileBucket());
	}
	
	public List<FileBucket> getFiles() {
		return files;
	}

	public void setFiles(List<FileBucket> files) {
		this.files = files;
	}
}

This class can handle up to 3 file uploads.

Create Controller class

package com.websystique.springmvc.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.websystique.springmvc.model.FileBucket;
import com.websystique.springmvc.model.MultiFileBucket;
import com.websystique.springmvc.util.FileValidator;
import com.websystique.springmvc.util.MultiFileValidator;

@Controller
public class FileUploadController {

	private static String UPLOAD_LOCATION="C:/mytemp/";
	
	@Autowired
	FileValidator fileValidator;
	

	@Autowired
	MultiFileValidator multiFileValidator;

	
	@InitBinder("fileBucket")
	protected void initBinderFileBucket(WebDataBinder binder) {
	   binder.setValidator(fileValidator);
	}


	@InitBinder("multiFileBucket")
	protected void initBinderMultiFileBucket(WebDataBinder binder) {
	   binder.setValidator(multiFileValidator);
	}

	
	@RequestMapping(value={"/","/welcome"}, method = RequestMethod.GET)
	public String getHomePage(ModelMap model) {
		return "welcome";
	}

	@RequestMapping(value="/singleUpload", method = RequestMethod.GET)
	public String getSingleUploadPage(ModelMap model) {
		FileBucket fileModel = new FileBucket();
		model.addAttribute("fileBucket", fileModel);
		return "singleFileUploader";
	}

	@RequestMapping(value="/singleUpload", method = RequestMethod.POST)
	public String singleFileUpload(@Valid FileBucket fileBucket, BindingResult result, ModelMap model) throws IOException {

		if (result.hasErrors()) {
			System.out.println("validation errors");
			return "singleFileUploader";
		} else {			
			System.out.println("Fetching file");
			MultipartFile multipartFile = fileBucket.getFile();

			//Now do something with file...
			FileCopyUtils.copy(fileBucket.getFile().getBytes(), new File(UPLOAD_LOCATION + fileBucket.getFile().getOriginalFilename()));
			
			String fileName = multipartFile.getOriginalFilename();
			model.addAttribute("fileName", fileName);
			return "success";
		}
	}

	
	@RequestMapping(value="/multiUpload", method = RequestMethod.GET)
	public String getMultiUploadPage(ModelMap model) {
		MultiFileBucket filesModel = new MultiFileBucket();
		model.addAttribute("multiFileBucket", filesModel);
		return "multiFileUploader";
	}

	@RequestMapping(value="/multiUpload", method = RequestMethod.POST)
	public String multiFileUpload(@Valid MultiFileBucket multiFileBucket, BindingResult result, ModelMap model) throws IOException {

		
		if (result.hasErrors()) {
			System.out.println("validation errors in multi upload");
			return "multiFileUploader";
		} else {			
			System.out.println("Fetching files");
			List<String> fileNames= new ArrayList<String>();
			
			//Now do something with file...
			for(FileBucket bucket : multiFileBucket.getFiles()){
				FileCopyUtils.copy(bucket.getFile().getBytes(), new File(UPLOAD_LOCATION + bucket.getFile().getOriginalFilename()));
				fileNames.add(bucket.getFile().getOriginalFilename());
			}
			
			model.addAttribute("fileNames", fileNames);
			return "multiSuccess";
		}
	}
	
	
	
}

Above controller is fairly trivial. It handles GET and POST request for file upload view. Once file is selected from File picker and user clicked on upload, we are simply creating a new file with the same name and bytes content as original file, copying the bytes from original file. For that we are using Spring FileCopyUtils utility class to copy stream from source to destination. In this example, we have specified destination as C:/mytemp folder, all files will end up in this folder.

Create Validators classes

We are using some Validators to verify that user have indeed selected a file to be uploaded. They are shown below.

package com.websystique.springmvc.util;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.websystique.springmvc.model.FileBucket;

@Component
public class FileValidator implements Validator {
	
	public boolean supports(Class<?> clazz) {
		return FileBucket.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		FileBucket file = (FileBucket) obj;
		
		if(file.getFile()!=null){
			if (file.getFile().getSize() == 0) {
				errors.rejectValue("file", "missing.file");
			}
		}
	}
}
package com.websystique.springmvc.util;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.websystique.springmvc.model.FileBucket;
import com.websystique.springmvc.model.MultiFileBucket;

@Component
public class MultiFileValidator implements Validator {
	
	public boolean supports(Class<?> clazz) {
		return MultiFileBucket.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		MultiFileBucket multiBucket = (MultiFileBucket) obj;
		
		int index=0;
		
		for(FileBucket file : multiBucket.getFiles()){
			if(file.getFile()!=null){
				if (file.getFile().getSize() == 0) {
					errors.rejectValue("files["+index+"].file", "missing.file");
				}
			}
			index++;
		}
		
	}
}

messages.properties

missing.file= Please select a file.

Create Views

singleFileUploader.jsp

<%@ 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"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Spring 4 MVC File Upload Example</title>
	<link href="<c:url value='/static/css/bootstrap.css' />"  rel="stylesheet" type="text/css"></link>
	<link href="<c:url value='/static/css/app.css' />" rel="stylesheet" type="text/css"></link>
</head>
<body> 

	<div class="form-container">
		<h1>Spring 4 MVC File Upload Example </h1>
		<form:form method="POST" modelAttribute="fileBucket" enctype="multipart/form-data" class="form-horizontal">
		
			<div class="row">
				<div class="form-group col-md-12">
					<label class="col-md-3 control-lable" for="file">Upload a file</label>
					<div class="col-md-7">
						<form:input type="file" path="file" id="file" class="form-control input-sm"/>
						<div class="has-error">
							<form:errors path="file" class="help-inline"/>
						</div>
					</div>
				</div>
			</div>
	
			<div class="row">
				<div class="form-actions floatRight">
					<input type="submit" value="Upload" class="btn btn-primary btn-sm">
				</div>
			</div>
		</form:form>
		<a href="<c:url value='/welcome' />">Home</a>
	</div>
</body>
</html>

success.jsp

<%@ 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" %>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>File Upload Success</title>
	<link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
	<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
	<div class="success">
		File  <strong>${fileName}</strong> uploaded successfully.
		<br/><br/>
		<a href="<c:url value='/welcome' />">Home</a>	
	</div>
</body>
</html>

multiFileUploader.jsp

<%@ 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"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Spring 4 MVC File Multi Upload Example</title>
	<link href="<c:url value='/static/css/bootstrap.css' />"  rel="stylesheet" type="text/css"></link>
	<link href="<c:url value='/static/css/app.css' />" rel="stylesheet" type="text/css"></link>
</head>
<body> 

	<div class="form-container">
		<h1>Spring 4 MVC Multi File Upload Example </h1>
		<form:form method="POST" modelAttribute="multiFileBucket" enctype="multipart/form-data" class="form-horizontal">
		
			<c:forEach var="v" varStatus="vs" items="${multiFileBucket.files}">
				<form:input type="file" path="files[${vs.index}].file" id="files[${vs.index}].file" class="form-control input-sm"/>
				<div class="has-error">
					<form:errors path="files[${vs.index}].file" class="help-inline"/>
				</div>
			</c:forEach>
			<br/>
			<div class="row">
				<div class="form-actions floatRight">
					<input type="submit" value="Upload" class="btn btn-primary btn-sm">
				</div>
			</div>
		</form:form>
		
		<br/>
		<a href="<c:url value='/welcome' />">Home</a>
	</div>
</body>
</html>

multiSuccess.jsp

<%@ 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" %>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>File Upload Success</title>
	<link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link>
	<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
	<div class="success">
			<c:forEach var="fileName" items="${fileNames}">
				File  <strong>${fileName}</strong> uploaded successfully<br/>
			</c:forEach>
			<br/>
		<a href="<c:url value='/welcome' />">Home</a>
	</div>
</body>
</html>

welcome.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Spring 4 MVC File Upload Example</title>
	<link href="<c:url value='/static/css/bootstrap.css' />"  rel="stylesheet"></link>
	<link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link>
</head>
<body>
	<div class="form-container">
		<h1>Welcome to FileUploader Example</h1>
		
		Click on below links to see FileUpload in action.<br/><br/>
		
		<a href="<c:url value='/singleUpload' />">Single File Upload</a>  OR  <a href="<c:url value='multiUpload' />">Multi File Upload</a>
	</div> 
</body>
</html>

Create Initialization class

package com.websystique.springmvc.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { HelloWorldConfiguration.class };
    }
  
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }
  
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
 
 }

Build, Deploy & Run Application

Now build the war (either by eclipse as was mentioned in previous tutorials) or via maven command line( mvn clean install). Deploy the war to a Servlet 3.0 container . Since here i am using Tomcat, i will simply put this war file into tomcat webapps folder and click on start.bat inside tomcat/bin directory.

Open browser and browse at http://localhost:8080/Spring4MVCFileUploadCommonsExample/

Spring4MVCFileUploadCommonsExample_img2

Now click on Single File Upload link.

Spring4MVCFileUploadCommonsExample_img3

click on Upload, without selecting a file. It should show validation failure message.

Spring4MVCFileUploadCommonsExample_img4

Click on Choose File.

Spring4MVCFileUploadCommonsExample_img5

File picker should be shown. Select a file.

Spring4MVCFileUploadCommonsExample_img6

Clink on Upload. File uploaded.

Spring4MVCFileUploadCommonsExample_img7

You can check Upload folder [C:/mytemp] for uploaded file.

Now Go back, and clink on multi upload link this time.

Spring4MVCFileUploadCommonsExample_img8

Click on upload without any file selection, should receive validation error.

Spring4MVCFileUploadCommonsExample_img9

Select files.

Spring4MVCFileUploadCommonsExample_img10

Clink on Upload. All selected files should be uploaded.

Spring4MVCFileUploadCommonsExample_img11

Finally check the storage folder [C:/mytemp].

Spring4MVCFileUploadCommonsExample_img12

That’s it. Next post shows the same example using Spring 3.0 specific API’s.

Download Source Code



References

If you like tutorials on this site, why not take a step further and connect me on Facebook , Google Plus & Twitter as well? I would love to hear your thoughts on these articles, it will help me improve further our learning process.

If you appreciate the effort I have put in this learning site, help me improve the visibility of this site towards global audience by sharing and linking this site from within and beyond your network. You & your friends can always link my site from your site on www.websystique.com, and share the learning.

After all, we are here to learn together, aren’t we?

  • Pingback: Spring MVC 4 File Download Example - WebSystique()

  • FaisalHyder

    can you post the file upload by Angular JS ? kindly it is very bothering part.

  • FaisalHyder

    There is no need to add methods for File Size check, it can be simply covered by adding
    multipart.maxFileSize=50Mb (whatever size) inside application.properties file in resource folder.

  • FaisalHyder

    Once again , your tutorials are amazing para by para..! thanks alot.! :)

  • Brendo Felipe

    Olá, parabéns pelo post!

    Teria como validar as imagens através das anotações do Bean Validation?

  • fsl4faisal

    Thank you for the wonderful example.

    Served most of my purpose.
    However I have to display the same image to the jsp afterwards meaning ..
    Here is the example:-

    After addition of student with all the data along with profile pic when the user click on add, all the details would be saved in the database and the image would be saved in the server location that we would provide.

    After addition of the student the user would be redirected to view page of the same student with all the details from the database along with the profile pic from the server location.

    How would I do that.
    I checked your other tutorials also and I don’t think download image would work in my case..

    Thank you in advance.

    • FaisalHyder

      we can discuss this in detail, kindly elaborate your approach so i can suggest accordingly

  • Muhammad Fahad Zaeem

    Hi
    i have given all required path .. i have configured tomcat properly.. i successfully run the file download tutorial.. but it is giving me 404 error when i acces url/singleUpload

    • websystique

      Hi Fahad,

      I’ve just run this very project via eclipse+tomcat without any issue. Seems that you still might have issue with tomcat setup. Did you follow each step mentioned in that setup path (specially the one with Deployment Assembly) for this project?

    • Code Coders

      Hi It means that url/singleUpload is not in your context resource try to examine your
      controller for your @Request Mapping(value=” url/singleUpload” ….) and if the resource of that url is not in the created context, might be in your spring config you should set your e.g. . This blog is great….Thanks!!!

  • renam chagas

    Hi Websystique,

    Thank you for the tutorial.

    Im trying to implement this on the springsecurity tutorial you post it (XML) but im getting an error everytime I try to upload.
    - 405 – Request method ‘POST’ not supported

    Do I need to change or add anythign to make it work ?

    Thank you in advance

    • websystique

      Hi Renam, I would need more info. Which SpringSecurity Post are you trying to integrate with?

      BTW, I recall someone discussing somewhat similar issue in past. Look at this post, mainly in comment section, you may find some details over there.

      • renam chagas

        Hi Websystique,

        Im using this post http://websystique.com/spring-security/spring-security-4-password-encoder-bcrypt-example-with-hibernate/

        I check that post but didn`t help im still getting the 405 error

      • renam chagas

        I disable the CSRF and im not getting the http 405 anymore, instead im getting an error java.lang.NullPointerException

        That happen because fileBucket.getFile is returning null

        • renam chagas

          Form Upload:

          ====================

          Controller:

          @RequestMapping(value=”/singleUpload”, method = RequestMethod.POST)

          public String singleFileUpload(@Valid FileBucket fileBucket, BindingResult result, ModelMap model) throws IOException {

          if (result.hasErrors()) {
          System.out.println(“validation errors”);
          return “file_upload_form”;
          } else {
          System.out.println(“Fetching file”);
          MultipartFile multipartFile = fileBucket.getFile();
          String fileName = multipartFile.getOriginalFilename();

          ====================

          Filebucket

          MultipartFile file;
          public MultipartFile getFile() {
          return file;
          }
          public void setFile(MultipartFile file) {
          this.file = file;

          • websystique

            Hi Renam,
            Strange. Did you try with modelAttribute instead of modelattribute in JSP?

  • http://www.isk108.com sai

    “The requested resource is not available”. I get this error

    • websystique

      Hi Sai,
      Which container/javvee versions are you using? Could you please provide some more info (logs/screenshot)?

      • http://www.isk108.com sai

        Hey I use Tomcat 7 and Java 7.

        • websystique

          Hi Sai,

          This is simple context problem. Are you sure you have your pom.xml updated with configuration(as defined in maven-war-plugin above e.g.)?

          • Zoz

            Hi websystique, thanks for this example

            I have the same problem than sai.

            I tried Spring4Hibernate4-Mysql and other and it works perfectly.

            But “/spring-mvc-4-file-upload-example-using-commons-fileupload” and “spring-mvc-4-fileupload-download-hibernate-example” doesn’t work. At url “http://localhost:8080/Spring4MVCFileUploadDownloadWithHibernate/” i got “The requested resource is not available”
            When i create a index.jsp file in webapp, Tomcat show me this file correctly. So i suppose the mapping doesn’t work correctly

            I will test your solution later but i dont know how it will solve the problem

          • websystique

            Hi Zoz,

            Did you go through the link i mentioned in above message to Sai? I have tested this example [I prefer to deploy to an external tomcat instance, not via eclipse] and it is working without any issue. With tomcat inside eclipse, please refer to those links, they will surely help you. Let me know if you still face issues.

          • websystique

            Hi Zoz,
            I’ve create a post about How To setup tomcat with Eclipse using this very project as an example. Every step is shown in detail. Nothing is changed in project, and it works fine.

          • Zoz

            Humm, i tried that with my mars-jee-eclipse, it didn’t work,. After i tried that with my luna-jee-eclipse and it worked.. After i retried that with my mars-jee-eclipse and it work..
            Now i have to test that on my other pc

            Thanks you very much for time spent, and congrats for your site btw
            I find there is really few JEE sample code, so I think it would be interesting to make other

          • Zoz

            ummm, and why the project structure is not the same than the source code ?

        • websystique

          Hi Sai,
          In case you still finding hard to setup tomcat and eclipse together, I’ve just created a post about How To setup tomcat with Eclipse using this very project as an example. Every step is shown in detail. Nothing is changed in project, and it works fine.

      • http://www.isk108.com sai

        Hey,

        Are you sure that we don’t require web.xml at all…?

        • websystique

          No you don’t.

      • http://www.isk108.com sai

        Hey guys,

        I am constrained to tomcat7. and usage of web.xml as I have incorporated JPA initially although I use spring 4.

        I have given maven clean, install and then build.

        As you know we perform something called as tomcat:run goal to run tomcat with eclipse-maven. [Attached screenshot]

        So can you tell me what can be the reason for the error page.

      • http://www.isk108.com sai

        Hey,

        I am able to run when I am deploying war separately into tomcat not via eclipse.

        • websystique

          Hi Sai,
          Have you checked Setup tomcat with Eclipse ?

          • http://www.isk108.com sai

            I am facing this issue

          • websystique

            Hi Sai,
            Your local environment seems corrupted. As i mentioned before, please follow the link to setup your environment correctly.

          • http://www.isk108.com sai

            That’s what I did. I deleted the server and added it back again. It reoccurs.

          • websystique

            Please check your environment variables. There is no backup folder in actual tomcat, why do you have a backup folder there? And the original catalina.policy files is found in conf folder, not under backup.

      • http://www.isk108.com sai

        Still I’ll be trying to integrate this.

        if I have two different images with same name and file type.

        When I saw the logic and tested it then it is overriding ……

  • Pingback: Spring MVC 4 FileUpload-Download Hibernate+MySQL Example - WebSystique()