Spring MVC文件上传示例教程–单个和多个文件

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.

在任何Web应用程序中,文件上传都是非常常见的任务。 我们之前已经看到了如何在ServletStruts2 File Uploading中上传文件 。 今天,我们将学习有关Spring File Upload的知识 ,特别是针对单个和多个文件的Spring MVC File Upload

Spring MVC文件上传 (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 MVC框架通过集成Apache Commons FileUpload API提供了对上传文件的支持。 上传文件的过程非常简单,并且需要简单的配置。 我们将在STS中创建一个简单的Spring MVC项目,如下图所示。

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.

大部分是STS工具生成的样板代码,我们将重点介绍利用Spring文件上传集成所需的更改。

Apache Commons FileUpload的Maven依赖关系 (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.

首先,我们需要在pom.xml文件中添加Apache Commons FileUpload依赖项,以便所需的jar文件成为Web应用程序的一部分。 以下是我的pom.xml文件中的依赖项片段。

<!-- 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文件上传表单视图 (Spring File Upload Form Views)

We will create two JSP pages to allow single and multiple file uploads in spring web application.

我们将创建两个JSP页面,以允许在Spring Web应用程序中上传单个和多个文件。

upload.jsp view code:

upload.jsp查看代码:

<%@ 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:

uploadMultiple.jsp查看代码:

<%@ 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.

注意,这些文件是简单HTML文件,为了避免复杂性,我没有使用任何JSP或Spring标记。 需要注意的重要一点是,表单enctype应该是multipart / form-data ,以便Spring Web应用程序知道请求中包含需要处理的文件数据。

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多部分配置 (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.

要利用Apache Commons FileUpload处理多部分请求,我们要做的就是配置class为org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver bean。

Our final Spring configuration file looks like below.

我们最终的Spring配置文件如下所示。

servlet-context.xml code:

servlet-context.xml代码:

<?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.

请注意,我通过为multipartResolver bean提供maxUploadSize属性值来设置最大上传大小限制。 如果您查看DispatcherServlet类的源代码,将会看到在下面的方法中定义并初始化了一个名为multipartResolver的MultipartResolver变量。

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.

使用此配置,在传递给Controller类之前,multipartResolver将处理enctype为multipart / form-data的任何请求。

Spring文件上传控制器类 (Spring File Upload Controller Class)

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

控制器类代码非常简单,我们需要为uploadFileuploadMultipleFile URI定义处理程序方法。

FileUploadController.java code:

FileUploadController.java代码:

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.

请注意,使用Spring注释使我们的生活更轻松,代码看起来更易读。

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.

uploadFileHandler方法用于处理单个文件上传方案,而uploadMultipleFileHandler方法用于处理多个文件上传方案。 实际上,我们可以使用一种方法来处理这两种情况。

Now export the application as WAR file and deploy it into Tomcat servlet container.

现在将应用程序导出为WAR文件,并将其部署到Tomcat Servlet容器中。

When we run our application, below images shows us the request and responses.

当我们运行我们的应用程序时,下面的图像向我们显示了请求和响应。

Spring MVC文件上传示例 (Spring MVC File Upload Example)

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.

从上面的链接下载该项目并进行试用以了解更多信息。

翻译自: https://www.journaldev.com/2573/spring-mvc-file-upload-example-single-multiple-files

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值