增强的Java FTP工具----扩展免费版的edtftpj
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.WriteMode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.StringTookit;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
/**
* FTP增强工具
*
* @author leizhimin 2008-12-13 16:13:01
*/
public class UltraFTPClient extends FileTransferClient {
private static Log log = LogFactory.getLog(UltraFTPClient.class);
public UltraFTPClient() {
}
/**
* 下载文件(夹),在本地保持FTP上的目录结构
*
* @param localFolderPath 本地存放文件夹
* @param remotePath 远程文件(夹)路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws FTPException
* @throws IOException
*/
public void ftpDownload(final String localFolderPath, final String remotePath, String remoteSubPath) throws FTPException, IOException, ParseException {
if (isDir(remoteSubPath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, StringTookit.getParentPath(remotePath));
if (!new File(localPath).exists())
new File(localPath).mkdirs();
String[] x = directoryNameList(remoteSubPath, false);
for (String fname : x) {
String rmFilePath = StringTookit.formatPath(remoteSubPath + "/" + fname);
if (isDir(rmFilePath)) {
ftpDownload(localFolderPath, remotePath, rmFilePath);
} else {
String _localPath = localFolderPath + "/" +
StringTookit.getRelativeRootPath(rmFilePath, StringTookit.getParentPath(remotePath));
downloadFile(_localPath, rmFilePath, WriteMode.OVERWRITE);
}
}
} else if (isFile(remotePath)) {
String localPath = localFolderPath + StringTookit.getRelativeRootPath(remoteSubPath, remotePath);
downloadFile(localPath, remoteSubPath, WriteMode.OVERWRITE);
} else {
log.error("所下载的文件或文件夹不存在,请检查!");
}
log.info("FTP下载从服务器上的" + remoteSubPath + "下载到本地" + localFolderPath + "结束!");
}
/**
* 上传文件(夹),在FTP上保持本地的目录结构
*
* @param localFile 本地文件
* @param localFilePath 本地文件的路径
* @param remoteSubPath 远程文件存放相对根目录
* @throws IOException
* @throws FTPException
*/
public void ftpUpload(File localFile, final String localFilePath, final String remoteSubPath) throws IOException, FTPException {
if (localFile.isDirectory()) {
for (File file : localFile.listFiles()) {
if (file.isDirectory()) {
String remotePath = StringTookit.formatPath(remoteSubPath) + StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
log.info(remotePath);
if (!isExist(remotePath)) createDirectory(remotePath);
ftpUpload(file, localFilePath, remoteSubPath);
} else {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(file.getPath(), StringTookit.getParentPath(localFilePath));
uploadFile(file.getPath(), remotePath, WriteMode.APPEND);
}
}
} else if (localFile.isFile()) {
String remotePath = StringTookit.formatPath(remoteSubPath) +
StringTookit.getRelativeRootPath(localFile.getPath(), StringTookit.getParentPath(localFilePath));
if (!isExist(StringTookit.getParentPath(remotePath)))
createDirectory(StringTookit.getParentPath(remotePath));
System.out.println(remotePath);
uploadFile(localFile.getPath(), remotePath, WriteMode.APPEND);
}
log.info("FTP上传" + localFile.getPath() + "到" + remoteSubPath + "目录下结束!");
}
/**
* @param remotePath 远程文件(夹)路径
* @return 远程的文件(夹)资源是否存在
*/
public boolean isExist(String remotePath) {
boolean flag = true;
try {
directoryList(remotePath);
} catch (Exception e) {
flag = false;
}
return flag;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件时返回ture,否则返回false
*/
public boolean isFile(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return true;
}
}
} catch (Exception e) {
}
return false;
}
/**
* @param remotePath 远程文件(夹)路径
* @return 当远程资源存在且为文件夹时返回ture,否则返回false
*/
public boolean isDir(String remotePath) {
try {
int size = directoryList(remotePath).length;
if (size >= 0) {
if (exists(remotePath)) {
return false;
} else {
return true;
}
}
} catch (Exception e) {
}
return false;
}
}
* 字符串工具箱
*
* @author leizhimin 2008-12-15 22:40:12
*/
public final class StringTookit {
/**
* 将一个字符串的首字母改为大写或者小写
*
* @param srcString 源字符串
* @param flag 大小写标识,ture小写,false大些
* @return 改写后的新字符串
*/
public static String toLowerCaseInitial(String srcString, boolean flag) {
StringBuilder sb = new StringBuilder();
if (flag) {
sb.append(Character.toLowerCase(srcString.charAt(0)));
} else {
sb.append(Character.toUpperCase(srcString.charAt(0)));
}
sb.append(srcString.substring(1));
return sb.toString();
}
/**
* 将一个字符串按照句点(.)分隔,返回最后一段
*
* @param clazzName 源字符串
* @return 句点(.)分隔后的最后一段字符串
*/
public static String getLastName(String clazzName) {
String[] ls = clazzName.split("//.");
return ls[ls.length - 1];
}
/**
* 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的"/"符号。
*
* @param path 文件路径
* @return 格式化后的文件路径
*/
public static String formatPath(String path) {
String reg = "+|/+";
String temp = path.trim().replaceAll(reg, "/");
if (temp.endsWith("/")) {
return temp.substring(0, temp.length() - 1);
}
return temp;
}
/**
* 获取文件父路径
*
* @param path 文件路径
* @return 文件父路径
*/
public static String getParentPath(String path) {
return new File(path).getParent();
}
/**
* 获取相对路径
*
* @param fullPath 全路径
* @param rootPath 根路径
* @return 相对根路径的相对路径
*/
public static String getRelativeRootPath(String fullPath, String rootPath) {
String relativeRootPath = null;
String _fullPath = formatPath(fullPath);
String _rootPath = formatPath(rootPath);
if (_fullPath.startsWith(_rootPath)) {
relativeRootPath = fullPath.substring(_rootPath.length());
} else {
throw new RuntimeException("要处理的两个字符串没有包含关系,处理失败!");
}
if (relativeRootPath == null) return null;
else
return formatPath(relativeRootPath);
}
}
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import zzvcom.cms.ccm.commons.JavaXmlTookit;
import zzvcom.cms.ccm.commons.SysParamsTookit;
/**
* FTP配置
*
* @author leizhimin 2008-12-5 22:49:39
*/
public class FtpServerConfigration {
private static final Log log = LogFactory.getLog(FtpServerConfigration.class);
private String username; //用户名
private String password; //密码
private String ip; //ip
private Integer port; //端口
private Integer timeout; //超时时间
private Integer buffersize; //缓存大小
private Integer notifytime; //通知时间
private String connectMode; //连接模式
private String encoding; //编码方式
public FtpServerConfigration(String username, String password, String ip, Integer port) {
this.username = username;
this.password = password;
this.ip = ip;
this.port = port;
this.timeout = Integer.valueOf(SysParamsTookit.getProperty("timeout", "36000000"));
this.buffersize = Integer.valueOf(SysParamsTookit.getProperty("buffersize", "2048000"));
this.notifytime = Integer.valueOf(SysParamsTookit.getProperty("notifytime", "5000"));
this.connectMode = SysParamsTookit.getProperty("connectMode", "PASV");
this.encoding = SysParamsTookit.getProperty("encoding", "GBK");
}
public FtpServerConfigration(String ftpConfigXml) {
FtpServerConfigration config = (FtpServerConfigration) JavaXmlTookit.xml2Java(ftpConfigXml, FtpServerConfigration.class);
if (StringUtils.isBlank(config.getUsername())
|| StringUtils.isBlank(config.getPassword())
|| StringUtils.isBlank(config.getIp())
|| config.getPort() == null) {
log.error("FTP最基本的配置属性(username、password、ip、port)不能为空,请检查!");
} else {
this.username = config.getUsername();
this.password = config.getPassword();
this.ip = config.getIp();
this.port = config.getPort();
}
if (config.getTimeout() == null)
this.timeout = Integer.valueOf(SysParamsTookit.getProperty("timeout", "36000000"));
if (config.getBuffersize() == null)
this.buffersize = Integer.valueOf(SysParamsTookit.getProperty("buffersize", "2048000"));
if (config.getNotifytime() == null)
this.notifytime = Integer.valueOf(SysParamsTookit.getProperty("notifytime", "5000"));
if (StringUtils.isBlank(config.getConnectMode()))
this.connectMode = SysParamsTookit.getProperty("connectMode", "PASV");
if (StringUtils.isBlank(config.getEncoding()))
this.encoding = SysParamsTookit.getProperty("encoding", "GBK");
}
/**
* 获取当前FTP连接配置
*
* @return 当前FTP连接配置
*/
public FtpServerConfigration getConfigration() {
return this;
}
/**
* 构建FTP客户端连接,并进行连接
*
* @return FTP客户端连接
* @throws Exception 当构建客户端失败时抛出
*/
public UltraFTPClient buildFtpClient() throws Exception {
UltraFTPClient client = new UltraFTPClient();
try {
client.setUserName(username);
client.setPassword(password);
client.setRemoteHost(ip);
client.setRemotePort(port);
client.setTimeout(timeout);
client.getAdvancedSettings().setTransferBufferSize(buffersize);
client.getAdvancedSettings().setTransferNotifyInterval(notifytime);
client.getAdvancedSettings().setControlEncoding(encoding);
// client.setEventListener(new UploadListener(client)); //设置事件监听器
if (connectMode.equalsIgnoreCase("ACTIVE")) {
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.ACTIVE); //设置为被动模式
} else if (connectMode.equalsIgnoreCase("PASV")) {
client.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV); //设置为被动模式
} else {
log.error("标识为" + connectMode + "的FTP连接模式配置错误,连接模式仅有两种ACTIVE和PASV,请检查!");
}
client.connect();
log.info("FTP连接成功!详细信息(远程主机:" + ip + ",用户名:" + username + ")");
} catch (Exception e) {
log.info("FTP创建连接发生异常!", e);
throw e;
}
return client;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public Integer getTimeout() {
return timeout;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public Integer getBuffersize() {
return buffersize;
}
public void setBuffersize(Integer buffersize) {
this.buffersize = buffersize;
}
public Integer getNotifytime() {
return notifytime;
}
public void setNotifytime(Integer notifytime) {
this.notifytime = notifytime;
}
public String getConnectMode() {
return connectMode;
}
public void setConnectMode(String connectMode) {
this.connectMode = connectMode;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
}
# FTP连接模式:ACTIVE,PASV为两种连接模式
#port=21
ftp.timeout=360000
ftp.buffersize=20480
ftp.notifytime=5000
ftp.connectMode=PASV
ftp.encoding=GBK
转载于:https://www.cnblogs.com/cuker919/archive/2010/06/24/4878614.html
增强的Java FTP工具----扩展免费版的edtftpj相关推荐
- ftp工具上传,三大ftp上传工具(推荐)
对于ftp上传工具,你了解多少?其实一般人也接触不到这种软件.ftp上传工具软件主要是针对从事网站管理的工作人员比较有利的一款工具.可以帮助他们快速的解决工作中的问题.方便.简单.快捷又明了的解决问题 ...
- 上传ftp工具,细数5款很好用的上传ftp工具
一提到ftp上传软件,大家第一个想到的不知道是什么,有可能你不太熟悉,但从事网站管理人员一定都用过.ftp上传软件是网络上用来传送文件的工具.ftp上传软件以汉化版和破解版的居多.但是破解版的一般都比 ...
- ftp 工具 绿色,这6款ftp 工具 绿色是站长们不可或缺的必备工具
可能很多人都不太熟悉什么是ftp客户端工具,但是这个工具使用的人还是非常多的,比如那些从事网站管理的人员.他们的日常工作中接触的最多的工具可能就是ftp客户端工具.这款工具一般是英文版的偏多,那有没有 ...
- ftp 工具 绿色,四款将会让你爱不释手的绿色 ftp 工具
可能很多人都不太熟悉什么是ftp工具,但是这个工具使用的人还是非常多的,比如那些从事网站管理的人员.他们的日常工作中接触的最多的工具可能就是ftp工具.这款工具一般是英文版的偏多,那有没有我们中国自己 ...
- ftp工具 绿色,细数3款绿色 ftp工具
网络上最不好找的就是绿色版的工具,毕竟在现在这个年代不可能让你免费使用别人的产品,所以绿色版的工具是真的难找.相信常常使用电脑的小伙伴也都知道,绿色版的软件都不好找,更何况是ftp上传工具绿色版,ft ...
- 手写一个好用的Java FTP操作工具类
前言 网上百度了很多FTP的java 工具类,发现文章代码都比较久远,且代码臃肿,即使搜到了代码写的还可以的,封装的常用操作方法不全面,于是自己花了半天实现一个好用的工具类.最初想用java自带的FT ...
- java ftp封装_使用FTP连接池封装Java工具类
使用FTP连接池封装工具类 背景 早前的思路是将FTP连接的管理(对象池功能)与FTP工具类(上传下载方法)在一个工程中实现,在工具类中调用是模板类提供的模板方法, 由模板方法与对象池打交道--初始时 ...
- java ftp ftpclient_详解JAVA中使用FTPClient工具类上传下载
详解JAVA中使用FTPClient工具类上传下载 在Java程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件.本文简单介绍如何利用jakarta commons中的FTPClie ...
- 高效Java编程工具集锦
Java 开发者常常都会想办法如何更快地编写 Java 代码,让编程变得更加轻松.目前,市面上涌现出越来越多的高效编程工具.所以,以下总结了一系列工具列表,其中包含了大多数开发人员已经使用.正在使用或 ...
最新文章
- 【PHPExcel】各种数据类型
- 【转】学习asp.net比较完整的流程
- python霍夫变换检测直线_OpenCV-Python教程(9、使用霍夫变换检测直线)
- 【51nod2026】Gcd and Lcm(杜教筛)
- mysql数据库druid密码加密_Druid数据库密码加密
- angular之性能优化
- 轻量函数式 JavaScript:十、函数式异步
- 深度学习pytorch基础入门教程(1小时)-张量、操作、转换
- 网络蜘蛛及搜索引擎基本原理
- 数学建模-时间序列分析
- 脑科学磁共振成像(MRI)初学者必看——功能脑网络、小世界网络、FDR校正、脑电信号频率变换、模板、假设检验、广义线性模型、独立成分分析、影像组学、任务态和静息态方法汇总
- 巴比特 | 元宇宙每日必读:央视网在Polygon发布数字藏品,支持无封闭期的转赠,这是什么新玩法?...
- SSL证书是要怎么部署的,SSL证书怎么安装你知道吗?
- 基于工业智能网关的PLC远程控制解决方案
- 【Linux命令行与Shell脚本编程】第五章 理解 Shell 父子关系 后台进程 协程
- 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include pch.h”?
- 基于百万级别的站内信设计
- 印度人写的java代码
- meter元素的使用
- kafka基础入门(4):kafka消费者
热门文章
- 荣耀es升级鸿蒙,华为手机明年全部升级鸿蒙系统 所有自研设备换OS
- oc 画一个圆弧_SolidWorks一步扫描特征,就可以画出一个螺母,你有思路吗
- html内容封装为一个对象_技术赋能还是内容为王,哪一个才是短视频创作的关键?...
- mysql 表损坏_MYSQL数据表损坏的原因分析和修复方法小结(推荐)
- 语言中要输出表格_C语言 | 表格输出若干人的信息
- 楚留香手游服务器维护,【楚留香手游】4.27日游戏维护公告
- 计算机电路基础答案,计算机电路基础模拟试卷答案(B)
- fetch用英语解释_fetch的意思在线翻译,解释fetch中文英文含义,短语词组,音标读音,例句,词源,同义词【澳典网ODict.Net】...
- axure8.0发布html,Axure 8.0 beta最后一批新增和改进功能的介绍
- 动画制作c语言程序,C语言动画制作