apache commons net的maven地址:

http://mvnrepository.com/artifact/commons-net/commons-net/3.6

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

apache commons net的说明文档地址:http://commons.apache.org/proper/commons-net/

apache commons net的依赖关系:http://commons.apache.org/proper/commons-net/dependencies.html

看到apache commons net 3.6依赖于junit 4.12。

下面的测试程序用于下载指定位置的ftp上的文件到本地目录:

package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply; public class FTPDataGetter {
public static final String FTP_SRV_HOSTNAME = "192.168.100.35";
public static final Integer FTP_SRV_PORT = 21;
public static final String FTP_SRV_USERNAME = "zifeiy";
public static final String FTP_SRV_PASSWORD = "password";
public static final String LOCAL_DIRECTORY = "E:\\report"; private FTPClient ftpClient = null; public FTPDataGetter() {
this.ftpClient = new FTPClient();
ftpClient.setControlEncoding("utf-8");
try {
ftpClient.connect(FTP_SRV_HOSTNAME, FTP_SRV_PORT);
ftpClient.login(FTP_SRV_USERNAME, FTP_SRV_PASSWORD);
int replyCode = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(replyCode) == true) {
// 登陆成功
System.out.println("FTP connect to " + FTP_SRV_HOSTNAME + ":" + FTP_SRV_PORT + " succeed!");
}
else {
System.out.println("FTP connect to " + FTP_SRV_HOSTNAME + ":" + FTP_SRV_PORT + " failed!");
this.ftpClient = null;
}
} catch (IOException e) {
e.printStackTrace();
}
} public void downloadOneDayFiles(String dayString) {
if (this.ftpClient == null) {
System.out.println("[ERROR] because ftp client is NULL!");
return;
}
try {
ftpClient.changeWorkingDirectory("sdata/S-999000");
// 进入系统文件夹
for (FTPFile sysFile : this.ftpClient.listDirectories()) {
String sysName = sysFile.getName();
// System.out.println("sysName = " + sysName);
boolean changeSysFlag = ftpClient.changeWorkingDirectory(sysName);
if (changeSysFlag == true) {
// 进入类型文件夹 ADD或者ALL
for (FTPFile typeFile : this.ftpClient.listDirectories()) {
String typeName = typeFile.getName();
boolean changeTypeFlag = ftpClient.changeWorkingDirectory(typeName);
if (changeTypeFlag == true) {
// 进入当前目录,该目录下应该只有一个文件
boolean changeDayFlag = ftpClient.changeWorkingDirectory(dayString);
if (changeDayFlag == true) {
for (FTPFile needFile : ftpClient.listFiles()) {
System.out.println(needFile.getName());
downloadOneFile(dayString, needFile.getName());
}
ftpClient.changeToParentDirectory();
}
ftpClient.changeToParentDirectory();
}
}
ftpClient.changeToParentDirectory();
}
} } catch (IOException e) {
e.printStackTrace();
}
} private boolean downloadOneFile(String dayString, String filename) {
if (this.ftpClient == null) {
return false;
}
OutputStream os = null;
System.out.println("begin download file " + filename + " ...");
String directoryString = LOCAL_DIRECTORY + File.separator + dayString;
File directoryFile = new File(directoryString);
if (directoryFile.exists() == false) {
directoryFile.mkdirs();
}
File localFile = new File(directoryString + File.separator + filename);
try {
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(filename, os);
os.close();
System.out.println("download file " + filename + " finished!");
return true;
} catch (Exception e) {
System.out.println("download file " + filename + " failed!");
e.printStackTrace();
return false;
}
} public static void main(String[] args) {
FTPDataGetter ftpDataGetter = new FTPDataGetter();
for (int year = 2018; year<=2018; year ++) {
for (int month = 1; month <= 12; month ++) {
for (int day = 10; day <= 20; day ++) {
String dayString = String.format("%d%02d%02d", year, month, day);
ftpDataGetter.downloadOneDayFiles(dayString);
}
}
}
// ftpDataGetter.downloadOneDayFiles("20180502");
}
}

使用apache commons net进行ftp传输的更多相关文章

  1. Java 利用Apache Commons Net 实现 FTP文件上传下载

    package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  2. Java使用Apache Commons Net实现FTP功能

    maven依赖: <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...

  3. org.apache.commons.net.ftp

    org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object Java.lang.Object继承 org.apache. ...

  4. org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication

    Ftp问题 最近遇到了ftp读取中文乱码的问题,代码中使用的是FtpClient.google一下找到了解决方案. FTP协议里面,规定文件名编码为iso-8859-1,FTP类中默认的编码也是这个. ...

  5. 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载

    在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java  实体类(配置类) package com.sxd.ftp; public class FtpCo ...

  6. 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码

    和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...

  7. 利用Apache commons-net 包进行FTP文件和文件夹的上传与下载

    首先声明:这段代码是我在网上胡乱找的,调试后可用. 需要提前导入jar包,我导入的是:commons-net-3.0.1,在网上可以下载到.以下为源码,其中文件夹的下载存在问题:FTPFile[] a ...

  8. Java使用Apache Commons Net的FtpClient进行下载时会宕掉的一种优化方法

    在使用FtpClient进行下载测试的时候,会发现一个问题,就是我如果一直重复下载一批文件,那么经常会宕掉. 也就是说程序一直停在那里一动不动了. 每个人的情况都不一样,我的情况是因为我在本地之前就有 ...

  9. 编写更少量的代码:使用apache commons工具类库

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

随机推荐

  1. python3 基础一

    一.python基本运行 1.python特点:(1)python使用C语言开发,但是python不再有C语言中的指针等复杂数据类型,(2)python有很强的面向对象特性,而且简化了面向对象的实现, ...

  2. Hadoop的简单了解与安装

    hadoop 一, Hadoop  分布式 简介Hadoop  是分布式的系统架构,是  Apache  基金会顶级金牌项目 分布式是什么?学会用大数据的思想来看待和解决问题 思 想很重要 1-1 . ...

  3. error: RPC failed; curl 18 transfer closed with outstanding read data remaining

    报错: error: RPC failed; curl 18 transfer closed with outstanding read data remaining fatal: The remot ...

  4. JAVA遇见HTML——JSP篇(1、JAVA WEB简介)

    比如淘宝.新浪.搜狐.网易就是Web应用程序

  5. centos7.0利用yum快速安装mysql8.0

    我这里直接使用MySQL Yum存储库的方式快速安装: 抽象 MySQL Yum存储库提供用于在Linux平台上安装MySQL服务器,客户端和其他组件的RPM包.这些软件包还可以升级和替换从Linux ...

  6. asp.net用sql数据库生成json字符串并显示出来

    use Shop ,) )) insert into DictBase select '包装' UNION ALL select '价格' UNION ALL select '品牌' 工厂方法模式 I ...

  7. Codeforces Round #584 C. Paint the Digits

    链接: https://codeforces.com/contest/1209/problem/C 题意: You are given a sequence of n digits d1d2-dn. ...

  8. Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题

    C. Bad Sequence Problem Description: Petya's friends made him a birthday present — a bracket sequenc ...

  9. 008_Linux驱动之_IO口的配置

    1. 测试:配置S3C2440的GPF4,5,6为输出 2. 测试IO的地址从芯片手册中获取以下资料 3. 从上面可以看出配置输出对应的设置是01=输出,那么对应位如:[9:8]需要设置成=01 4. ...

  10. 编写测试类实现并发访问固定URL(亲测能用!!!)

    1.类目录 2.LatchTest.java类 package com.test; import java.util.concurrent.CountDownLatch; public class L ...