您好,欢迎您访问我们燿动吧 – 知识分享,快乐你我,燿动青春!

燿动吧 – 知识分享,快乐你我,燿动青春

                                          您现在的位置是:燿动吧 > 项目中心 >

                                          SpringBoot进行文件上传和多文件上传

                                          文章来源:醉巧 时间:2025-03-11

                                          怎样正在springboot工程行为效劳器,来接纳经由过程http 上传的multi-file的文献。

                                          创办1个springmvc工程您须要spring-boot-starter-thymeleaf战 spring-boot-starter-web的起步依靠。为例或许上传文献正在效劳器,您须要正在web.xml中参加标签干相干的摆设,但正在sringboot 工程中,它一经为您主动干了,因此没有须要您干一切的摆设。

                                          崔素强博客使用

                                          由于名目中应用thymeleaf去衬托页里,引进thymeleaf

                                          <!--______________________________________________thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

                                          建设文献须要界说的设置

                                          #上传文献年夜小操纵spring.http.multipart.max-file-size=128KBspring.http.multipart.max-request-size=128KB#thymeleafspring.thymeleaf.cache=falsespring.thymeleaf.check-template=truespring.thymeleaf.check-template-location=truespring.thymeleaf.content-type=text/htmlspring.thymeleaf.enabled=truespring.thymeleaf.encoding=UTF-8spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.html

                                          写1个操纵类,有入进尾页,单文献上传战多文献上传

                                          packagecom.example.demo.controller;importjava.io.File;importjava.util.List;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.multipart.MultipartFile;importorg.springframework.web.multipart.MultipartHttpServletRequest;importorg.springframework.web.servlet.mvc.support.RedirectAttributes;importcom.example.demo.util.UUID;/***文献上传尝试类*/@ControllerpublicclassFileUploadController{@GetMapping("/uploadIndex")publicStringfileUploadIndex(){return"upload";}@PostMapping("/uploadFile")publicStringhandleFileUpload(@RequestParam("file")MultipartFilefile,Modelmodel,RedirectAttributesredirectAttributes){StringfileName=file.getOriginalFilename();StringfilePath="D:\\temp\\";Filedest=newFile(filePath+UUID.getUUID()+fileName.substring(fileName.lastIndexOf(".")));try{//能够依照日期死成文献,依照自界说划定规矩死成文献实file.transferTo(dest);redirectAttributes.addFlashAttribute("message","文献:"+file.getOriginalFilename()+"上传乐成");}catch(Exceptione){e.printStackTrace();redirectAttributes.addFlashAttribute("message","文献:"+file.getOriginalFilename()+"上传腐臭");}return"redirect:/uploadIndex";}@PostMapping("/multiUpload")publicStringmultiUpload(HttpServletRequestrequest,Modelmodel,RedirectAttributesredirectAttributes){List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles("file");StringfilePath="D:\\temp\\";StringBuffersb=newStringBuffer("");for(inti=0;i<files.size();i++){MultipartFilefile=files.get(i);StringfileName=file.getOriginalFilename();Filedest=newFile(filePath+UUID.getUUID()+fileName.substring(fileName.lastIndexOf(".")));try{//能够依照日期死成文献,依照自界说划定规矩死成文献实file.transferTo(dest);sb.append("文献:"+file.getOriginalFilename()+"上传乐成。");}catch(Exceptione){e.printStackTrace();sb.append("文献:"+file.getOriginalFilename()+"上传腐烂。");}}redirectAttributes.addFlashAttribute("message",sb.toString());return"redirect:/uploadIndex";}}

                                          页里upload.html代码以下

                                          <htmlxmlns:th="http://www.thymeleaf.org"><body><divth:if="${message}">体系提醒:<h2th:text="${message}"/></div><div><formmethod="POST"enctype="multipart/form-data"action="/uploadFile"><table><tr><td>拔取文献:</td><td><inputtype="file"name="file"/></td></tr><tr><td></td><td><inputtype="submit"value="上传"/></td></tr></table></form><formmethod="POST"enctype="multipart/form-data"action="/multiUpload"><table><tr><td>采用文献1:</td><td><inputtype="file"name="file"/></td></tr><tr><td>采用文献2:</td><td><inputtype="file"name="file"/></td></tr><tr><td>挑选文献3:</td><td><inputtype="file"name="file"/></td></tr><tr><td></td><td><inputtype="submit"value="多文献上传"/></td></tr></table></form></div></body></html>

                                          恶果成绩以下

                                          QQ截图20210410103049.jpg

                                          END

                                          推举您浏览更多相关于“ 文献上传springbootthymeleafmultipart ”的著作