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. 2 Servlet 细节

    1 Servlet 配置详解 ①  由于客户端在浏览器只能通过URL访问web服务器的资源,所以Servlet程序若想被外界访问,必须把Servlet 程序映射到一个URL 地址上,这个工作在项目we ...

  2. ActiveMQ入门案例-生产者代码实现

    <–start–> 使用Java程序操作ActiveMQ生产消息,代码的复杂度较高,但也没有默写下来的必要. 开发ActiveMQ首先需要导入activemq-all.jar包,如果是ma ...

  3. 索引使用,分析初探。(explain分析执行计划,以及强制使用force index)

    促使这次探索的初衷还是因为要对一个定时脚本性能进行优化. 脚本有两个指定状态分别是status, latest_process_status,和一个超期时间expire_time进行限制. 按照我以前 ...

  4. Android SDK Tools 24.3.2 Build脚本Bug

    如下图版本: 在%Android_home%\tools\ant\build.xml中, 在483行附近, 少了aidl,aapt,dx, zipalign四个变量的声明. 加上就OK了. <p ...

  5. qtp10 安装笔记

    windows10系统安装QTP 10 1 QTP10 程序文件夹下,找到“setup”双击它运行安装程序-点击 否 继续安装 2 安装必要组件 3 下一步 选择安装程序目录-安装插件 直到完成安装 ...

  6. C#使用WebClient下载文件到本地目录

    C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...

  7. How to sign app

    codesign --display --verbose=4 /applications/qq.app codesign --display --entitlements - /application ...

  8. 搭建Hexo博客(四)-设置

    1.主题 比较流行的是yilia,下载主题到theme目录即可.再根据说明文档进行设置. 2.图片发布 source下创建的目录,并且目录下有文件,则文件会被生成到post目录中.图片可以放在这里.既 ...

  9. Nginx 缓存深入理解

    100课陶辉 proxy_cache_methods 指令主要是根据请求方法指定是否使用缓存 Syntax: proxy_cache_methods GET | HEAD | POST ...; De ...

  10. Django restframe 视图函数以及ModelSerializer的使用

    建立model数据库 from django.db import models __all__ = ['Book', 'Publisher', 'Author'] # Create your mode ...