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. MySQL dump文件导入

    1 打开cmd 输入要导入的数据库,用户名,密码,dump文件路径 mysql -u employees <E:\employees_db\load_departments.dump

  2. [转帖]linux下的X server:linux图形界面原理

    linux下的X server:linux图形界面原理 https://www.cnblogs.com/liangxiaofeng/p/5034912.html linux下的X server:lin ...

  3. spring 标签

    */ @Slf4j @Service public class RetryService { @Autowired private MqConfig mqConfig; /** * 如果网络连接失败, ...

  4. vscode運行vue和html

    html 选中html文件,右键选择view in broswer.

  5. Learning to Rank for IR的评价指标—MAP,NDCG,MRR

    转自: https://www.cnblogs.com/eyeszjwang/articles/2368087.html MAP(Mean Average Precision):单个主题的平均准确率是 ...

  6. Java反射和注解

    反射:http://blog.csdn.net/liujiahan629629/article/details/18013523 注解:http://www.cnblogs.com/peida/arc ...

  7. Cherry.chen window.clipboardData实现剪切板操作总结 (好像只有ie好用)

    window.clipboardData的作用是在页面上将需要的东西复制到剪贴板上,提供了对于预定义的剪贴板格式的访问,以便在编辑操作中使用. 三个方法 (1)clearData(sDataForma ...

  8. maven + bat 实现快速编译打包模块代码

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  9. mpvue——Error: EPERM: operation not permitted

    报错 $ npm run build > mpvue@ build D:\wamp\www\webpack\mpvue\my-project > node build/build.js w ...

  10. python中raise的用法

    有关于python里raise显示引发异常的方法: 当程序出错时,python会自动触发异常,也可以通过raise显示引发异常 一旦执行了raise语句,raise之后的语句不在执行 如果加入了try ...