Spring MVCファイルのアップロード例 – Commons FileUpload
この記事では、 `CommonsMultipartResolver`を使ってSpring MVC Webアプリケーションでのファイルアップロードを処理する方法を説明します。
使用されるツール:
-
Spring 4.3.5.RELEASE
-
commons-fileupload 1.3.2
-
Maven 3
-
Tomcat 7または8とJetty 8,9
1.プロジェクトの構成
標準のMavenプロジェクト構造。
プロジェクトの依存関係
2.1 `commons-fileupload.jar`が必要です。
pom.xml
<!-- Apache Commons Upload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency>
2.2 Maven Pomファイルを完成させる。
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.mkyong</groupId> <artifactId>spring-mvc-file-upload</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Spring MVC file upload</name> <properties> <jdk.version>1.8</jdk.version> <spring.version>4.3.5.RELEASE</spring.version> <jstl.version>1.2</jstl.version> <servletapi.version>3.1.0</servletapi.version> <commons.fileupload.version>1.3.2</commons.fileupload.version> <logback.version>1.1.3</logback.version> <jcl.slf4j.version>1.7.12</jcl.slf4j.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- compile only, deployed container will provide this --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servletapi.version}</version> <scope>provided</scope> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${jcl.slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- Apache Commons Upload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons.fileupload.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> <!-- embedded Jetty server, for testing --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.11.v20150529</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/spring4upload</contextPath> </webApp> </configuration> </plugin> <!-- configure Eclipse workspace --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <version>2.9</version> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> <wtpversion>2.0</wtpversion> <wtpContextName>/spring4upload</wtpContextName> </configuration> </plugin> </plugins> </build> </project>
ターミナル
$ mvn dependency:tree[INFO]Scanning for projects...[INFO][INFO]------------------------------------------------------------------------[INFO]Building Spring MVC file upload 1.0-SNAPSHOT[INFO]------------------------------------------------------------------------[INFO][INFO]--- maven-dependency-plugin:2.8:tree (default-cli) @ spring-mvc-file-upload ---[INFO]com.mkyong:spring-mvc-file-upload:war:1.0-SNAPSHOT[INFO]+- org.springframework:spring-webmvc:jar:4.3.5.RELEASE:compile[INFO]| +- org.springframework:spring-aop:jar:4.3.5.RELEASE:compile[INFO]| +- org.springframework:spring-beans:jar:4.3.5.RELEASE:compile[INFO]| +- org.springframework:spring-context:jar:4.3.5.RELEASE:compile[INFO]| +- org.springframework:spring-core:jar:4.3.5.RELEASE:compile[INFO]| +- org.springframework:spring-expression:jar:4.3.5.RELEASE:compile[INFO]| \- org.springframework:spring-web:jar:4.3.5.RELEASE:compile[INFO]+- javax.servlet:jstl:jar:1.2:compile[INFO]+- javax.servlet:javax.servlet-api:jar:3.1.0:provided[INFO]+- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile[INFO]| \- org.slf4j:slf4j-api:jar:1.7.12:compile[INFO]+- ch.qos.logback:logback-classic:jar:1.1.3:compile[INFO]| \- ch.qos.logback:logback-core:jar:1.1.3:compile[INFO]\- commons-fileupload:commons-fileupload:jar:1.3.2:compile[INFO] \- commons-io:commons-io:jar:2.2:compile
3. CommonsMultipartResolver
`multipartResolver`ビーンを作成します。
SpringWebMvcConfig.java
package com.mkyong; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @EnableWebMvc @Configuration @ComponentScan({"com.mkyong"}) public class SpringWebMvcConfig extends WebMvcConfigurerAdapter { private int maxUploadSizeInMb = 5 ** 1024 ** 1024;//5 MB @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/jsp/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Bean public CommonsMultipartResolver multipartResolver() { CommonsMultipartResolver cmr = new CommonsMultipartResolver(); cmr.setMaxUploadSize(maxUploadSizeInMb ** 2); cmr.setMaxUploadSizePerFile(maxUploadSizeInMb);//bytes return cmr; } }
完了すると、ファイルアップロードの設定が正しく設定されます。
4. ServletInitializer
標準的なサーブレット初期化子。何も特別なものはありません。
MyWebInitializer.java
package com.mkyong; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[]getServletConfigClasses() { return new Class[]{SpringWebMvcConfig.class}; } @Override protected String[]getServletMappings() { return new String[]{"/"}; } @Override protected Class<?>[]getRootConfigClasses() { return null; } }
5.スプリングコントローラ
5.1アップロードされたファイルは `MultipartFile`にマップされます
UploadController.java
package com.mkyong.controller; import com.mkyong.model.UploadForm; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.StringJoiner; @Controller public class UploadController { //save uploaded file to this folder private static String UPLOADED__FOLDER = "F://temp//"; @GetMapping("/") public String index() { return "upload"; } @PostMapping("/upload") public String multiFileUpload(@ModelAttribute UploadForm form, RedirectAttributes redirectAttributes) { StringJoiner sj = new StringJoiner(" , "); for (MultipartFile file : form.getFiles()) { if (file.isEmpty()) { continue;//next pls } try { byte[]bytes = file.getBytes(); Path path = Paths.get(UPLOADED__FOLDER + file.getOriginalFilename()); Files.write(path, bytes); sj.add(file.getOriginalFilename()); } catch (IOException e) { e.printStackTrace(); } } String uploadedFileName = sj.toString(); if (StringUtils.isEmpty(uploadedFileName)) { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); } else { redirectAttributes.addFlashAttribute("message", "You successfully uploaded '" + uploadedFileName + "'"); } return "redirect:/uploadStatus"; } @GetMapping("/uploadStatus") public String uploadStatus() { return "uploadStatus"; } }
5.2 HTML値をこのモデルにマップします。
UploadForm.java
package com.mkyong.model; import org.springframework.web.multipart.MultipartFile; public class UploadForm { MultipartFile[]files; public MultipartFile[]getFiles() { return files; } public void setFiles(MultipartFile[]files) { this.files = files; } }
6. Spring JSPビュー
upload.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <body> <h1>Spring MVC multi files upload example</h1> <form:form method="POST" action="${pageContext.request.contextPath}/uploadMulti" modelAttribute="uploadForm" enctype="multipart/form-data"> <input type="file" name="files"/><br/> <input type="file" name="files"/><br/> <input type="file" name="files"/><br/><br/> <input type="submit" value="Submit"/> </form:form> </body> </html>
uploadStatus.jsp
<html> <body> <h1>Upload Status</h1> <h2>Message : ${message}</h2> </body> </html>
7.最大アップロードサイズを超えました
最大アップロードサイズを処理するには例外を超え、
@ ControllerAdvice`を宣言し、
MaxUploadSizeExceededException`をキャッチします。
GlobalExceptionHandler.jsp
package com.mkyong.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @ControllerAdvice public class GlobalExceptionHandler { //commons-fileupload @ExceptionHandler(MaxUploadSizeExceededException.class) public String handleError2(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", e.getCause().getMessage()); return "redirect:/uploadStatus"; } }
8.デモ
8.1以下のソースコードを入手し、埋め込まれたJettyサーバ `mvn jetty:run`でテストしてください。コンテキストは `/spring4upload`です
ターミナル
project $ mvn jetty:run//...[INFO]Started o.e.j.m.p.JettyWebAppContext@341672e{/spring4upload, file:/SpringMVCUploadExample/src/main/webapp/,AVAILABLE}{file:/SpringMVCUploadExample/src/main/webapp/}[WARNING]!RequestLog[INFO]Started ServerConnector@3ba1308d{HTTP/1.1}{0.0.0.0:8080}[INFO]Started @3743ms[INFO]Started Jetty Server[INFO]Starting scanner at interval of 10 seconds.
8.2 `http://localhost:8080/spring4upload`にアクセスし、いくつかのファイルを選択してアップロードします。
8.3結果。
8.3 10MB以上のファイルサイズをアップロードすると、この例外ページが表示されます。
-
接続のリセット** + WebアプリケーションをTomcatにデプロイし、ファイルサイズが例外を超えた場合、これはTomcatの `maxSwallowSize`設定によって引き起こされる可能性があります。これを読む –
Springファイルのアップロードと接続のリセットの問題
8.ソースコードをダウンロードする
ダウンロード:
spring-file-upload-commons-fileupload.zip
(5 KB)
参考文献
マルチパート(ファイルアップロード)のサポート]。 link://spring-mvc/spring-mvc-file-upload-example/[Spring MVCファイル
アップロードの例]。リンク://spring/spring-file-upload-and-connection-reset-issue/[Spring
ファイルアップロードと接続リセットの問題]
リンク://タグ/ファイルアップロード/[ファイルアップロード]リンク://タグ/マルチパート/[マルチパート]
spring mvc