idea创建项目springbootdemo-download-upload
- 加上thymeleaf模板maven依赖
- application.properties配置
| |
| spring.thymeleaf.cache=false |
| |
| spring.servlet.multipart.max-file-size=10MB |
| |
| spring.servlet.multipart.max-request-size=50MB |
文件上传前端表单upload.html
| <!DOCTYPE html> |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title>动态添加文件上传列表</title> |
| <link href="css/bootstrap.min.css" rel="stylesheet"> |
| <script src="js/jquery-3.4.1.js"></script> |
| <script> |
| function add(){ |
| var innerdiv = "<div>"; |
| innerdiv += "<input type='file' name='fileUpload' required='required'>" + |
| "<input type='button' value='删除' onclick='remove(this)'>"; |
| innerdiv +="</div>"; |
| $("#file").append(innerdiv); |
| $("#submit").css("display","block"); |
| } |
| function remove(obj) { |
| $(obj).parent().remove(); |
| if($("#file div").length ==0){ |
| $("#submit").css("display","none"); |
| } |
| } |
| |
| </script> |
| </head> |
| <div th:if="${uploadStatus}" style="color: red" th:text="${uploadStatus}"> |
| 上传成功</div> |
| <form th:action="@{/uploadFile}" method="post" enctype="multipart/form-data"> |
| 上传文件: |
| <input type="button" value="添加文件" onclick="add()"/> |
| <div id="file" style="margin-top: 10px;" th:value="文件上传区域"> </div> |
| <input id="submit" type="submit" value="上传" |
| style="display: none;margin-top: 10px;"/> |
| </form> |
| </body> |
| </html> |
文件上传后端springboot的controller
| @RequestMapping("/toUpload") |
| public String toUpload(){ |
| return "upload"; |
| } |
| |
| |
| |
| |
| @PostMapping("/uploadFile") |
| public String uploadFile(MultipartFile[] fileUpload, Model model) { |
| |
| model.addAttribute("uploadStatus", "上传成功!"); |
| for (MultipartFile file : fileUpload) { |
| |
| String fileName = file.getOriginalFilename(); |
| |
| fileName = UUID.randomUUID()+"_"+fileName; |
| |
| |
| |
| String dirPath = "C:/Users/Desktop/"; |
| File filePath = new File(dirPath); |
| if(!filePath.exists()){ |
| filePath.mkdirs(); |
| } |
| try { |
| file.transferTo(new File(dirPath+fileName)); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| |
| model.addAttribute("uploadStatus","上传失败: "+e.getMessage()); |
| } |
| } |
| |
| return "upload"; |
| } |
文件下载
| |
| <dependency> |
| <groupId>commons-io</groupId> |
| <artifactId>commons-io</artifactId> |
| <version>2.11.0</version> |
| </dependency> |
前端下载
| <!DOCTYPE html> |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>文件下载</title> |
| </head> |
| <body> |
| <div style="margin-bottom: 10px">文件下载列表:</div> |
| <table> |
| <tr> |
| <td>9010094_s.jpg</td> |
| <td><a th:href="@{/download(filename='9010094_s.jpg')}">下载文件</a></td> |
| </tr> |
| <tr> |
| <td>第5章 SpringBoot实现Web开发.ppt</td> |
| <td><a th:href="@{/download(filename='第5章 SpringBoot实现Web开发.ppt')}"> |
| 下载文件</a></td> |
| </tr> |
| </table> |
| </body> |
| </html> |
| |
springboot后端controller路由
| @RequestMapping("/todownload") |
| public String todownload(){ |
| return "download"; |
| } |
| |
| |
| |
| @GetMapping("/ddownload") |
| public ResponseEntity<byte[]> fileDownload(String filename){ |
| |
| String dirPath = "C:\\Users\\Desktop/"; |
| |
| File file = new File(dirPath + File.separator + filename); |
| |
| HttpHeaders headers = new HttpHeaders(); |
| |
| headers.setContentDispositionFormData("attachment",filename); |
| |
| headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| try { |
| return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return new ResponseEntity<byte[]>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED); |
| } |
| } |
| |
| |
| |
| @GetMapping("/download") |
| public ResponseEntity<byte[]> fileDownload(HttpServletRequest request, |
| String filename) throws Exception{ |
| |
| String dirPath = "C:/Users/Desktop/download/"; |
| |
| File file = new File(dirPath + File.separator + filename); |
| |
| HttpHeaders headers = new HttpHeaders(); |
| |
| filename=getFilename(request,filename); |
| headers.setContentDispositionFormData("attachment",filename); |
| |
| headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
| try { |
| return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return new ResponseEntity<byte[]>(e.getMessage().getBytes(),HttpStatus.EXPECTATION_FAILED); |
| } |
| } |
| |
| private String getFilename(HttpServletRequest request, String filename) |
| throws Exception { |
| |
| String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"}; |
| |
| String userAgent = request.getHeader("User-Agent"); |
| for (String keyWord : IEBrowserKeyWords) { |
| if (userAgent.contains(keyWord)) { |
| |
| return URLEncoder.encode(filename, "UTF-8").replace("+"," "); |
| } |
| } |
| |
| return new String(filename.getBytes("UTF-8"), "ISO-8859-1"); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)