springboot实现文件上传下载.zip


在Spring Boot框架中,文件上传和下载是常见的功能需求,特别是在构建Web应用程序时。本教程将详细介绍如何使用Spring Boot实现这两个功能。 我们需要引入相关的依赖。在`pom.xml`文件中添加Spring Web和MultipartFile处理的支持: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 如果使用其他模板引擎,如Freemarker或JSP,可以替换上面的thymeleaf依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!-- 可选,用于文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies> ``` 接着,创建一个简单的Controller来处理文件上传和下载的请求。例如,`Uploaddemo1Controller.java`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @Controller @RequestMapping("/uploaddemo1") public class Uploaddemo1Controller { private static final String UPLOAD_DIR = "uploads"; @Autowired public Uploaddemo1Controller() { File uploadDir = new File(UPLOAD_DIR); if (!uploadDir.exists()) { uploadDir.mkdir(); } } // 文件上传页面 @GetMapping public String uploadForm(Model model) { model.addAttribute("message", ""); return "uploadForm"; } // 处理文件上传请求 @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file, Model model) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOAD_DIR, file.getOriginalFilename()); Files.write(path, bytes); model.addAttribute("message", "文件上传成功!"); } catch (IOException e) { model.addAttribute("message", "文件上传失败:" + e.getMessage()); } } else { model.addAttribute("message", "请选择一个文件进行上传!"); } return "uploadForm"; } // 文件下载页面 @GetMapping("/download/{filename}") public void handleFileDownload(@PathVariable("filename") String filename, HttpServletResponse response) throws IOException { File file = new File(UPLOAD_DIR, filename); if (file.exists()) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8")); Files.copy(file.toPath(), response.getOutputStream()); } else { throw new FileNotFoundException("文件不存在!"); } } } ``` 在这个控制器中,我们定义了两个方法:`uploadForm`用于展示上传文件的表单页面,`handleFileUpload`用于接收并处理文件上传的请求。`handleFileDownload`则处理文件下载请求,设置响应头以指示浏览器以附件形式下载文件。 创建一个简单的Thymeleaf模板`uploadForm.html`,提供文件选择和提交按钮: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <form method="POST" enctype="multipart/form-data" th:action="@{/uploaddemo1/upload}"> <input type="file" name="file" required/> <button type="submit">上传文件</button> </form> <p th:text="${message}"></p> </body> </html> ``` 现在,你可以启动Spring Boot应用,访问`http://localhost:8080/uploaddemo1`,通过提供的表单上传文件。上传的文件会被保存到项目的`uploads`目录下。要下载文件,只需访问`http://localhost:8080/uploaddemo1/download/filename`,其中`filename`替换为实际的文件名。 这个简单的示例展示了如何在Spring Boot中实现文件上传和下载的基本功能。在实际项目中,你可能需要考虑更多的因素,比如文件大小限制、安全问题(如防止路径遍历攻击)、错误处理等。对于大规模应用,可能还需要集成云存储服务,如阿里云OSS、AWS S3等。


































































































- 1


- 粉丝: 5463
- 资源: 107
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- C#全自动多线程上位机源码编程:纯代码驱动的工控界面解决方案.pdf
- C#全自动多线程上位机源码:纯源代码与多功能工控屏通信系统.pdf
- C#全自动多线程上位机源码编程:纯源代码与功能强大的工控屏幕一体机解决方案.pdf
- C#三菱FX编程口协议RS422圆口测试工具及其相关资料.pdf
- C#三轴涂胶软件源码:多功能、稳定、高效的生产助手.pdf
- C#全自动工控屏上位机触摸源代码:纯代码实现,多页签功能强大,支持PLC通信与自由设定.pdf
- C#上位机:TCP、UDP、串口通信及数据采集源码.pdf
- C#上位机APP监控西门子S7-1200全套源代码:起重机高空维护解决方案.pdf
- C#上位机APP监控西门子PLC S7-1200起重机维护项目完整架构与源代码.pdf
- C#上位机OPC DA网口通讯协议:支持95% PLC连接并赠送编程课程及OPC服务器.pdf
- C#上位机Modbus Rtu协议源码:包括存储、数据库连接、趋势曲线图、数据报表及报警管理功能.pdf
- C#上位机APP监控西门子S7-1200全套源代码:起重机高空维护专用解决方案.pdf
- C#上位机案例:动态添加控件的Kuka机器人监控工具.pdf
- C#上位机开发:精致Winform窗体通讯西门子PLC,数据保存与实时监测的报警系统(三层架构编程).pdf
- C#上位机开发项目:Modbus RTU协议与SQL Server数据库联合应用.pdf
- 日本人工智能发展战略的演进及其经济、社会、军事领域的影响


