1. 引入依赖
<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.8.0</version></dependency>
2. 自定义 FTP 配置
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;(prefix = "ftp")public class FTPConfig {private String server;private int port;private String user;private String password;// Getters and Setterspublic 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;}}
ftp:server: ftp.example.comport: 21user: ftpuser:ftppassword
3. 实现 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;public class FTPService {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. 编写控制器类
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;public class FTPController {private FTPService ftpService;public ResponseEntity<String> uploadFile( 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 服务器
POSThttp://localhost:8080/uploadFile
- file: 上传的文件。
总结
通过本文,我们成功地在 Spring Boot 项目中实现了将文件推送到 FTP 服务器的功能。我们通过引入 Apache Commons Net 依赖、自定义 FTP 配置、创建 FTP 服务类和控制器类,实现了文件的上传和管理。这种方式可以帮助我们在各种应用场景中将文件高效地推送到 FTP 服务器,方便文件的存储和共享。
获取方式:点“在看”,关注公众号并回复Java领取,更多内容陆续奉上。
PS:因公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。
点“在看”支持码农呀,谢谢啦

本篇文章来源于微信公众号: 新生代码农
微信扫描下方的二维码阅读本文

Comments NOTHING