在云原生应用开发中,AWS EFS作为弹性文件存储服务,常被用来实现多实例共享文件存储的需求。结合Java和Spring Boot开发API接口,能够方便地对EFS中的文件进行上传和下载操作,满足业务系统的文件管理要求。

前期准备
在开始编码前,需要完成以下基础配置:
- 拥有可用的AWS账号,并创建好EFS文件系统,记录文件系统的ID和挂载点信息
- 准备一台Linux系统的服务器或者本地开发环境,完成EFS的挂载操作,假设挂载路径为
/mnt/efs - 创建Spring Boot项目,确保JDK版本在8及以上
项目依赖配置
在Spring Boot项目的pom.xml中添加必要的依赖,主要包含Web模块用于开发API接口:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
核心配置类
为了方便管理EFS的挂载路径,我们可以在配置文件中定义相关参数,然后通过配置类读取:
首先在application.yml中添加配置:
efs: mount-path: /mnt/efs
然后创建配置类读取该参数:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "efs")
public class EfsConfig {
private String mountPath;
public String getMountPath() {
return mountPath;
}
public void setMountPath(String mountPath) {
this.mountPath = mountPath;
}
}
文件上传API实现
文件上传接口接收前端传递的文件,将其保存到EFS的指定目录下,这里我们将文件保存到/mnt/efs/uploads路径:
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.UUID;
@RestController
@RequestMapping("/api/efs")
public class FileUploadController {
@Autowired
private EfsConfig efsConfig;
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传文件不能为空";
}
// 构建EFS中的存储目录
String uploadDir = efsConfig.getMountPath() + File.separator + "uploads";
File dir = new File(uploadDir);
if (!dir.exists()) {
dir.mkdirs();
}
// 生成唯一文件名,避免重名覆盖
String originalFilename = file.getOriginalFilename();
String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + fileSuffix;
Path filePath = Paths.get(uploadDir, newFileName);
try {
// 将文件写入EFS路径
Files.write(filePath, file.getBytes());
return "文件上传成功,文件名:" + newFileName;
} catch (IOException e) {
e.printStackTrace();
return "文件上传失败:" + e.getMessage();
}
}
}
文件下载API实现
文件下载接口根据前端传递的文件名,从EFS的对应目录读取文件并返回给前端:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
@RequestMapping("/api/efs")
public class FileDownloadController {
@Autowired
private EfsConfig efsConfig;
@GetMapping("/download")
public ResponseEntity<Resource> downloadFile(@RequestParam("fileName") String fileName) {
// 构建EFS中的文件路径
String uploadDir = efsConfig.getMountPath() + File.separator + "uploads";
Path filePath = Paths.get(uploadDir, fileName);
File file = filePath.toFile();
if (!file.exists()) {
return ResponseEntity.notFound().build();
}
try {
Resource resource = new FileSystemResource(file);
// 设置响应头,指定文件名和下载类型
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.internalServerError().build();
}
}
}
接口测试验证
完成代码编写后,启动Spring Boot项目,我们可以通过Postman等工具测试接口:
- 上传测试:选择POST请求,地址为
http://127.0.0.1:8080/api/efs/upload,在Body中选择form-data,key为file,类型为File,选择本地文件发送请求,即可看到上传成功的返回信息 - 下载测试:选择GET请求,地址为
http://127.0.0.1:8080/api/efs/download?fileName=上传返回的文件名,发送请求后即可下载对应的文件
注意事项
- 确保运行Spring Boot应用的服务器有EFS挂载路径的读写权限,避免出现权限不足导致的文件操作失败
- 生产环境中需要对上传文件的类型、大小做限制,避免恶意文件上传和存储资源耗尽
- 如果EFS挂载在多台服务器上,多实例场景下上传下载操作都能正常访问到同一份文件,满足共享存储的需求
JavaSpring_BootAWS_EFS文件上传文件下载修改时间:2026-06-12 09:12:37