您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot如何實現多文件上傳”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot如何實現多文件上傳”文章能幫助大家解決問題。
package com.yqifei.upload.controller; import io.swagger.annotations.Api; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * @ClassName UploadController * @Description TODO * @Author jiangyuntao * @Data 2023/3/7 23:52 * @Version 1.0 * @Email yuntaojiang@foxmail.com */ @RestController @CrossOrigin @RequestMapping("/posts") @Api(tags = "文件上傳控制器") public class UploadController { /* http://localhost:8088/swagger-ui.html# */ @PostMapping(value="/upload") @CrossOrigin public List<String> fileload(@RequestParam(value = "file") MultipartFile[] file, HttpServletRequest request) throws IOException { System.out.println(file.length); String savaLaction="d:/data/"; String fileSaveName; List<String> imageUri = new ArrayList<>(); for (MultipartFile multipartFile:file) { System.out.println("文件"+multipartFile.getOriginalFilename()); fileSaveName = UUID.randomUUID().toString()+multipartFile.getOriginalFilename(); multipartFile.transferTo(new File(savaLaction,fileSaveName)); String res = request.getScheme()+"://"+request.getServerName()+":"+"8080"+savaLaction+"/"+fileSaveName; imageUri.add(res); } System.out.println(imageUri); return imageUri; } }
package com.yqifei.upload.utils; import org.springframework.context.annotation.Configuration; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebFilter(filterName = "CorsFilter") @Configuration public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin","*"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); chain.doFilter(req, res); } }
# 應用名稱 spring.application.name=upload # 應用服務 WEB 訪問端口 server.port=8088 spring.web.resources.static-locations=file:d:/data/ spring.servlet.multipart.max-request-size=50MB spring.servlet.multipart.max-file-size=50MB
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Multiple File Upload</title> </head> <body> <h2>Multiple File Upload</h2> <form> <input type="file" id="fileInput" multiple /> <button type="button" onclick="uploadFiles()">Upload</button> </form> <div id="progress"></div> <div>圖片返回值地址:</div> <div id="result"></div> </body> <script> function uploadFiles() { const files = document.getElementById("fileInput").files; const xhr = new XMLHttpRequest(); const formData = new FormData(); for (let i = 0; i < files.length; i++) { formData.append("file", files[i]); } xhr.open("POST", "http://localhost:8088/posts/upload"); xhr.upload.addEventListener("progress", function (event) { if (event.lengthComputable) { const percent = Math.round((event.loaded / event.total) * 100); const progress = document.getElementById("progress"); progress.innerHTML = "Upload progress: " + percent + "%"; } }); xhr.addEventListener("load", function (event) { const response = JSON.parse(event.target.responseText); console.log(response); // 在HTML頁面上找到需要顯示響應結果的元素 const resultElement = document.getElementById("result"); // 更新元素的文本內容為服務器返回的值 resultElement.textContent = response; }); xhr.send(formData); } </script> </html>
修改tomcat的server.xml文件
加上下面這句
<Context docBase ="/home/springbootVue/files" path ="/home/springbootVue/files" debug ="0" reloadable ="true"/> // docBase代表文件路徑,path是瀏覽器訪問時的路徑。 // 若自己創建的文件夾在tomcat目錄的webapps中,不同之處: docBase直接寫文件夾文字即可(注意:沒有/) 例如 docBase ="home/springbootVue/files"
關于“SpringBoot如何實現多文件上傳”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。