springboot多文件上傳實(shí)現(xiàn)使用postman測(cè)試多文件上傳接口
使用postman測(cè)試多文件上傳接口
1、創(chuàng)建測(cè)試類(FileController.java)
package com.jeff.controller; import java.io.File; import java.io.IOException; import java.util.List; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class FileController { @PostMapping("/upload") public String upload(@RequestParam("files") List<MultipartFile> files) { if (files.isEmpty()) { return "上傳失敗,未選擇文件"; } for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); // 獲取文件后綴名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 重新生成文件名 String fName = System.currentTimeMillis() + suffixName; System.out.println("文件名:" + fName); String filePath = "F:\\Jeff\\project\\workspace\\mavenDemo\\src\\main\\resources\\static\\"; File dest = new File(filePath + fName); try { file.transferTo(dest); System.out.println(fName + "上傳成功!"); } catch (IOException e) { System.out.println(fName + "上傳異常!" + e); return "error"; } } return "success"; } }
2、使用postman測(cè)試多文件上傳接口(選擇多個(gè)文件)
3、查看項(xiàng)目路徑
4、如果報(bào)下圖錯(cuò)誤,請(qǐng)查看 解決方法
解決方法:The field files exceeds its maximum permitted size of 1048576 bytes
錯(cuò)誤原因:
SpringBoot的默認(rèn)上傳文件的大小是1M,如果上傳的文件超過了1M就會(huì)出現(xiàn)這樣的錯(cuò)誤
解決方法:
在application.properties配置文件中設(shè)置上傳的文件大小限制,即可解決
# 上傳文件總的最大值 spring.servlet.multipart.max-request-size=10MB # 單個(gè)文件的最大值 spring.servlet.multipart.max-file-size=10MB
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus 處理大數(shù)據(jù)插入太慢的解決
這篇文章主要介紹了mybatis-plus 處理大數(shù)據(jù)插入太慢的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12JAVA多線程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解
這篇文章主要介紹了JAVA多線程實(shí)現(xiàn)生產(chǎn)者消費(fèi)者的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06詳解json在SpringBoot中的格式轉(zhuǎn)換
這篇文章主要介紹了詳解json在SpringBoot中的格式轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11springMVC實(shí)現(xiàn)圖形驗(yàn)證碼(kaptcha)代碼實(shí)例
這篇文章主要介紹了springMVC實(shí)現(xiàn)圖形驗(yàn)證碼(kaptcha)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2019-09-09SpringBoot接入輕量級(jí)分布式日志框架(GrayLog)的操作方法
這篇文章主要介紹了SpringBoot接入輕量級(jí)分布式日志框架(GrayLog)的方法,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03