Spring Boot实现Excel文件上传接收:超详细Controller示例
在Spring Boot中,我们可以使用`MultipartFile`来处理文件上传,包括Excel文件。以下是一个简单的Controller示例,它接收一个Excel文件并将其保存到服务器: 首先,你需要在pom.xml文件中添加Apache POI依赖,用于解析Excel文件: ```xml <dependencies> ... <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ... </dependencies> ``` 然后创建一个Controller类,例如`FileUploadController`: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; 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.StandardCopyOption; public class FileUploadController { @Value("${file.upload.path}") private String uploadPath; @PostMapping("/upload-excel") public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) { if (file == null || file.isEmpty()) { return ResponseEntity.badRequest().body("File is empty"); } try { // 检查文件类型是否为Excel if (!file.getOriginalFilename().toLowerCase().endsWith(".xls") && !file.getOriginalFilename().toLowerCase().endsWith(".xlsx")) { return ResponseEntity.badRequest().body("Invalid file type, only Excel files (.xls, .xlsx) are allowed"); } // 创建目标文件路径 Path targetFile = Paths.get(uploadPath, file.getOriginalFilename()); // 将文件从内存复制到服务器 Files.copy(file.getInputStream(), targetFile, StandardCopyOption.REPLACE_EXISTING); return ResponseEntity.ok("File uploaded successfully: " + targetFile); } catch (IOException e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred while uploading file: " + e.getMessage()); } } } ``` 在这个例子中,我们首先检查上传的文件是否为空或类型是否为Excel。然后,我们将文件从请求体复制到服务器上的指定目录(可以通过`@Value`注解配置)。 注意:这个例子没有包含Excel内容的解析,如果你需要解析Excel内容,你可能需要使用Apache POI或者其他的库,这将涉及到更复杂的代码和逻辑。 ######[AI写代码神器 | 525点数解答 | 2024-04-01 12:12:38]
- Python 利用 win32com 库:将 Excel 转换为 A4 横向 PDF 并单独打印首列(字节豆包 | 507点数解答 | 2025-03-10 15:48:12)325
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)404
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)383
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)361
- IntelliJ IDEA 搭建 Spring Boot 项目:从创建到 JPA 实体类及数据源配置全攻略(讯飞星火 | 920点数解答 | 2024-10-31 15:37:22)275
- Maven POM文件开头元素详解及Spring Boot项目配置规范(DeepSeek | 1912点数解答 | 2026-04-23 16:43:34)9
- Java 代码分析:从结构优势到潜在问题与注意要点(字节豆包 | 698点数解答 | 2025-09-15 10:13:12)150
- Spring Boot 与 Vue 联手:轻松实现微信扫码登录全攻略(字节豆包 | 207点数解答 | 2025-05-13 09:12:19)179
- Spring框架实战:从配置到实现,搞定员工信息查询功能(GPT | 2135点数解答 | 2024-06-12 08:32:35)324
- Java实现链表反转:迭代与递归双解法详解及开发实战指南(DeepSeek | 1409点数解答 | 2026-03-15 15:09:29)50
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)366
- 缜密逻辑,全新无名杀“风餐”技能代码精心打造与深度解析 (字节豆包 | 469点数解答 | 2025-04-05 11:37:06)261