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. java.security.ProviderException: java.security.KeyException

    本机部署没问题,部署到linux服务器报错: javax.net.ssl.SSLException: java.security.ProviderException: java.security.Ke ...

  2. 如何在Ubuntu 16.04上安装配置Redis

    如何在Ubuntu 16.04上安装配置Redis Redis是一个内存中的键值存储,以其灵活性,性能和广泛的语言支持而闻名.在本指南中,我们将演示如何在Ubuntu 16.04服务器上安装和配置Re ...

  3. error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2

    使用VS2013版本引用外部的lib进行编译时候提示: 错误 25 error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项:  值“0”不匹配值“2”(jrtpl ...

  4. Python实现Linux命令xxd -i功能

    目录 Python实现Linux命令xxd -i功能 声明 一. Linux xxd -i功能 二. xxd -i常见用途 三. 类xxd -i功能的Python实现 Python实现Linux命令x ...

  5. EXCEL 2007施工进度横道图制作步骤及实战练习

    [知识讲解] 1.将图表中的部分图形“隐藏”起来 如果为了实现某种特殊的图表效果,需要将图表中的部分图形“隐藏”起来,除了将该系列删除外(有时候这种方法不能达到所需要的效果),还可以通过下面的方法来实 ...

  6. Spring学习笔记--代理

    静态代理 1.静态代理的角色分析(UML画图推荐使用StarUML软件) 抽象角色---一般使用接口或者抽象类来实现. 真实角色---被代理的角色. 代理角色---代理真实角色—代理真是角色后一般会做 ...

  7. web -- Navigator.vibrate(); 使设备(有振动硬件)产生有频率的振动

    MDN 文档 此方法需要用户手势. 否则,它返回false. const koa2 = require(`koa2`); const Router = require(`koa-router`); c ...

  8. Docker 修改国内镜像地址

    curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://86d2a50b.m.daocloud.io 该脚本可以 ...

  9. The Salt Master has cached the public key报错解决办法

    参考:http://www.52devops.com/chuck/814.html 查看salt-minion的运行状态,显示salt-master已经缓存了这个minion,但是minion在重新认 ...

  10. 关于Dosbox0.74无法使用masm命令

    今天尝试在dosbox里编译asm源代码文件 但是提示“illegal command”,也就是非法命令 开始还以为我的dosbox版本不对 但是去网上查阅资料发现别人用这个版本都可以使用 所以百思不 ...