两台服务器ftp传文件,ftp 两台服务器传输文件 apache

public void ftpServer2Server() throwsSocketException, IOException {

FTPClient ftp1= newFTPClient();

FTPClient ftp2= newFTPClient();//连接ftp

ftp1.connect("xxx.xxx.xxx.xxx", 21); //可以不需要port

ftp1.login("user", "123456");//ftp2照样如此……

String initDir = "mydata";

ftp2.connect("xxx.com", 21); //可以不需要port

ftp2.login("user2", "pswd2", initDir);

ftp2.changeWorkingDirectory(initDir);//list files

listFiles(ftp1);//list dir

listFiles(ftp2);

OutputStream os= ftp2.storeFileStream("testFromFtp.mp3");//这句话就是获取ftp2的流

if (os == null) {

System.out.println(" italkdd.upload.akamai.com not open: " +ftp2.getReplyString());return;

}

ftp1.retrieveFile("test.mp3", os); //这句话是把文件从ftp1复制到ftp2中,通过流is

os.close();

System.out.println(" upload suc: " +ftp2.getReplyString());//Must call completePendingCommand() to finish command.

if (!ftp2.completePendingCommand()) {//完成数据传送,进入文件管理,这条语句特别关键,不然你不能进行文件管理操作(获取当前目录)特别重要

System.err.println("File transfer failed.");

}else{

System.err.println("File transfer suc.");

}/*InputStream input;

OutputStream output;

input = new FileInputStream("foobaz.txt");

output = ftp.storeFileStream("foobar.txt")

if(!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) {

input.close();

output.close();

ftp.logout();

ftp.disconnect();

System.err.println("File transfer failed.");

System.exit(1);

}

Util.copyStream(input, output);

input.close();

output.close();

// Must call completePendingCommand() to finish command.

if(!ftp.completePendingCommand()) {

ftp.logout();

ftp.disconnect();

System.err.println("File transfer failed.");

System.exit(1);

}*/listFiles(ftp2);

ftp1.logout();

ftp1.disconnect();

ftp2.logout();

ftp2.disconnect();

}public voidlistFiles(FTPClient ftp) {//list dir

try{

FTPFile[] listfiles=ftp.listFiles();for (int ii = 0; ii < listfiles.length; ii++) {

System.out.println(" == " +listfiles[ii].getName());

}

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值