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. Nginx的简单了解与使用

    一.产生背景 我们日常生活中经常遇到的问题 这是几年前的12306,现在的12306基本上不会出现这样的问题了 上述场景产生的主要 2 大原因:1.巨大流量2.单台服务器资源和能力有限在海量并发的环境 ...

  2. js获取文件后缀名

    function extname(filename){ if(!filename||typeof filename!='string'){ return false }; let a = filena ...

  3. C语言calloc()函数:分配内存空间并初始化——stm32中的应用

    经常在代码中看到使用malloc来分配,然后memset清零,其实calloc更加方便,一句顶两句~ 头文件:#include <stdlib.h> calloc() 函数用来动态地分配内 ...

  4. WeakReference 弱引用

    弱引用是使用WeakReference类创建的.因为对象可能在任意时刻被回收,所以在引用该对象前必须确认它存在. class MainEntryPoint { static void Main() { ...

  5. java开发时,eclipse设置编码

    修改eclipse默认工作空间编码方式,General——Workspace——Text file encoding 修改工程编码方式,右击工程——Properties——Resource——Text ...

  6. AtCoder Beginner Contest 116 D - Various Sushi 【贪心+栈】

    Problem Statement There are NN pieces of sushi. Each piece has two parameters: "kind of topping ...

  7. mouseenter([[data],fn])

    mouseenter([[data],fn]) 概述 当鼠标指针穿过元素时,会发生 mouseenter 事件.该事件大多数时候会与mouseleave 事件一起使用.广州大理石机械构件 与 mous ...

  8. Ubuntu: error: snap “phpstorm” has “install-snap” change in progress

    Ubuntu: error: snap “phpstorm” has “install-snap” change in progress 投稿日 : 2019-06-10 | カテゴリー : linu ...

  9. v-for为什么要加key,能用index作为key么

    前言 在vue中使用v-for时,一直有几个疑问: v-for为什么要加key 为什么有时候用index作为key会出错 带着这个疑问,结合各种博客和源码,终于有了点眉目. virtual dom 要 ...

  10. CSP-S模拟测试69 题解

    一如既往的垃圾,又回到了那个场场垫底的自己,明明考场上都想到正解了,但是就是拿不到分,可能是互奶把rp用光了吧以后一定加强训练代码能力. T1: 考场上一直yy矩阵快速幂,虽然自己矩阵快速幂一点都不会 ...