Spring Boot:将文件推送到 FTP 服务器
创始人
2025-01-19 01:31:55
0

在企业应用中,将文件推送到 FTP 服务器是一个常见的需求。本文将介绍如何在 Spring Boot 项目中实现将文件推送到 FTP 服务器,包括引入依赖、自定义配置和编写代码示例。

1. 引入依赖

首先,在 Spring Boot 项目的 pom.xml 文件中引入 Apache Commons Net 依赖,它是一个用于处理 FTP 操作的库:

     commons-net     commons-net     3.8.0  

2. 自定义 FTP 配置

创建一个配置类 FTPConfig,用于配置 FTP 连接的相关参数:

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration;  @Configuration @ConfigurationProperties(prefix = "ftp") public class FTPConfig {      private String server;     private int port;     private String user;     private String password;      // Getters and Setters     public String getServer() {         return server;     }      public void setServer(String server) {         this.server = server;     }      public int getPort() {         return port;     }      public void setPort(int port) {         this.port = port;     }      public String getUser() {         return user;     }      public void setUser(String user) {         this.user = user;     }      public String getPassword() {         return password;     }      public void setPassword(String password) {         this.password = password;     } } 

在 application.yml 文件中添加 FTP 服务器的相关配置:

ftp:   server: ftp.example.com   port: 21   user: ftpuser   password: ftppassword 

3. 实现 FTP 服务类

创建一个服务类 FTPService,用于连接 FTP 服务器并上传文件:

import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;  import java.io.IOException; import java.io.InputStream;  @Service public class FTPService {      @Autowired     private FTPConfig ftpConfig;      public void uploadFile(String remotePath, MultipartFile file) throws IOException {         FTPClient ftpClient = new FTPClient();         try {             ftpClient.connect(ftpConfig.getServer(), ftpConfig.getPort());             ftpClient.login(ftpConfig.getUser(), ftpConfig.getPassword());             ftpClient.enterLocalPassiveMode();             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);              try (InputStream inputStream = file.getInputStream()) {                 boolean done = ftpClient.storeFile(remotePath, inputStream);                 if (done) {                     System.out.println("File " + file.getOriginalFilename() + " has been uploaded successfully.");                 } else {                     throw new IOException("Failed to upload file " + file.getOriginalFilename());                 }             }         } finally {             if (ftpClient.isConnected()) {                 ftpClient.logout();                 ftpClient.disconnect();             }         }     } } 

4. 编写控制器类

创建一个控制器类 FTPController,用于处理文件上传请求并调用 FTPService 进行文件上传:

import org.springframework.beans.factory.annotation.Autowired; 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.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;  import java.io.IOException;  @RestController public class FTPController {      @Autowired     private FTPService ftpService;      @PostMapping("/uploadFile")     public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) {         try {             String remotePath = "/uploads/" + file.getOriginalFilename();             ftpService.uploadFile(remotePath, file);             return new ResponseEntity<>("File uploaded successfully", HttpStatus.OK);         } catch (IOException e) {             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);         }     } } 

完整代码结构

src ├── main │   ├── java │   │   └── com │   │       └── example │   │           └── ftp │   │               ├── FTPApplication.java │   │               ├── FTPConfig.java │   │               ├── FTPService.java │   │               └── FTPController.java │   └── resources │       └── application.yml 

测试上传文件到 FTP 服务器

启动 Spring Boot 应用程序,使用 Postman 或类似工具发送 POST 请求至以下 URL,并上传要推送到 FTP 服务器的文件:

POST http://localhost:8080/uploadFile 

请求参数:

file: 上传的文件。 

总结

通过本文,我们成功地在 Spring Boot 项目中实现了将文件推送到 FTP 服务器的功能。我们通过引入 Apache Commons Net 依赖、自定义 FTP 配置、创建 FTP 服务类和控制器类,实现了文件的上传和管理。这种方式可以帮助我们在各种应用场景中将文件高效地推送到 FTP 服务器,方便文件的存储和共享。

相关内容

热门资讯

终于清楚!微信小程序黑科技(外... 终于清楚!微信小程序黑科技(外挂),微乐自建房道具有用吗果然有挂方针1、辅助器安装包、辅助器2、分享...
现有关情况通报如下!人人燕赵辅... 现有关情况通报如下!人人燕赵辅助下载,丽水都莱脚本(其实真的有下载)-哔哩哔哩1、用户打开应用后不用...
目前来看"中至赣州冲... 目前来看"中至赣州冲关辅助"原来真的是有辅助脚本(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更...
有玩家发现!hhpoker有后... 您好,腾讯游戏大厅辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
一分钟了解!!微乐小程序免费黑... 一分钟了解!!微乐小程序免费黑科技(外挂),微乐小程序脚本一贯新版有挂1、完成辅助器v3.3的残局,...
于此同时"多乐辅助器... 于此同时"多乐辅助器使用教程"其实存在有辅助神器(哔哩哔哩);1、下载好多乐辅助器使用教程脚本下载之...
此事引发网友热议!边锋斗地主辅... 此事引发网友热议!边锋斗地主辅助微信,湖南牵手胡子跑辅助器(确实有挂安装)-哔哩哔哩1、湖南牵手胡子...
更值得关注的是!哈糖大菠萝破解... 更值得关注的是!哈糖大菠萝破解器,四川途游小程序辅助,都是是有辅助神器(哔哩哔哩)1、不需要AI权限...
一分钟秒懂!微乐小程序免费黑科... 一分钟秒懂!微乐小程序免费黑科技(外挂),微乐家乡官方app下载其实有挂头条1、打开软件启动之后找到...
黑科技技巧"财神十三... 黑科技技巧"财神十三张脚本怎么弄的教程"总是真的有辅助器(哔哩哔哩)1、财神十三张脚本怎么弄的教程免...