SpringBoot 文件上传和下载的实现源码文件上传和下载的实现源码
主要介绍了SpringBoot 文件上传和下载的实现源码,代码简单易懂非常不错,具有参考借鉴价值,需要的朋友可
以参考下
本篇文章介绍SpringBoot的上传和下载功能。
一、创建一、创建SpringBoot工程,添加依赖工程,添加依赖
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
工程目录为:
Application.java 启动类
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
二、创建测试页面二、创建测试页面
在resources/templates目录下新建uploadForm.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:if="${message}">
<h2 th:text="${message}"/>
</div>
<div>
<form method="POST" enctype="multipart/form-data" action="/">
<table>
<tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
</div>
<div>
<ul>
<li th:each="file : ${files}">
<a th:href="${file}" rel="external nofollow" th:text="${file}" />
</li>
</ul>