Tutorial

Spring MVC File Upload Example Tutorial - Single and Multiple Files

Published on August 4, 2022
author

Pankaj

Spring MVC File Upload Example Tutorial - Single and Multiple Files

File Uploading is a very common task in any web application. We have earlier seen how to upload files in Servlet and Struts2 File Uploading. Today we will learn about Spring File upload, specifically Spring MVC File Upload for single and multiple files.

Spring MVC File Upload

Spring MVC framework provides support for uploading files by integrating Apache Commons FileUpload API. The process to upload files is very easy and requires simple configurations. We will create a simple Spring MVC project in STS that will look like below image. Spring File Uplaod Example Project, MultipartFile, MultipartResolver Most of the part is the boiler-plate code generated by STS tool, we will focus on the changes that are required to utilize Spring file upload integration.

Maven Dependencies for Apache Commons FileUpload

First of all, we need to add Apache Commons FileUpload dependencies in our pom.xml file, so that required jar files are part of the web application. Below is the dependency snippet from my pom.xml file.

<!-- Apache Commons FileUpload --> 
<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.3.1</version>
</dependency>

<!-- Apache Commons IO --> 
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>

Spring File Upload Form Views

We will create two JSP pages to allow single and multiple file uploads in spring web application. upload.jsp view code:

<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Upload File Request Page</title>
</head>
<body>
	<form method="POST" action="uploadFile" enctype="multipart/form-data">
		File to upload: <input type="file" name="file"><br /> 
		Name: <input type="text" name="name"><br /> <br /> 
		<input type="submit" value="Upload"> Press here to upload the file!
	</form>	
</body>
</html>

uploadMultiple.jsp view code:

<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
<title>Upload Multiple File Request Page</title>
</head>
<body>
	<form method="POST" action="uploadMultipleFile" enctype="multipart/form-data">
		File1 to upload: <input type="file" name="file"><br /> 
		Name1: <input type="text" name="name"><br /> <br /> 
		File2 to upload: <input type="file" name="file"><br /> 
		Name2: <input type="text" name="name"><br /> <br />
		<input type="submit" value="Upload"> Press here to upload the file!
	</form>
</body>
</html>

Notice that these files are simple HTML files, I am not using any JSP or Spring tags to avoid complexity. The important point to note is that form enctype should be multipart/form-data, so that Spring web application knows that the request contains file data that needs to be processed. Also note that for multiple files, the form field “file” and “name” are the same in the input fields, so that the data will be sent in the form of an array. We will take the input array and parse the file data and store it in the given file name.

Spring MVC Multipart Configuration

To utilize Apache Commons FileUpload for handling multipart requests, all we need to do is configure multipartResolver bean with class as org.springframework.web.multipart.commons.CommonsMultipartResolver. Our final Spring configuration file looks like below. servlet-context.xml code:

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

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
		up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/**" location="/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
		in the /WEB-INF/views directory -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

	<beans:bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

		 <!-- setting maximum upload size -->
		<beans:property name="maxUploadSize" value="100000" />

	</beans:bean>

	<context:component-scan base-package="com.journaldev.spring.controller" />

</beans:beans>

Notice that I am setting maximum upload size limit by providing the maxUploadSize property value for multipartResolver bean. If you will look into the source code of DispatcherServlet class, you will see that a MultipartResolver variable with name multipartResolver is defined and initialized in below method.

private void initMultipartResolver(ApplicationContext context)
  {
    try
    {
      this.multipartResolver = ((MultipartResolver)context.getBean("multipartResolver", MultipartResolver.class));
      if (this.logger.isDebugEnabled()) {
        this.logger.debug("Using MultipartResolver [" + this.multipartResolver + "]");
      }
    }
    catch (NoSuchBeanDefinitionException ex)
    {
      this.multipartResolver = null;
      if (this.logger.isDebugEnabled())
        this.logger.debug("Unable to locate MultipartResolver with name 'multipartResolver': no multipart request handling provided");
    }
  }

With this configuration, any request with enctype as multipart/form-data will be handled by multipartResolver before passing on to the Controller class.

Spring File Upload Controller Class

Controller class code is very simple, we need to define handler methods for the uploadFile and uploadMultipleFile URIs. FileUploadController.java code:

package com.journaldev.spring.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Handles requests for the application file upload requests
 */
