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. elk架构

    (1)Kafka:接收用户日志的消息队列.(2)Logstash:做日志解析,统一成JSON输出给Elasticsearch.(3)Elasticsearch:实时日志分析服务的核心技术,一个sche ...

  2. JDK源码那些事儿之常用的ArrayList

    前面已经讲解集合中的HashMap并且也对其中使用的红黑树结构做了对应的说明,这次就来看下简单一些的另一个集合类,也是日常经常使用到的ArrayList,整体来说,算是比较好理解的集合了,一起来看下 ...

  3. onbeforeunload、onpagehide、onunload、onload、onpageshow的正确执行顺序

    一.Chrome支持onbeforeunload.onpagehide.onunload,只是在这些方法执行的时候alert,console这些方法已经被注销了. 二.浏览器跳转.关闭.刷新时都按a, ...

  4. node 初级概念总结

    (1)nodejs跳过了服务器软件 直接充当web服务器,nodejs也没有Web容器 (2)nodejs --- 自身哲学---[花最小的硬件成本,追求更好的并发请求,更高的处理性能] (3)nod ...

  5. BZOJ 5496: [2019省队联测]字符串问题 (后缀数组+主席树优化建图+拓扑排序)

    题意 略 分析 考场上写了暴力建图40分溜了-(结果只得了30分) 然后只要优化建边就行了 首先给出的支配关系无法优化,就直接A向它支配的B连边. 考虑B向以B作为前缀的所有A连边,做一遍后缀数组,两 ...

  6. 获取select框下option所有值

    document.getElementById('roomId').options[0].value;获取第一个值 var roomIds = $("#roomId option" ...

  7. MySQL组提交(group commit)

    MySQL组提交(group commit) 前提: 以下讨论的前提 是设置MySQL的crash safe相关参数为双1: sync_binlog=1 innodb_flush_log_at_trx ...

  8. CF788A Functions again dp

    求一个最长子段和就完了,可以出T1? code: #include <bits/stdc++.h> #define N 100006 #define ll long long #defin ...

  9. D. AB-string ( 思维 )

    传送门 题意: 给你一个长度为n的字符串, 字符串只由 A B 组成. 问你这个字符串存在多少个 good string: ( 连续的一段子串 ) good string 的定义就是: 字符串中所有的 ...

  10. Gradle 如何打包 Spring Boot 可执行 JAR

    如何在 Gradle 中配置一个项目可以打包为 Spring Boot 可执行 Jar? 你首先需要添加到 org.springframework.boot 到插件中: 例如下面的代码: plugin ...