import org..ftp.FTPClient;
import org..ftp.FTPReply;
import java.io.IOException;
public class FTPConnectionManager {
private FTPClient ftpClient;
private String host;
private int port;
private String username;
private String password;
public FTPConnectionManager(String host, int port, String username, String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.ftpClient = new FTPClient;
public boolean connect throws IOException {
// 设置连接超时时间
ftpClient.setConnectTimeout(10000);
// 设置数据传输超时时间
ftpClient.setDataTimeout(30000);
ftpClient.connect(host, port);
int replyCode = ftpClient.getReplyCode;
if (!FTPReply.isPositiveCompletion(replyCode)) {
disconnect;
throw new IOException(“FTP服务器拒绝连接,回复代码: ” + replyCode);
boolean loginSuccess = ftpClient.login(username, password);
if (!loginSuccess) {
disconnect;
throw new IOException(“FTP登录失败,用户名或密码错误”);
// 设置二进制传输模式
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 使用被动模式,避免防火墙问题
ftpClient.enterLocalPassiveMode;
return true;
public void disconnect {
try {
if (ftpClient != null && ftpClient.isConnected) {
ftpClient.logout;
ftpClient.disconnect;
} catch (IOException e) {
System.err.println(“断开FTP连接时发生错误: ” + e.getMessage);
public FTPClient getFtpClient {
return ftpClient;
#!/bin/bash
ftp -n << END_FTP
open 192.168.1.22
user test testing
binary
prompt off
mput files
close
bye
END_FTP
内容均以整理官方公开资料,价格可能随活动调整,请以购买页面显示为准,如涉侵权,请联系客服处理。
本文由星速云发布。发布者:星速云。禁止采集与转载行为,违者必究。出处:https://www.67wa.com/5733.html