You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Java与Spring Boot API实现文件上传及AWS EFS文件传输?

用Java和Spring Boot实现文件上传API

完全可以通过Spring Boot快速实现文件上传API,核心是利用Spring的MultipartFile类处理上传请求:

  1. 基础配置
    在application.properties里设置上传文件的大小限制:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
  1. 编写文件上传Controller
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "上传文件不能为空";
        }
        // 定义文件存储路径(可根据需求调整)
        String filePath = "/path/to/storage/directory/" + file.getOriginalFilename();
        File dest = new File(filePath);
        // 确保目录存在
        dest.getParentFile().mkdirs();
        // 保存文件
        file.transferTo(dest);
        return "文件上传成功:" + filePath;
    }
}

集成AWS EFS实现文件上传与下载

Spring Boot操作AWS EFS本质是操作本地挂载的文件系统——EFS需要先挂载到你的应用服务器(比如EC2实例、EKS节点),之后Spring Boot就像读写本地文件一样操作EFS,无需特殊SDK(挂载方式比直接用SDK更直观)。

步骤1:挂载AWS EFS到服务器

  • 在AWS控制台创建EFS文件系统,配置安全组允许应用服务器通过NFS协议(端口2049)访问
  • 登录应用服务器,执行挂载命令(以EC2为例):
    sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport <EFS文件系统DNS>:/ /mnt/efs
    
    可将挂载命令添加到/etc/fstab实现开机自动挂载。

步骤2:Spring Boot操作EFS

只需将文件存储路径指向EFS的挂载目录(比如/mnt/efs),复用上传代码并调整路径即可:

// 修改文件存储路径为EFS挂载目录
String filePath = "/mnt/efs/uploads/" + file.getOriginalFilename();

下载示例

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@RestController
public class FileDownloadController {

    @GetMapping("/download/{fileName}")
    public ResponseEntity<FileSystemResource> downloadFile(@PathVariable String fileName) {
        File file = new File("/mnt/efs/uploads/" + fileName);
        if (!file.exists()) {
            return ResponseEntity.notFound().build();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
        return ResponseEntity.ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new FileSystemResource(file));
    }
}

注意事项

  • 确保Spring Boot进程对EFS挂载目录有读写权限(可通过chown命令调整目录权限)
  • 容器化部署时,需将EFS挂载目录映射到容器内部路径
  • 高并发场景下,可结合异步处理或分布式文件锁避免文件冲突

内容的提问来源于stack exchange,提问作者sagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:40:28