package com.ytd.zjdlbb.service.zjdlbb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

/**
*******************************************************************************
* 文件名称:FTPService.java 系统名称: 类 概 述: FTP文件上传下载 创建日期:2015年4月3日 下午5:17:52 版 本:
* V1.0.0
*******************************************************************************
*/
public class FTPService {
private Logger log = Logger.getLogger(getClass());
protected static String FTP_CONTROL_ENCODING = "ISO-8859-1";
protected static String FTP_CHARSET_NAME;
protected static String FTP_SYSTEM_TYPE;
protected static String LOCAL_FILE_ENCODING = System.getProperty("file.encoding");
protected static String LOCAL_SYSTEM_TYPE = System.getProperty("os.name");
public static String FTP_CONTROL_ENCODING_LOCAL = "GBK";

/**
* @Description 概要说明:上传文件到FTP服务器
*
* @date 2015年4月6日 下午4:12:51
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param port
* 端口
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* FTP保存文件的目录
* @param localFile
* 本地需要上传的文件
* @return boolean
*/
public boolean upload(String ip, int port, String username, String pwd, String remoteDir, String localFile) {
File in = new File(localFile);
return upload(ip, port, username, pwd, remoteDir, in);
}

/**
* @Description 概要说明:上传文件到FTP服务器
*
* @date 2015年4月6日 下午4:12:51
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* FTP保存文件的目录
* @param localFile
* 本地需要上传的文件
* @return boolean
*/
public boolean upload(String ip, String username, String pwd, String remoteDir, String localFile) {
File in = new File(localFile);
return upload(ip, username, pwd, remoteDir, in);
}

/**
* @Description 概要说明:上传文件到FTP服务器
*
* @date 2015年4月6日 下午4:12:51
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param port
* 端口
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* FTP保存文件的目录
* @param in
* 本地需要上传的文件
* @return boolean
*/
public boolean upload(String ip, int port, String username, String pwd, String remoteDir, File in) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
// 建立连接、登录
success = login(ftp, ip, username, pwd);
if (!success) {
return success;
}
uploadFile(ftp, remoteDir, in);
// 退出登录
ftp.logout();
success = true;
} catch (IOException e) {
log.error("", e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

/**
* @Description 概要说明:上传文件到FTP服务器
*
* @date 2015年4月6日 下午4:12:51
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* FTP保存文件的目录
* @param in
* 本地需要上传的文件
* @return boolean
*/
public boolean upload(String ip, String username, String pwd, String remoteDir, File in) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
// 建立连接、登录
success = login(ftp, ip, username, pwd);
if (!success) {
return success;
}
uploadFile(ftp, remoteDir, in);
// 退出登录
ftp.logout();
success = true;
} catch (IOException e) {
log.error("", e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

/**
* @Description 概要说明:本地文件上传到FTP指定目录
*
* @date 2015年4月6日 下午4:16:10
* @update xxx(yyyy-MM-dd)
* @param ftp
* FTP对象
* @param remoteDir
* FTP目录
* @param in
* 本地文件
* @return void
* @throws IOException
*/
public void uploadFile(FTPClient ftp, String remoteDir, File in) throws IOException {
String fileName = in.getName();
try {
if (!in.exists()) {
throw new IOException("文件[" + fileName + "]不存在!");
} else {
if (in.isDirectory()) {
remoteDir = remoteDir + "/" + fileName;
ftp.makeDirectory(local2Remote_encoding(remoteDir));
File[] files = in.listFiles();
for (File tmp : files) {
uploadFile(ftp, remoteDir, tmp);
}
} else {
uploadFile(ftp, remoteDir, fileName, in);
}
}
log.info("[" + fileName + "/" + in.length() / 1024 + "KB]上传成功!");
} catch (IOException e) {
log.info("[" + fileName + "]上传失败!");
throw e;
}
}

/**
* @Description 概要说明:文件上传
*
* @date 2015年4月6日 下午4:54:21
* @update xxx(yyyy-MM-dd)
* @param ftp
* @param remoteDir
* @param fileName
* @param in
* @throws IOException
* void
*/
public void uploadFile(FTPClient ftp, String remoteDir, String fileName, File in) throws IOException {
remoteDir = local2Remote_encoding(remoteDir + "/");
ftp.changeWorkingDirectory(remoteDir);
fileName = local2Remote_encoding(fileName);
InputStream input = null;
try {
log.debug("localFile:" + in.getAbsolutePath());
input = new FileInputStream(in);
boolean isSucc = ftp.storeFile(fileName, input);
if (!isSucc) {
throw new IOException("文件[" + fileName + "/" + (in.getTotalSpace() / 1024) + "Kb]上传失败!");
}
} catch (IOException e) {
throw e;
} finally {
input.close();
}
}

/**
* @Description 概要说明:从FTP上下载文件
*
* @date 2015年4月3日 下午10:53:43
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* 需要下载的目标目录
* @param localDir
* 本地保存目录
* @return boolean
*/
public List<File> download(String ip, String username, String pwd, String remoteDir, String localDir) {
boolean success = false;
FTPClient ftp = new FTPClient();
List<File> list = null;
try {
// 建立连接、登录
success = login(ftp, ip, username, pwd);
if (!success) {
return list;
}
list = downloadFile(ftp, remoteDir, localDir);
// 退出登录
ftp.logout();
} catch (IOException e) {
log.error("", e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return list;
}

/**
* @Description 概要说明:从FTP服务器上下载文件
*
* @date 2015年4月3日 下午10:46:05
* @update xxx(yyyy-MM-dd)
* @param ip
* FTP服务器IP地址
* @param port
* 端口号
* @param username
* 用户名
* @param pwd
* 密码
* @param remoteDir
* 需要下载的目标目录
* @param localDir
* 本地保存目录
* @return boolean
*/
public List<File> download(String ip, int port, String username, String pwd, String remoteDir, String localDir) {
boolean success = false;
FTPClient ftp = new FTPClient();
List<File> list = null;
try {
success = login(ftp, ip, port, username, pwd);
if (!success) {
return list;
}
list = downloadFile(ftp, remoteDir, localDir);
ftp.logout();
} catch (IOException e) {
log.error("", e);
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return list;
}

/**
* @Description 概要说明:过滤本目录以及父级目录
*
* @date 2015年4月4日 上午12:00:15
* @update xxx(yyyy-MM-dd)
* @param fs
* @return List<FTPFile>
*/
protected List<FTPFile> filterSelf(FTPClient ftp, FTPFile[] fs) throws UnsupportedEncodingException {
List<FTPFile> list = new ArrayList<FTPFile>();
for (FTPFile tmp : fs) {
String tmpName = tmp.getName();
Pattern p1 = Pattern.compile("^\\.$");
Matcher m1 = p1.matcher(tmpName);
Pattern p2 = Pattern.compile("^\\.\\.$");
Matcher m2 = p2.matcher(tmpName);
boolean self1 = m1.find();
boolean self2 = m2.find();
if (!self1 && !self2) {
list.add(tmp);
}
}
return list;
}

/**
* @Description 概要说明:文件过滤逻辑,重写自定义逻辑
*
* @date 2015年4月3日 下午10:38:03
* @update xxx(yyyy-MM-dd)
* @param fs
* @return List<FTPFile>
*/
protected List<FTPFile> filter(FTPClient ftp, List<FTPFile> fs, Object... params) {
List<FTPFile> files = new ArrayList<FTPFile>();
List<FTPFile> dirs = new ArrayList<FTPFile>();
for (FTPFile tmp : fs) {
if (tmp.isDirectory()) {
dirs.add(tmp);
} else {
files.add(tmp);
}
}
files.addAll(dirs);
return files;
}

/**
* @Description 概要说明:下载文件到本地
*
* @date 2015年4月3日 下午5:22:18
* @update xxx(yyyy-MM-dd)
* @param ftp
* ftp对象
* @param files
* 远程主机文件集合
* @param localDir
* 需要下载到本地目录
* @throws IOException
* void
*/
public List<File> downloadFile(FTPClient ftp, String remoteDir, String localDir) throws IOException {
String tmpRemote = local2Remote_encoding(remoteDir);
ftp.changeWorkingDirectory("~");
ftp.changeWorkingDirectory(tmpRemote);
FTPFile[] fs = ftp.listFiles();
List<File> list = new ArrayList<File>();
// 过滤文件
List<FTPFile> files = filter(ftp, filterSelf(ftp, fs));
// 递归下载
for (FTPFile file : files) {
try {
String tmpName = file.getName();
if (file.isDirectory()) {
String remoteTmpDir = remoteDir + "/" + tmpName + "/";
String localTmpDir = localDir + "/" + tmpName + "/";
File localTmpFile = new File(localTmpDir);
if (localTmpFile.exists()) {
if (!localTmpFile.isDirectory()) {
throw new IOException("文件[" + localTmpFile.getName() + "]不是目录!");
}
} else {
localTmpFile.mkdirs();
}
list.addAll(downloadFile(ftp, remoteTmpDir, localTmpDir));
} else {
File f = downloadFile(ftp, file, remoteDir, localDir);
if (f != null) {
list.add(f);
log.info("文件[" + file.getName() + "/" + file.getSize() / (1024) + "KB]下载成功!");
}
}
} catch (IOException e) {
log.info("文件[" + file.getName() + "]下载失败!");
throw e;
}
}

return list;
}

/**
* @Description 概要说明:下载文件到本地
*
* @date 2015年4月3日 下午5:21:30
* @update xxx(yyyy-MM-dd)
* @param ftp
* ftp对象
* @param file
* 远程主机文件
* @param localDir
* 需要下载到本地目录
* @throws IOException
* void
*/
public File downloadFile(FTPClient ftp, FTPFile file, String remoteDir, String localDir) throws IOException {
String tmpName = file.getName();
ftp.changeWorkingDirectory("~");
remoteDir = local2Remote_encoding(remoteDir);
ftp.changeWorkingDirectory(remoteDir);
File localFile = new File(localDir + File.separator + tmpName);
tmpName = local2Remote_encoding(tmpName);
OutputStream os = null;
boolean isSucc = false;
try {
log.debug("localFile:" + localFile.getAbsoluteFile());
os = new FileOutputStream(localFile);
isSucc = ftp.retrieveFile(tmpName, os);
if (isSucc) {
return localFile;
}/*
* else{ try { InputStream in =
* ftp.retrieveFileStream("/"+remoteDir+"/"+tmpName); int len = -1;
* byte[] readB = new byte[1024]; while ((len = in.read(readB)) !=
* -1) { os.write(readB, 0, len); os.flush(); } isSucc = true;
* return localFile; } catch (IOException e) { throw e; } }
*/
return null;
} catch (IOException e) {
throw e;
} finally {
os.close();
if (!isSucc && localFile.exists()) {
log.warn("文件[" + tmpName + "/" + (file.getSize() / 1024) + "Kb]下载失败!");
localFile.delete();
}
}
}

/**
* @Description 概要说明:登录FTP
*
* @date 2015年4月3日 下午5:22:44
* @update xxx(yyyy-MM-dd)
* @param ftp
* FTP对象
* @param ip
* FTP主机IP地址
* @param port
* 端口
* @param username
* 用户名
* @param pwd
* 密码
* @return
* @throws SocketException
* @throws IOException
* boolean
*/
protected boolean login(FTPClient ftp, String ip, int port, String username, String pwd) throws SocketException, IOException {
ftp.connect(ip, port);
ftp.login(username, pwd);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
log.info("FTP Server IP:" + ip);
log.info("FTP Server User:" + username + " login success.");
reply = ftp.sendCommand("OPTS UTF8", "ON");
if (FTPReply.isPositiveCompletion(reply)) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
FTP_CONTROL_ENCODING_LOCAL = "UTF-8";
}

FTP_SYSTEM_TYPE = ftp.getSystemType();
FTP_CHARSET_NAME = ftp.getCharsetName();
FTP_CONTROL_ENCODING = ftp.getControlEncoding();

// 解决文件名乱码问题
ftp.setControlEncoding(FTP_CONTROL_ENCODING_LOCAL);
ftp.enterLocalPassiveMode();// 设置被动模式
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);// 设置传输的模式

return true;
}

/**
* @Description 概要说明:登录FTP
*
* @date 2015年4月3日 下午5:23:25
* @update xxx(yyyy-MM-dd)
* @param ftp
* FTP对象
* @param ip
* FTP主机IP地址
* @param username
* 用户名
* @param pwd
* 密码
* @return
* @throws SocketException
* @throws IOException
* boolean
*/
protected boolean login(FTPClient ftp, String ip, String username, String pwd) throws SocketException, IOException {
ftp.connect(ip);
ftp.login(username, pwd);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
log.info("FTP Server IP:" + ip);
log.info("FTP Server User:" + username + " login success.");
reply = ftp.sendCommand("OPTS UTF8", "ON");
if (FTPReply.isPositiveCompletion(reply)) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
FTP_CONTROL_ENCODING_LOCAL = "UTF-8";
}

FTP_SYSTEM_TYPE = ftp.getSystemType();
FTP_CHARSET_NAME = ftp.getCharsetName();
FTP_CONTROL_ENCODING = ftp.getControlEncoding();

// 解决文件名乱码问题
ftp.setControlEncoding(FTP_CONTROL_ENCODING_LOCAL);
ftp.enterLocalPassiveMode();// 设置被动模式
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);// 设置传输的模式

return true;
}

public String remote2Local_encoding(String remote) {
String result = null;
try {
result = new String(remote.getBytes(FTP_CONTROL_ENCODING), FTP_CONTROL_ENCODING_LOCAL);
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
return result;
}

public String local2Remote_encoding(String local) {
String result = null;
try {
result = new String(local.getBytes(FTP_CONTROL_ENCODING_LOCAL), FTP_CONTROL_ENCODING);
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
return result;
}

public String sys2Local_encoding(String sys) {
String result = null;
try {
result = new String(sys.getBytes(), FTP_CONTROL_ENCODING_LOCAL);
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
return result;
}

public String sys2Remote_encoding(String sys) {
String result = null;
try {
result = new String(sys.getBytes(), FTP_CONTROL_ENCODING);
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
return result;
}

/**
* @Description 概要说明:是否最新文件
*
* @date 2016年1月20日 下午3:49:47
* @update xxx(yyyy-MM-dd)
* @param ftp
* @param file
* @param remoteDir
* @return
* @throws IOException
* boolean
*/
public boolean isLast(FTPClient ftp, FTPFile file, String remoteDir) throws IOException {
String tmpRemote = local2Remote_encoding(remoteDir);
ftp.changeWorkingDirectory("~");
ftp.changeWorkingDirectory(tmpRemote);
FTPFile[] ftpFiles = ftp.listFiles();
List<FTPFile> files = filter(ftp, filterSelf(ftp, ftpFiles));
long lastTime = 0;
FTPFile tmpFile = null;
for (FTPFile ftpFile : files) {
long tmpLastTime = ftpFile.getTimestamp().getTimeInMillis();
if (tmpLastTime > lastTime) {
lastTime = tmpLastTime;
tmpFile = ftpFile;
}
}

return tmpFile == null ? false : file.getName().equals(tmpFile.getName());
}

/**
* @Description 概要说明:匹配结束字符文件
* @date 2016年1月20日 下午3:49:47
* @update xxx(yyyy-MM-dd)
* @param file
* FTP文件对象
* @param end
* 结束字符
* @return
* @throws IOException
* boolean
*/
public boolean endWith(FTPFile file, String end) throws IOException {
if (null == end) {
end = "";
}
return file.getName().endsWith(end);
}

public boolean isLastAndEndWith(FTPClient ftp, FTPFile file, String remoteDir, String end) throws IOException {
if (file.getName().endsWith(end)) {
String tmpRemote = local2Remote_encoding(remoteDir);
ftp.changeWorkingDirectory("~");
ftp.changeWorkingDirectory(tmpRemote);
FTPFile[] ftpFiles = ftp.listFiles();
List<FTPFile> files = filter(ftp, filterSelf(ftp, ftpFiles));
long lastTime = 0;
FTPFile tmpFile = null;
for (FTPFile ftpFile : files) {
long tmpLastTime = ftpFile.getTimestamp().getTimeInMillis();
if (ftpFile.getName().endsWith(end) && tmpLastTime > lastTime) {
lastTime = tmpLastTime;
tmpFile = ftpFile;
}
}

return tmpFile == null ? false : file.getName().equals(tmpFile.getName());
}

return false;
}

public static void main(String args[]) {
FTPService ftp = new FTPService();
String ip = "127.0.0.1";
int port = 1111;
String username = "crd";
String pwd = "crd";
String remoteDir = "";
String localDir = "C:/Users/Administrator/Desktop/test";
ftp.download(ip, port, username, pwd, remoteDir, localDir);
System.out.println(122);
}

}

ftp工具类的更多相关文章

  1. java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)

    1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...

  2. FTP工具类开发

    正所谓工欲善其事必先利其器,熟悉了下一套流程,以此铭记. 1.FTP服务搭建 由于本人使用wondiow系统,所以针对window的童鞋们可以查看.至于windowX这里配置类似,所以不要纠结于win ...

  3. Java操作FTP工具类(实例详解)

    这里使用Apache的FTP jar 包 没有使用Java自带的FTPjar包  工具类 package com.zit.ftp; import java.io.File; import java.i ...

  4. 静态资源上传至远程ftp服务器,ftp工具类封装

    工具类,是一个单独的工程项目 提取必要信息至ftp.properties配置文件中 ftp_host=192.168.110.128 ftp_port=21 ftp_username=ftpuser ...

  5. Java-FtpUtil工具类

    package cn.ipanel.app.newspapers.util; import java.io.BufferedReader; import java.io.DataInputStream ...

  6. java 工具类使用

    BigDecimalUtil 金额计算工具类 import java.math.BigDecimal; public class BigDecimalUtil { private BigDecimal ...

  7. FTP上传-封装工具类

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  8. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  9. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

随机推荐

  1. 【代码审计】YzmCMS_PHP_v3.6 代码执行漏洞分析

      0x00 环境准备 YzmCMS官网:http://www.yzmcms.com/ 程序源码下载:http://pan.baidu.com/s/1pKA4u99 测试网站首页: 0x01 代码分析 ...

  2. 终端IO(上)

    一.综述 终端IO有两种不同的工作方式: 规范方式输入处理.在这种方式中,终端输入以行为单位进行处理.对于每个读要求,终端驱动程序最多返回一行. 非规范方式输入处理.输入字符不以行为单位进行装配 如果 ...

  3. 【netcore基础】wwwroot下静态资源文件访问权限控制

    本文参考如下博问 https://q.cnblogs.com/q/107836 业务要求 上传的资源文件(.mp3 .mp4等)只有购买了之后才能有权限访问,所以对上传的资源文件目录进行访问权限控制 ...

  4. Clover 3 --- Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能。

    http://cn.ejie.me/ http://cn.ejie.me/uploads/setup_clover@3.4.6.exe  软件下载 默认图标实在比较难看,更换图标 更改图标---选择图 ...

  5. Oracle数据库入门——基础知识

    1.安装完成Oracle数据库后,使用sqlplus客户端登录数据库管理系统,只输入用户名,没有输入密码时,会提示口令为空,登录被拒绝. 请输入用户名:system 输入口令: ERROR:ORA-0 ...

  6. Thrift的一些概念

    Thrift最初是由Facebook开发的,因为随着流量和网络结构的扩展,一些操作如搜索.分发.事件日志记录等已经超出系统的处理范围,所以Facebook的工程师开发服务时选择了多种不同的编程语言来达 ...

  7. .NET Core开发日志——Global Tools

    .NET Core 2.1引入了一个新的功能,Global Tools,其本质是包含控制台应用程序的nuget包,目前而言,还没有特别有用的工具,不过相信随着时间的推移,各种有创意或者实用性强的Glo ...

  8. 泡泡一分钟:Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition

    Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition Peng Yin, Lingyun Xu, Z ...

  9. 2017年蓝桥杯省赛A组c++第1题(走迷宫)

    /* 标题:迷宫 X星球的一处迷宫游乐场建在某个小山坡上. 它是由10x10相互连通的小房间组成的. 房间的地板上写着一个很大的字母. 我们假设玩家是面朝上坡的方向站立,则: L表示走到左边的房间, ...

  10. [development][profile][dpdk] KK程序性能调优

    KK程序: 1. 两个线程,第一个从DPDK收包,通过一个ring数据传递给第二个线程.第二个线程将数据写入共享内存. 2. 第二个内存在发现共享内存已满时,会直接丢弃数据. 3. 线程二有个选项de ...