@Controller
public class FileUploadController {

	private static final Logger logger = LoggerFactory
			.getLogger(FileUploadController.class);

	/**
	 * Upload single file using Spring Controller
	 */
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public @ResponseBody
	String uploadFileHandler(@RequestParam("name") String name,
			@RequestParam("file") MultipartFile file) {

		if (!file.isEmpty()) {
			try {
				byte[] bytes = file.getBytes();

				// Creating the directory to store file
				String rootPath = System.getProperty("catalina.home");
				File dir = new File(rootPath + File.separator + "tmpFiles");
				if (!dir.exists())
					dir.mkdirs();

				// Create the file on server
				File serverFile = new File(dir.getAbsolutePath()
						+ File.separator + name);
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(serverFile));
				stream.write(bytes);
				stream.close();

				logger.info("Server File Location="
						+ serverFile.getAbsolutePath());

				return "You successfully uploaded file=" + name;
			} catch (Exception e) {
				return "You failed to upload " + name + " => " + e.getMessage();
			}
		} else {
			return "You failed to upload " + name
					+ " because the file was empty.";
		}
	}

	/**
	 * Upload multiple file using Spring Controller
	 */
	@RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST)
	public @ResponseBody
	String uploadMultipleFileHandler(@RequestParam("name") String[] names,
			@RequestParam("file") MultipartFile[] files) {

		if (files.length != names.length)
			return "Mandatory information missing";

		String message = "";
		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];
			String name = names[i];
			try {
				byte[] bytes = file.getBytes();

				// Creating the directory to store file
				String rootPath = System.getProperty("catalina.home");
				File dir = new File(rootPath + File.separator + "tmpFiles");
				if (!dir.exists())
					dir.mkdirs();

				// Create the file on server
				File serverFile = new File(dir.getAbsolutePath()
						+ File.separator + name);
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(serverFile));
				stream.write(bytes);
				stream.close();

				logger.info("Server File Location="
						+ serverFile.getAbsolutePath());

				message = message + "You successfully uploaded file=" + name
						+ "<br />";
			} catch (Exception e) {
				return "You failed to upload " + name + " => " + e.getMessage();
			}
		}
		return message;
	}
}

Notice the use of Spring annotations that make our life easier and code looks more readable. uploadFileHandler method is used to handle single file upload scenario whereas uploadMultipleFileHandler method is used to handle multiple files upload scenario. Actually we could have a single method to handle both the scenarios. Now export the application as WAR file and deploy it into Tomcat servlet container. When we run our application, below images shows us the request and responses.

Spring MVC File Upload Example

Spring MVC Single File Upload Form Spring MVC Single File Upload Response Spring MVC Multiple File Upload Example Spring Multiple File Upload Response You can check the server logs to know the location where the files have been stored. Download the project from the above link and play around with it to learn more.

Download Spring File Upload Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Category:
Tutorial
Tags:

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
March 25, 2014

Thanks for this example. I had a problem with iexplorer 8, i don´t know why @ResponseBody don´t like it and i have to add “produces = “text/plain”” in RequestMapping: @RequestMapping(value = “/uploadFile”, method = RequestMethod.POST ,produces = “text/plain”) public @ResponseBody String uploadFileHandler(@RequestParam(“name”) String name, @RequestParam(“file”) MultipartFile file) { … … … … … Sorry for my english and thank you one more time for this good example. ;)

