Springboot實(shí)現(xiàn)多文件上傳代碼解析
一說明
spingMVC支持文件上傳,我們通過Apach 的 commons-fileupload 包的CommonsMultipartResolver 去實(shí)現(xiàn)了
spingMVC的MultipartResolver 。
本文章的示例是個(gè)簡(jiǎn)單的多文件上傳,根據(jù)不同的業(yè)務(wù)自行修改。
二pom.xlm
<dependencies> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
三 application.yml
spring: servlet: multipart: max-file-size: 200MB #單個(gè)文件上傳大小 max-request-size: 600MB #連續(xù)上傳文件大小 youku1327: file: root: path: "C:\\mydata\\generator\\version06\\" #存儲(chǔ)路徑
四controller
/** * @Author lsc * @Description <p> </p> * @Date 2019/10/2 20:58 * @Version 1.0 */ @RestController public class FileUploadController { @Value("${youku1327.file.root.path}") private String fileRootPath; @PostMapping("/file/upload") public String fileUpload(@RequestParam("files")MultipartFile[] files){ String filePath = ""; // 多文件上傳 for (MultipartFile file : files){ // 上傳簡(jiǎn)單文件名 String originalFilename = file.getOriginalFilename(); // 存儲(chǔ)路徑 filePath = new StringBuilder(fileRootPath) .append(System.currentTimeMillis()) .append(originalFilename) .toString(); try { // 保存文件 file.transferTo(new File(filePath)); } catch (IOException e) { e.printStackTrace(); } } return filePath; } }
五啟動(dòng)類
/** * @Author lsc * @Description <p> </p> * @Date 2019/10/2 20:54 * @Version 1.0 */ @SpringBootApplication public class FileUploadApplication { public static void main(String[] args) { SpringApplication.run(FileUploadApplication.class,args); } }
六測(cè)試
發(fā)送http的post請(qǐng)求,使用表單形式,key為files需要與MultipartFile[] 的參數(shù)名稱一致,挑選兩個(gè)文件,發(fā)送成功后,會(huì)看到最后返回的文件路徑;
打開保存的文件路徑可以發(fā)現(xiàn)已經(jīng)實(shí)現(xiàn)文件上傳。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot大文件上傳、分片上傳、斷點(diǎn)續(xù)傳、秒傳的實(shí)現(xiàn)
本文主要介紹了springboot大文件上傳、分片上傳、斷點(diǎn)續(xù)傳、秒傳的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Spring Cloud Feign實(shí)例講解學(xué)習(xí)
這篇文章主要介紹了Spring Cloud Feign實(shí)例講解學(xué)習(xí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Java線程編程中isAlive()和join()的使用詳解
這篇文章主要介紹了Java線程編程中isAlive()和join()的使用詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09mybatis if test判斷BigDecimal遇到的坑及解決
這篇文章主要介紹了mybatis if test判斷BigDecimal遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java 8新時(shí)間日期庫(kù)java.time的使用示例
這篇文章主要給你大家介紹了關(guān)于Java 8新時(shí)間日期庫(kù)java.time的使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析
這篇文章主要介紹了springboot 定時(shí)任務(wù)@Scheduled實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09SpringBoot使用SOFA-Lookout監(jiān)控的方法
本文介紹SpringBoot使用螞蟻金服SOFA-Lookout配合Prometheus進(jìn)行監(jiān)控,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03