Spring MVC 파일 업로드 예제 자습서 - 단일 및 다중 파일
파일 업로드는 모든 웹 애플리케이션에서 매우 일반적인 작업입니다. 우리는 이전에 Struts2 파일 업로드 방법을 보았습니다. 오늘 우리는 Spring 파일 업로드, 특히 단일 및 다중 파일에 대한 Spring MVC 파일 업로드에 대해 배웁니다.
Spring MVC 파일 업로드

Apache Commons FileUpload에 대한 Maven 종속성
우선, 필요한 jar 파일이 웹 애플리케이션의 일부가 되도록 pom.xml 파일에 Apache Commons FileUpload 종속성을 추가해야 합니다. 다음은 내 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>
스프링 파일 업로드 양식 보기
스프링 웹 애플리케이션에서 단일 및 다중 파일 업로드를 허용하기 위해 두 개의 JSP 페이지를 생성합니다. 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 보기 코드:
<%@ 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>
이러한 파일은 간단한 HTML 파일이며 복잡성을 피하기 위해 JSP 또는 Spring 태그를 사용하지 않습니다. 주목해야 할 중요한 점은 양식 enctype이 multipart/form-data여야 한다는 것입니다. 그래야 Spring 웹 애플리케이션이 요청에 처리해야 하는 파일 데이터가 포함되어 있음을 알 수 있습니다. 또한 여러 파일의 경우 입력 필드에서 "file\ 및 "name\ 형식 필드가 동일하므로 데이터가 배열 형식으로 전송됩니다. 입력 배열을 가져 와서 파일 데이터를 구문 분석하고 지정된 파일 이름에 저장합니다.
Spring MVC 멀티파트 구성
멀티파트 요청을 처리하기 위해 Apache Commons FileUpload를 활용하려면 org.springframework.web.multipart.commons.CommonsMultipartResolver
클래스로 multipartResolver
빈을 구성하기만 하면 됩니다. 최종 Spring 구성 파일은 다음과 같습니다. 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>
multipartResolver 빈에 대한 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");
}
}
이 구성을 사용하면 enctype이 multipart/form-data인 모든 요청이 Controller 클래스로 전달되기 전에 multipartResolver에 의해 처리됩니다.
스프링 파일 업로드 컨트롤러 클래스
컨트롤러 클래스 코드는 매우 간단합니다. uploadFile 및 uploadMultipleFile URI에 대한 처리기 메서드를 정의해야 합니다. 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;
}
}
우리의 삶을 더 쉽게 만들고 코드를 더 읽기 쉽게 만드는 Spring 주석의 사용에 주목하십시오. uploadFileHandler
메서드는 단일 파일 업로드 시나리오를 처리하는 데 사용되는 반면 uploadMultipleFileHandler
메서드는 여러 파일 업로드 시나리오를 처리하는 데 사용됩니다. 실제로 우리는 두 시나리오를 모두 처리하는 단일 방법을 가질 수 있습니다. 이제 응용 프로그램을 WAR 파일로 내보내고 Tomcat 서블릿 컨테이너에 배포합니다. 애플리케이션을 실행할 때 아래 이미지는 요청과 응답을 보여줍니다.
Spring MVC 파일 업로드 예제

Spring 파일 업로드 프로젝트 다운로드