import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import java.text.SimpleDateFormat;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


 public void ftpServer2Server() throws SocketException, IOException {

        FTPClient ftp1 = new FTPClient();
FTPClient ftp2 = new FTPClient();
//连接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 void listFiles(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();
}
}

用mvn,pom.xml中加入对apache commons的依赖:

<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.5</version>
</dependency>

ftp 两台服务器传输文件 apache的更多相关文章

  1. Centos7 rsync+inotify两台服务器同步文件(单向)

    注:本篇介绍的是单向同步,即A文件同步到B,但B的文件不同步到A,双向同步的在下一篇文章中. rsync与inotify不再赘述,直接进入实战. 0.背景 两台服务器IP地址分别为: 源服务器:192 ...

  2. linux两台服务器之间文件/文件夹拷贝

    跨服务器拷贝需要用到的命令是scp. ----------------------拷贝文件夹---------------------------------------------- 把当前文件夹t ...

  3. Linux 两台服务器之间传输文件和文件夹

    今天处理一个项目要迁移的问题,突然发现这么多图片怎么移过去,可能第一时间想到的是先从这台服务器下载下来,然后再上传到另外一台服务器上面去,这个方法确实是可行,但是实在是太费时间了,今天我就教大家怎么快 ...

  4. Centos 6.5 rsync+inotify 两台服务器文件实时同步

    rsync和inotify是什么我这里就不在介绍了,有专门的文章介绍这两个工具. 1.两台服务器IP地址分别为: 源服务器:192.168.1.2 目标服务器:192.168.1.3 @todo:从源 ...

  5. linux下两台服务器文件实时同步方案设计和实现

    inux下两台服务器文件实时同步方案设计和实现 假设有如下需求: 假设两个服务器: 192.168.0.1 源服务器  有目录 /opt/test/ 192.168.0.2 目标服务器  有目录 /o ...

  6. SqlServer数据库同时备份到两台服务器上(并自动删除过期文件)

    数据库同时备份到两台服务器上(并自动删除过期文件) 举例 :(本地)服务器A: IP :192.168.1.1 (远程)服务器B: IP :192.168.1.2 数据库版本:SqlServer200 ...

  7. 【Linux】两台服务器ssh免密登录

    背景: 有些场景可能用到两台服务器ssh免密登录.比如服务器自动化部署 开始准备:  服务器A  linux   ip: 192.168.1.1 服务器B  linux  ip: 192.168.1. ...

  8. windows2003两台服务器,局域网之间不能互相访问

    准备在两台服务器之间,映射网络驱动器,但怎么也连不上了. 可以在网络邻居中看到对方的机器,但就是访问不到共享的文件,也无法做网络映射. 搜索了一下,发现在是因为防火墙中,没有把"文件和打印机 ...

  9. Nginx之负载均衡 :两台服务器均衡(填坑)

    第一步,两台服务器都要安装好Nginx和Tomcat,我这边的安装的是Nginx 1.16.1 Tomcat9: 第二步,安装完成之后,选择你要做均衡的那台服务器,,打开其Nginx 配置文件,在se ...

随机推荐

  1. 【转帖】Linux的历史----Linux内核剖析(一)

    Linux的历史----Linux内核剖析(一) 2015年04月09日 10:51:09 JeanCheng 阅读数:11351更多 所属专栏: Linux内核剖析    版权声明:本文为博主原创文 ...

  2. Spring boot+ logback环境下,日志存放路径未定义的问题

    日志路径未定义 环境:Spring boot + logback 配置文件: <configuration> <springProfile name="dev"& ...

  3. 关于我使用spring mvc框架做文件上传时遇到的问题

    非常感谢作者 原文:https://blog.csdn.net/lingirl/article/details/1714806 昨天尝试着用spring mvc框架做文件上传,犯了挺多不该犯的毛病问题 ...

  4. Object.defineProperties()与Proxy对象代理

    Object.defineProperties() 了不起啊..vue.js通过它实现双向绑定的 Object.defineProperties(obj,props) 方法直接在一个对象上定义新的属性 ...

  5. 在eclipse中spring的xml配置文件标签中class路径全限定名自动提示设置

    这个自动提示其实很简单,没有网上说的那些要在help下的Install中输入网址来下载更新一堆东西那么复杂. 只需要打开Help — — >Eclipse Marketplace... 然后在该 ...

  6. ERP系统的问题

    1.业务统计报表导出超时 2.库存统计相关接口查询容易导致慢查询,而且慢查询出现在主库上

  7. DELPHI中MDI子窗口的关闭和打开

    DELPHI中MDI子窗口的关闭 和打开       Delphi中MDI子窗口的关闭方式默认为缩小而不是关闭,所以当你单击子窗口右上角的关闭按钮时会   发觉该子窗口只是最小化,而不是你预期的那样被 ...

  8. vscode实现vue.js项目的过程

    https://blog.csdn.net/weixin_37567150/article/details/81291433 https://blog.csdn.net/ywl570717586/ar ...

  9. 创建iview框架的项目

    http://www.cnblogs.com/jf-67/p/8479176.html 在使用‘vue init webpack my-project’创建项目时,出现了错误 npm ERR! cod ...

  10. linux-shell系列4-init

    #!/bin/bash # 过滤出MAC地址MAC=`ifconfig |awk '{print $5}'|sed -n '1p;1q'` # 过滤网卡名字NET_NAME=`ifconfig |aw ...