日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Springboot 多文件上传

發(fā)布時(shí)間:2023/11/28 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot 多文件上传 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

其實(shí)多個(gè)文件和單個(gè)文件上傳是一樣的,可以使用同一個(gè)Controller

添加依賴


<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version>
</dependency><!-- 熱啟動 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional>
</dependency>

編寫頁面

ok頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>文件上傳</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}" media="all"><link rel="stylesheet" th:href="@{/css/bootstrap-theme.css}" media="all"><script type="text/javascript" th:src="@{/js/jquery-1.12.4.js}" charset="utf-8"></script><script type="text/javascript" th:src="@{/js/bootstrap.js}" charset="utf-8"></script></head>
<body>
<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">文件上傳</h3></div>
</div>
<div class="container"><div class=" panel panel-primary"><div class="panel-heading"><h3 class="panel-title">文件上傳結(jié)果</h3></div><div class="panel-body">上傳成功</div></div>
</div>
</body>
</html>

文件上傳頁面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>文件上傳</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}" media="all"><link rel="stylesheet" th:href="@{/css/bootstrap-theme.css}" media="all"><script type="text/javascript" th:src="@{/js/jquery-1.12.4.min.js}" charset="utf-8"></script><script type="text/javascript" th:src="@{/js/bootstrap.js}" charset="utf-8"></script>
</head>
<body>
<div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title">多文件上傳</h3></div>
</div>
<div class="container"><div class="row"><div class="col-md-8"><form class="form-horizontal" action="/file/uploads" enctype="multipart/form-data" method="post"><div class="form-group"><div class="input-group col-md-4"><span class="input-group-addon"><iclass="glyphicon glyphicon-upload"></i></span><input class="form-control" type="file" name="files" multiple="multiple" placeholder="文件"/></div></div><div class="form-group"><div class="col-md-4"><div class="btn-group btn-group-justified"><div class="btn-group"><button type="submit" class="btn btn-success" id="submitBtn"><span class="glyphicon glyphicon-share"></span>&nbsp;上傳</button></div></div></div></div></form></div></div>
</div>
</body>
</html>

主要就是文件的上傳,使用了表單上傳,Post方式請求,使用了bootstrap的多文件上傳組件

編寫Controller

PageController

@Controller
public class PageController {@RequestMapping("/{redirect}")public String redirect(@PathVariable String redirect) {if(StringUtils.isBlank(redirect)) {return "index";}return redirect;}
}

FileUploadController

@Controller
@RequestMapping("/file")
public class FileUploadController {/** 文件保存路徑 */@Value("${fileStorePath}")private String fileStorePath;/*** 上傳文件* @param files 文件對象* @return 視圖ok* @throws IOException 操作異常*/@RequestMapping("/uploads")public String uploadMoreFiles(@RequestParam("files") MultipartFile ... files) throws IOException {String fileName;for (MultipartFile file : files) {fileName = file.getOriginalFilename();assert fileName != null;File targetFile = new File(fileStorePath, fileName);if (!targetFile.exists()) {if (targetFile.mkdirs()) {System.out.println("創(chuàng)建文件成功!");}}file.transferTo(targetFile);}return "ok";}
}

添加配置

yml文件

spring:thymeleaf:prefix: classpath:/templates/suffix: .htmlmode: HTML5encoding: UTF-8##關(guān)閉緩存cache: falsecheck-template-location: trueservlet:content-type: text/htmlservlet:multipart:max-file-size: 200MBmax-request-size: 250MBserver:port: 80

properties文件

#熱啟動
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java#上傳文件路徑
fileStorePath=d:/file

個(gè)人習(xí)慣性的將需要修改的屬性放在properties配置文件中,小工程就沒有分文件了

測試界面


選擇文件

上傳結(jié)果

總結(jié)

以上是生活随笔為你收集整理的Springboot 多文件上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。