使用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 ...
随机推荐
- python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色)
python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色) 前言 在 python 开发中,有时候想通过cmd窗口来和用户交互,比如显示信息之类的,会比自己创建 GUI 来的方便,但是随之而 ...
- vue2 父子组件数据更改
父级以对象的形式传递数据给子级,子级更改此数据时,父级同步更改
- 25-SQLServer中的DMV和DMF的使用
一.总结 1.什么事DMV和DMFDMV(Dynamic Management View):动态管理视图DMF(Dynamic Management Function):动态管理函数 二.操作步骤 1 ...
- stm32中阻塞模式和非阻塞模式 in blocking mode 与 in non-blocking mode区别
阻塞模式和非阻塞模式...... 我的理解是:阻塞模式就像是一个延时函数,当这个函数没处理完那么,所有的按照流程需要执行的代码都不会被执行,要等到这个延时完成,类似 平时看书上写的LED灯闪烁,用的d ...
- python3爬虫系列19之反爬随机 User-Agent 和 ip代理池的使用
站长资讯平台:python3爬虫系列19之随机User-Agent 和ip代理池的使用我们前面几篇讲了爬虫增速多进程,进程池的用法之类的,爬虫速度加快呢,也会带来一些坏事. 1. 前言比如随着我们爬虫 ...
- Linux查看公网IP
curl cip.cc 查看公网IP curl -s icanhazip.com 查看公网IP, 只显示IP,没有供应商信息
- P4310 绝世好题 按位DP
这名字可海星\(OvO\) 思路:\(DP\) 提交:2次(\(zz\)我竟然把三目运算符写错了\(QwQ\)) 题解: 按位进行\(DP\):\(f[i]\)表示结尾的数字包括\(1<< ...
- ueditor+复制word图片粘贴上传
Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...
- [Luogu] 1600
https://www.luogu.org/problemnew/show/P1600 nlogn竟然T了 #include <iostream> #include <cstdio& ...
- ftp、sftp、vsftp、vsftpd、lftp以及一些网络客户端工具命令
ftp 是File Transfer Protocol的缩写,文件传输协议,用于在网络上进行文件传输的一套标准协议,使用客户/服务器模式.它属于网络传输协议的应用层.了解更多ftp lftp :是一个 ...