龙空技术网

Spring Boot文件上传示例(五十一)

IT技术院 491

前言:

此时各位老铁们对“apache文件上传pom”大致比较重视,你们都需要剖析一些“apache文件上传pom”的相关资讯。那么小编同时在网摘上网罗了一些关于“apache文件上传pom””的相关内容,希望看官们能喜欢,各位老铁们快快来了解一下吧!

本文介绍如何在Spring Boot Web应用程序中上传文件。

使用的工具 :

Spring Boot 1.4.3.RELEASESpring 4.3.5.RELEASEThymeleafMaven 3Embedded Tomcat 8.5.6

1. 项目结构

标准项目结构如下图所示 -

2. 项目依赖

Spring boot依赖关系,无需额外的文件上传库。

<project xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <modelVersion>4.0.0</modelVersion> <groupId>com.felix</groupId> <artifactId>spring-boot-file-upload</artifactId> <packaging>jar</packaging> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- hot swapping, disable cache for template, enable live reload --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

3.文件上传示例

Spring Boot文件上传,不需要什么特别的配置。在Controller中,将上传的文件映射到MultipartFile。

文件:UploadController.java -

package com.felix.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;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;@Controllerpublic class UploadController { //Save the uploaded file to this folder private static String UPLOADED_FOLDER = "D://temp//"; @GetMapping("/") public String index() { return "upload"; } @PostMapping("/upload") // //new annotation since 4.3 public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (file.isEmpty()) { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); return "redirect:uploadStatus"; } try { // Get the file and save it somewhere byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); Files.write(path, bytes); redirectAttributes.addFlashAttribute("message", "You successfully uploaded '" + file.getOriginalFilename() + "'"); } catch (IOException e) { e.printStackTrace(); } return "redirect:/uploadStatus"; } @GetMapping("/uploadStatus") public String uploadStatus() { return "uploadStatus"; }}

在thymeleaf,只是一些普通的HTML文件标签。文件:upload.html -

<!DOCTYPE html><html xmlns:th=""><body><h1>Spring Boot文件上传示例</h1><form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /><br/><br/> <input type="submit" value="提交" /></form></body></html>

另外一个页面,用为显示文件上传的状态。文件:uploadStatus.html -

<!DOCTYPE html><html lang="en" xmlns:th=""><body><h1>Spring Boot文件上传状态</h1><div th:if="${message}"> <h2 th:text="${message}"/></div></body></html>

4. 超过最大上传大小

要处理最大上传大小超出异常,请声明一个@ControllerAdvice并捕获MultipartException。

文件:GlobalExceptionHandler.java

package com.felix.controller;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.multipart.MultipartException;import org.springframework.web.servlet.mvc.support.RedirectAttributes;@ControllerAdvicepublic class GlobalExceptionHandler { // //4.3.5 supports RedirectAttributes redirectAttributes @ExceptionHandler(MultipartException.class) public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) { redirectAttributes.addFlashAttribute("message", e.getCause().getMessage()); return "redirect:/uploadStatus"; }}

5. Tomcat连接重置

如果部署到Tomcat,请配置maxSwallowSize以避免此Tomcat连接重置问题。 对于嵌入式Tomcat,声明一个TomcatEmbeddedServletContainerFactory,如下所示, SpringBootWebApplication.java -

package com.felix;import org.apache.coyote.http11.AbstractHttp11Protocol;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;// class SpringBootWebApplication { private int maxUploadSizeInMb = 10 * 1024 * 1024; // 10 MB public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } //Tomcat large file upload connection reset @Bean public TomcatEmbeddedServletContainerFactory tomcatEmbedded() { TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> { if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) { //-1 means unlimited ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1); } }); return tomcat; }}

6. Multipart文件大小

默认情况下,Spring Boot max文件上传大小为1MB,可以通过以下应用程序属性来配置它的值,application.properties -

# multipartspring.http.multipart.max-file-size=10MBspring.http.multipart.max-request-size=10MB

7. 运行示例

使用默认的嵌入式Tomcat启动Spring Boot的命令如下: mvn spring-boot:run ,运行结果如下 -

23:40:03,238 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5a39699c - Registering current configuration as safe fallback point . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/::Spring Boot:: (v1.4.3.RELEASE)2017-03-30 23:40:04 INFO com.felix.SpringBootWebApplication - Starting SpringBootWebApplication on MY-PC with PID 880 (F:\worksp\springboot\file-upload\target\classes started by Administrator in F:\worksp\springboot\file-upload)2017-03-30 23:40:04 DEBUG com.felix.SpringBootWebApplication - Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE2017-03-30 23:40:04 INFO com.felix.SpringBootWebApplication - No active profile set, falling back to default profiles: default2017-03-30 23:40:08 INFO com.felix.SpringBootWebApplication - Started SpringBootWebApplication in 5.359 seconds (JVM running for 6.355)Java

打开浏览器,访问: 输出结果如下 -

选择一个文件并将其上传,选择大于10mb的文件,将会看到页面提示如下 -

标签: #apache文件上传pom