- CrAfTy

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 16, 2014

    Got this tutorial working in one shot. Thanks for the clear explanation!!!

    - Rizzo7

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      July 30, 2014

      Good one. thanks…

      - Kumar

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        August 29, 2014

        Nice tutorial. Thanks

        - Chris Wong

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          September 17, 2014

          It shows me “You failed to upload => D:\shubhamTmp (Access is denied)”. At any given location it shows me “Access is denied”. What should I do now?

          - Shubham Chaurasia

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          September 17, 2014

          Sorry my mistake. The issue is resolved now. Thanks for a great tutorial.

          - Shubham Chaurasia

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          November 2, 2014

          Hi, Even I’m getting the same error you mentioned (Access is denied). Please tell me how you resolved the issue.

          - Hareesh Alil Vedurajan

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          January 23, 2015

          use catalina.base instead of catalina.home String rootPath = System.getProperty(“catalina.home”); String rootPath = System.getProperty("catalina.base );

          - Anurag Dikshit

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            December 27, 2014

            hi shubham, i am facing same issue can you please tell me how you resolved the issue

            - santu

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              September 19, 2014

              Very nice tutorial… But, What changes do i make for …i want to store with same name for file that of orignal name where its get browsed…means if i browsed file named “abc.txt”…so how can i store with that abc(same name )…

              - AKRICK

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              September 27, 2014

              In controller, use getOriginalFilename() through MultipartFile’s instance. Here file.getOriginalFilename(); returns the name of original file as string.

              - Shubham Chaurasia

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                September 22, 2014

                thanku soooooooooooo much for ur awesome code! it helpd us a lottttttt… :) :) :) :)

                - Akshay

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  October 7, 2014

                  It’s really great example Mr. Pankaj… Thanks for this post Anyways I am not able to upload large file with the above code. Actually I am trying to upload a movie of size 3 GB. The file is not getting uploaded and the control is entering into Catch block with Null exception message… Means not able to see any exception also. When I choose the movie and click on upload it is keep waiting for around 2 mins and showing an error message saying “You failed to upload mm => null” Even I have increased the value for CommonsMultipartResolver’s maxUploadSize (in the spring configuration file). Can anybody please help me out on this… Thanks in advance… :) :) :)

                  - Shekar

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    November 25, 2014

                    Thanks for this example. I have a question regarding the file path. Do you know how I am able to put the file path in a database? But before this, how to get the file path in the first place?

                    - Arvie

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    November 25, 2014

                    Use File.getCanonicalPath() or File.getAbsolutePath() to get the path details. You can save it as String in database column.

                    - Pankaj

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      December 12, 2014

                      Thanks for this post! A question: where should i put my uploaded images so i can show it on my site? For example: I upload a file.jpg and put it where, so i can then show i dinamically page with a Thanks!

                      - Renan

                      JournalDev
                      DigitalOcean Employee
                      DigitalOcean Employee badge
                      December 12, 2014

                      You can create a folder for images and then use “img” HTML tag to embed images from the server location.

                      - Pankaj

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        January 15, 2015

                        how do we upload multiple image file in database using spring mvc.Please share some idea about this one.

                        - deepak

                        JournalDev
                        DigitalOcean Employee
                        DigitalOcean Employee badge
                        January 19, 2015

                        Hi,I have one problem .i want to edit existed image. how could i do that

                        - sekhar

                          JournalDev
                          DigitalOcean Employee
                          DigitalOcean Employee badge
                          January 28, 2015

                          I want to read the data of that excel file please share the example

                          - Parag

                            JournalDev
                            DigitalOcean Employee
                            DigitalOcean Employee badge
                            February 2, 2015

                            hi trying to run the application in tomcat but it is throwing below exception. HTTP Status 400 - Required String parameter ‘name’ is not present can you explain what it is.

                            - suresh

                              JournalDev
                              DigitalOcean Employee
                              DigitalOcean Employee badge
                              February 5, 2015

                              Thanks for this Great Tutorial. Working Fine !!!.

                              - Aravinthan.K

                                JournalDev
                                DigitalOcean Employee
                                DigitalOcean Employee badge
                                February 12, 2015

                                Can you tell me how to do the same thing if i have to select the file from a remote server??

                                - Akhil

                                  JournalDev
                                  DigitalOcean Employee
                                  DigitalOcean Employee badge
                                  February 12, 2015

                                  I have Successfully saved my files into particular folder . Thanks. How to update images and Files ?

                                  - Aravinthan.K

                                    JournalDev
                                    DigitalOcean Employee
                                    DigitalOcean Employee badge
                                    February 14, 2015

                                    Thanks Pankaj. Your tutorial helped me a lot.

                                    - Binoy KB

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      February 15, 2015

                                      Hey Pankaj, I am able to upload file if I use one file upload option but when I use multiplefile upload option then in controller I always see one file even if I upload 2 images. I had seen one image in controller using debug option in eclipse. Can you please let me know what I am missing? I will really appreciate your help. Thanks, binoy.

                                      - Binoy KB

                                      JournalDev
                                      DigitalOcean Employee
                                      DigitalOcean Employee badge
                                      February 15, 2015

                                      Never mind my last request. I have solved it myself by upgrading spring 3.0.1 to spring 4.1.4. That was a bug in spring 3.0.1 and that is resolved by spring spring 3.1.0.

                                      - Binoy KB

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        February 26, 2015

                                        Hi, How can i upload larger file like 200 and 300 MB file?

                                        - Ravish

                                        JournalDev
                                        DigitalOcean Employee
                                        DigitalOcean Employee badge
                                        April 6, 2015

                                        Just increase value of maxUploadSize in servlet-context.xml

                                        - Ivan

                                          JournalDev
                                          DigitalOcean Employee
                                          DigitalOcean Employee badge
                                          March 31, 2015

                                          sir i want to upload files on server but also want to create thumbnails of the image please help me

                                          - Prabal Singh

                                            JournalDev
                                            DigitalOcean Employee
                                            DigitalOcean Employee badge
                                            April 9, 2015

                                            Can you explain, how could I update or replace a (“multipart file”) defined as (“lob” in model ) to the mysql database.

                                            - Saurav

                                              JournalDev
                                              DigitalOcean Employee
                                              DigitalOcean Employee badge
                                              May 5, 2015

                                              Sir, Its really good tutorial. But can you explain how to retrieve file from this folder? I tried but it give me Not allowed to load local resource error. please response.

                                              - sudeep

                                                JournalDev
                                                DigitalOcean Employee
                                                DigitalOcean Employee badge
                                                June 17, 2015

                                                Can you provide a RestTemplate client example to call the rest service

                                                - asit

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  June 18, 2015

                                                  Hi. Everything works great!!!. Just one question: If I use the original file name with is called like this: myFilé.txt the file uploaded is stored like this: myFilé.txt Do you know how to fix this?? Thanks anyway!!!

                                                  - David

                                                  JournalDev
                                                  DigitalOcean Employee
                                                  DigitalOcean Employee badge
                                                  February 19, 2017

                                                  problably is your pageEncoding, try changing ISO-8859-1 or UTF-8

                                                  - Mariana

                                                    JournalDev
                                                    DigitalOcean Employee
                                                    DigitalOcean Employee badge
                                                    April 16, 2017

                                                    // Create the file on server File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename()); This line code change will work for you.

                                                    - abhishek kumar

                                                      JournalDev
                                                      DigitalOcean Employee
                                                      DigitalOcean Employee badge
                                                      March 29, 2020

                                                      Hi, @David, use this method “encodeURIComponent(‘nameOfFileWhichContainsAccentLike_myFilé.txt’)” Get the file name > pass that name to this method via Javascript Or jQuery or something else > forward the encoded name to the controller. When you will print that name in controller Or when controller returned that to view it will show perfectly like ‘myFilé.txt’.

                                                      - Shubh Srivastava

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        July 5, 2015

                                                        I have this task to upload a CSV to 2 different tables(user- having userid,username,userhobby,userhealthstatus,userlocation-having Country,city,pincode) in the database using a single multipart form which only provides values for only a few columns(say -username,userhobby,pincode) in those 2 tables. Could you please help show how this can be achieved.

                                                        - Kiran

                                                        JournalDev
                                                        DigitalOcean Employee
                                                        DigitalOcean Employee badge
                                                        November 7, 2019

                                                        i think you should use spring batch

                                                        - sagar

                                                          JournalDev
                                                          DigitalOcean Employee
                                                          DigitalOcean Employee badge
                                                          August 8, 2015

                                                          Hi Pankaj, This tutorial is helped me lot to understand the file upload in spring MVC. In this example after submitting the POST request, it displays the status(Success/filure). I wanted to show the message in the same jsp page which contains other fields(it gets data from db) also. I have implemented controller same way, what I can do changes so I can get the same jsp page with other fields data and upload message in same page. Please help me on this. my jsp page: File to upload: Server Name 0){ for(VmclassObj vm:vms){ %> <td id=‘name_’> <input type=“checkbox” name=‘devices’ id=‘’ onClick=“toggleSelectAll(false)”>

                                                          - nishi

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            October 12, 2015

                                                            I am getting the 400-bad-request when uploading the file with more than 2 MB size. Can you please help me?

                                                            - Jayshree

                                                            JournalDev
                                                            DigitalOcean Employee
                                                            DigitalOcean Employee badge
                                                            October 12, 2015

                                                            It’s because of maxUploadSize configuration in spring context file, increase the size as per your requirement.

                                                            - Pankaj

                                                              JournalDev
                                                              DigitalOcean Employee
                                                              DigitalOcean Employee badge
                                                              December 9, 2015

                                                              Hi, Thanks for the tutorial. Can U show me how to override the MVC multipart resolver to customize it?

                                                              - Luciano

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                April 8, 2016

                                                                Thanks for this example But I deploy this example in tomcat and upload file. It show me “You failed to upload => E:\apache-tomcat-8.0.26-2\tmpFiles (Access is denied)”; I have used both “catalina.home” and “catalina.base”, but it doesn’t work… Could you give me some advices? Sorry for poor English…

                                                                - pj_zhong

                                                                JournalDev
                                                                DigitalOcean Employee
                                                                DigitalOcean Employee badge
                                                                January 13, 2017

                                                                plz. some one answer the about mention problem. as i am too getting the same error

                                                                - vinita singh

                                                                  JournalDev
                                                                  DigitalOcean Employee
                                                                  DigitalOcean Employee badge
                                                                  September 5, 2017

                                                                  I tried many times with different file storage location folders but I got same error as Access is Denied … Someone suggest me the way to get rid of it. Regards

                                                                  - psd

                                                                    JournalDev
                                                                    DigitalOcean Employee
                                                                    DigitalOcean Employee badge
                                                                    April 22, 2016

                                                                    I receive the return message on the same page?

                                                                    - anderson brito sousa

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      May 7, 2016

                                                                      I tried this tutorial but I get follwing exception java.lang.ClassNotFoundException: org.springframework.web.multipart.commons.CommansMultipartResolver I doensn’t able to understand why this exception comes Please help me how to resolve this exception.

                                                                      - Abhijeet Kale

                                                                      JournalDev
                                                                      DigitalOcean Employee
                                                                      DigitalOcean Employee badge
                                                                      November 22, 2016

                                                                      Add two jar as mentioned in pom.xml 1.commons-fileupload-1.3.1.jar 2. commons-io-2.4.jar

                                                                      - Surendra

                                                                        JournalDev
                                                                        DigitalOcean Employee
                                                                        DigitalOcean Employee badge
                                                                        November 15, 2016

                                                                        Very helpful tutorial. Also thanks for the webpage face lift.

                                                                        - Surakshar

                                                                          JournalDev
                                                                          DigitalOcean Employee
                                                                          DigitalOcean Employee badge
                                                                          December 29, 2016

                                                                          How can I get the progress while uploading the file?

                                                                          - Prabhat

                                                                            JournalDev
                                                                            DigitalOcean Employee
                                                                            DigitalOcean Employee badge
                                                                            February 27, 2017

                                                                            Hi, your tutorial it’s really helpfull, but I have a question. Where can I locate the files? because where you are saving them they are erase every time you deploy the application, and place the into de project it’s not an option, Do you have any advise? thanks

                                                                            - Angelica

                                                                              JournalDev
                                                                              DigitalOcean Employee
                                                                              DigitalOcean Employee badge
                                                                              June 22, 2017

                                                                              Hi Pankaj, Could you please also explain how we can return file content from spring controller.

                                                                              - Sahas

                                                                                JournalDev
                                                                                DigitalOcean Employee
                                                                                DigitalOcean Employee badge
                                                                                October 3, 2017

                                                                                i have a program than submit things to db . i want to add submit file to my code. i want help code in controller @RequestMapping(value = “/rest/add-new-record/{tableName}”, method = RequestMethod.POST) public String addNewRecord(@PathVariable String tableName, @RequestParam Map allRequestParams, HttpServletRequest request) throws SQLException { tableContentService.addNewRow(allRequestParams, tableName); TableMetaData metaData=getTableMetaData(tableName); metaData.getColumnMetaData(); ColumnMetaData columnMetaData=new ColumnMetaData(); return “content-manager”; } code in service public void addNewRow(Map recordData, String tableName) throws SQLException { tableContentDAO.addNewRecord(recordData,tableName, null); } //class java public void addNewRecord(Map recordData, String tableName, String id) throws SQLException { OracleConnection connection = (OracleConnection) getConnection(); Statement statement; Set columnNames = recordData.keySet(); List listValues = new ArrayList(); ResultSet resultSet = null; String pk_column = null; DatabaseMetaData databaseMetaData = connection.getMetaData(); resultSet = databaseMetaData.getPrimaryKeys(null, connection.getUserName(), tableName); while (resultSet.next()) { pk_column = resultSet.getString(“COLUMN_NAME”); } // System.out.println(“pk_column==================”+ pk_column); for (Map.Entry stringStringEntry : recordData.entrySet()) { String help = null; if (!stringStringEntry.getKey().equals(pk_column)) { if (isValidDate((String) stringStringEntry.getValue())) { help = " TO_DATE(’ " + stringStringEntry.getValue() + " ‘, ‘YYYY/MM/DD’)“; listValues.add(help); }//if is file else { listValues.add((String) stringStringEntry.getValue()); } } } StringJoiner columnNameJoiner = new StringJoiner(”,“); columnNameJoiner.add(pk_column); // System.out.println(“columnNameJoiner=================”+ columnNameJoiner); for (String columnName : columnNames) { if (columnName != null && !columnName.equals(pk_column) && !columnName.equals(“username”)) { columnNameJoiner.add(columnName); } } // System.out.println(“columnNameJoiner=================”+ columnNameJoiner); StringJoiner recordDataJoiner = new StringJoiner(”,“); for (String value : listValues) { if (value.contains(“TO_DATE”)) { recordDataJoiner.add(value); } else { recordDataJoiner.add(”’" + value + “'”); } } //******************************************************************************************************* String query; // columnNameJoiner.toString() ID,ID,NAME /*if (id == null) { query = String.format(“Insert into %s (%s) values” + " ((SELECT COALESCE(MAX(%s)+1,1) FROM %s) ,%s)“, tableName, columnNameJoiner.toString(), pk_column, tableName, recordDataJoiner.toString()); } else { query = String.format(“Insert into %s (%s) values (%s ,%s)”, tableName, columnNameJoiner.toString(), id, recordDataJoiner.toString()); } }*/ query = String.format(“Insert into %s (%s) values” + " ((SELECT COALESCE(MAX(%s)+1,1) FROM %s) ,%s)”, tableName, columnNameJoiner.toString(), pk_column, tableName, recordDataJoiner.toString()); System.out.println(query); statement = connection.createStatement(); statement.executeQuery(query); JdbcUtils.closeConnection(connection); } code in js function createFile(field) { var required = field.required ? “required” : “”; var html=“”; //todo AliMohamadi: readable writable difference // var disabled = field. html +=“”; html +=“”; html += field.comment; html += “”; return html; }

                                                                                - kalantary

                                                                                  JournalDev
                                                                                  DigitalOcean Employee
                                                                                  DigitalOcean Employee badge
                                                                                  November 20, 2017

                                                                                  Did any one faces MVC Portlet Mode mappings conflict between method and type level: [uploadFile] versus [VIEW]

                                                                                  - Jam

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    February 7, 2018

                                                                                    I am getting the error “Application download did not succeed. Check network connection.”. Upto Server File Creation Program works fine.But the folder is not created at specified location. Plz reply.

                                                                                    - Sujata

                                                                                    JournalDev
                                                                                    DigitalOcean Employee
                                                                                    DigitalOcean Employee badge
                                                                                    February 7, 2018

                                                                                    If folder is not getting created, then it could be the issue that your user account don’t have required privileges.

                                                                                    - Pankaj

                                                                                      JournalDev
                                                                                      DigitalOcean Employee
                                                                                      DigitalOcean Employee badge
                                                                                      February 16, 2018

                                                                                      hi could you explain how to run it with apache tompcat 8.5 thanks

                                                                                      - Cristian

                                                                                        JournalDev
                                                                                        DigitalOcean Employee
                                                                                        DigitalOcean Employee badge
                                                                                        April 18, 2018

                                                                                        Getting status 400-Required String Parameter ‘name’ is not present

                                                                                        - Himanshu Sharma

                                                                                          JournalDev
                                                                                          DigitalOcean Employee
                                                                                          DigitalOcean Employee badge
                                                                                          August 7, 2018

                                                                                          very helpful tutorial. can you please provide me annotation based configuration as i don want to use servlet-contex.xml and root-contex.xml

                                                                                          - vishwa

                                                                                            JournalDev
                                                                                            DigitalOcean Employee
                                                                                            DigitalOcean Employee badge
                                                                                            October 6, 2019

                                                                                            HI i am getting 404 not found. My controller mapping should be correct because i am able to reach other methods in the same class which are not handling multipart. Servlet.xml: Controller: exactly same Jsp: same code copied to my jsp Thanks in advance Manu

                                                                                            - Manu Gupta

                                                                                              JournalDev
                                                                                              DigitalOcean Employee
                                                                                              DigitalOcean Employee badge
                                                                                              January 10, 2020

                                                                                              great help full sir

                                                                                              - agastyakumar

                                                                                                JournalDev
                                                                                                DigitalOcean Employee
                                                                                                DigitalOcean Employee badge
                                                                                                June 5, 2020

                                                                                                public List uploadMultipleFiles(@RequestParam(“files”) MultipartFile[] files) { I am trying this singnature but facing the below error, Please help “message”: “Current request is not a multipart request”, “trace”: “org.springframework.web.multipart.MultipartException: Current request is not a multipart request\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:158)\n\tat org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.base/java.lang.Thread.run(Thread.java:830)\n”

                                                                                                - Abhishek

                                                                                                  Try DigitalOcean for free

                                                                                                  Click below to sign up and get $200 of credit to try our products over 60 days!

                                                                                                  Sign up

                                                                                                  Join the Tech Talk
                                                                                                  Success! Thank you! Please check your email for further details.

                                                                                                  Please complete your information!

                                                                                                  Become a contributor for community

                                                                                                  Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                                                                                                  DigitalOcean Documentation

                                                                                                  Full documentation for every DigitalOcean product.

                                                                                                  Resources for startups and SMBs

                                                                                                  The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                                                                                                  Get our newsletter

                                                                                                  Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                                                                                                  New accounts only. By submitting your email you agree to our Privacy Policy

                                                                                                  The developer cloud

                                                                                                  Scale up as you grow — whether you're running one virtual machine or ten thousand.

                                                                                                  Get started for free

                                                                                                  Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                                                                                                  *This promotional offer applies to new accounts only.