使用apache commons net进行ftp传输
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传输的更多相关文章
- Java 利用Apache Commons Net 实现 FTP文件上传下载
package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- Java使用Apache Commons Net实现FTP功能
maven依赖: <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> ...
- org.apache.commons.net.ftp
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object Java.lang.Object继承 org.apache. ...
- org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication
Ftp问题 最近遇到了ftp读取中文乱码的问题,代码中使用的是FtpClient.google一下找到了解决方案. FTP协议里面,规定文件名编码为iso-8859-1,FTP类中默认的编码也是这个. ...
- 【FTP】使用org.apache.commons.net.ftp.FTPClient 实现FTP的上传下载
在此之前,在项目中加上FTP的架包 第一步:配置FTP服务器的相关配置 FtpConfig.java 实体类(配置类) package com.sxd.ftp; public class FtpCo ...
- 【FTP】org.apache.commons.net.ftp.FTPClient实现复杂的上传下载,操作目录,处理编码
和上一份简单 上传下载一样 来,任何的方法不懂的,http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net ...
- 利用Apache commons-net 包进行FTP文件和文件夹的上传与下载
首先声明:这段代码是我在网上胡乱找的,调试后可用. 需要提前导入jar包,我导入的是:commons-net-3.0.1,在网上可以下载到.以下为源码,其中文件夹的下载存在问题:FTPFile[] a ...
- Java使用Apache Commons Net的FtpClient进行下载时会宕掉的一种优化方法
在使用FtpClient进行下载测试的时候,会发现一个问题,就是我如果一直重复下载一批文件,那么经常会宕掉. 也就是说程序一直停在那里一动不动了. 每个人的情况都不一样,我的情况是因为我在本地之前就有 ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
随机推荐
- YOLO---多个版本的简单认识
YOLO---多个版本的简单认识 YOLOv3 有好几个经典版本了:一.YOLOv3 (Darknet)官网 @ https://github.com/pjreddie/darknet二.YOLOv3 ...
- Thinkphp 两级联动
<!-- 前端代码部分--><div class="pop-movegroup" id="salesmanBox"> <dl> ...
- 开启树莓派自带的VNC功能
前期准备 树莓派可以连接路由器或连上wifi,我用的是自己的小米路由器,目的是为了获取内网IP 工具准备 下载 vnc viewer https://www.realvnc.com/en/connec ...
- bloomberg bulkfile 在oracle的存储
文章导航 bloomberg bulkfile 解析 bloomberg bulkfile 在oracle的存储 一 表名和字段名称的命名规则 1.1. 表名以文件名称直接命名,将文件名中的" ...
- 前端知识体系:JavaScript基础-作用域和闭包-闭包的实现原理和作用以及堆栈溢出和内存泄漏原理和相应解决办法
闭包的实现原理和作用 闭包: 有权访问另一个函数作用域中的变量的函数. 创建闭包的常见方式就是,在一个函数中创建另一个函数. 闭包的作用: 访问函数内部变量.保持函数在环境中一直存在,不会被垃圾回收机 ...
- tomcat——context.xml
本机tomcat位置:D:\tomcat7\apache-tomcat-7.0.61 context.xml 位置:D:\tomcat7\apache-tomcat-7.0.61\conf 每个Web ...
- 015_使用 expect 工具自动交互密码远程其他主机安装 httpd 软件
#!/bin/bash#删除~/.ssh/known_hosts 后,ssh 远程任何主机都会询问是否确认要连接该主机rm -rf ~/.ssh/known_hostsexpect <<E ...
- [Luogu] 区间统计Tallest Cow
https://www.luogu.org/problemnew/show/P2879 差分 | 线段树 #include <iostream> #include <cstdio&g ...
- [Luogu] 树状数组
https://www.luogu.org/problemnew/show/P3374 单点修改,区间查询 #include <iostream> #include <cstdio& ...
- Codevs 1743 反转卡片(splay)
1743 反转卡片 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description [dzy493941464|yywyzdzr原创] 小A将N ...