Ftpsclient上传文件到ftp时storeFile总是返回false(522 data connections must be encrypted)

使用java中org.apache.commons.net.ftp的FTPSClient将文件上传服务器时,调用storeFile总是返回false。

及时打印fClient.getReplyCode()和fClient.getReplyString(),返回522 data connections must be encrypted。

后来增加如下代码,解决问题

 

this.fClient.enterLocalPassiveMode();
this.fClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
this.fClient.execPROT("P");

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以使用Apache Commons Net库来上传文件FTP服务器。以下是一个简单的示例: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency> ``` 2. 编写上传代码 在Spring Boot应用程序中,您可以使用以下代码将文件上传到FTP服务器: ``` import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; @Service public class FtpService { private static final String FTP_HOST = "ftp.example.com"; private static final String FTP_USERNAME = "username"; private static final String FTP_PASSWORD = "password"; public void uploadFile(MultipartFile file) throws IOException { FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(FTP_HOST); ftpClient.login(FTP_USERNAME, FTP_PASSWORD); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); String fileName = file.getOriginalFilename(); InputStream inputStream = file.getInputStream(); ftpClient.storeFile(fileName, inputStream); } finally { ftpClient.logout(); ftpClient.disconnect(); } } } ``` 在上面的代码中,我们使用FTPClient类连接到FTP服务器,然后使用storeFile()方法将文件上传到服务器。 3. 测试上传 您可以使用以下代码测试上传: ``` import org.springframework.beans.factory.annotation.Autowired; 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; @RestController public class FileUploadController { @Autowired private FtpService ftpService; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { ftpService.uploadFile(file); return "File uploaded successfully"; } } ``` 在上面的代码中,我们使用Spring MVC的@RequestParam注释将上传的文件作为参数传递给uploadFile()方法。 希望这可以帮助您将文件上传到FTP服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值