- 浏览: 419408 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (146)
- Java (17)
- Oracle (22)
- 非技术 (15)
- 操作系统 (4)
- 菜鸟学Linux (7)
- 技术综合 (12)
- 数据库 (5)
- SSH框架 (6)
- Excel (1)
- FTP (1)
- Exception (18)
- Struts2 (1)
- eclipse (5)
- tomcat (2)
- openbravo (1)
- project.net (4)
- Alfresco (3)
- CAS (3)
- IntelliJ IDEA (5)
- MySQL (1)
- SQL Server (3)
- Windows (2)
- jboss (2)
- VMware (1)
- JavaScript (1)
- google (1)
- Extjs (1)
- Eclipse cvs (1)
最新评论
-
busiying119:
limin914245697 写道课后没答案 写道非常感谢楼主 ...
Spring rmi配置 -
limin914245697:
课后没答案 写道非常感谢楼主,在网上找了好几个Spring、R ...
Spring rmi配置 -
u010311110:
把service借口和java bean打成jar包放到客户端 ...
Spring rmi配置 -
课后没答案:
非常感谢楼主,在网上找了好几个Spring、RMI的代码例子, ...
Spring rmi配置 -
jiawang827:
按照上述步骤配置不成功啊,报java.lang.ClassNo ...
Alfresco-Community-4.0.e安装配置
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能。可以将ftp4j嵌到你的Java应用中,来传输文件(包括上传和下载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括:通过TCP/IP直接连接,通过FTP代理、HTTP代理、SOCKS4/4a代理和SOCKS5代理连接,通过SSL安全连接。
ftp4j该项目主页:
http://www.sauronsoftware.it/projects/ftp4j/
http://www.sauronsoftware.it/projects/ftp4j/manual.php
ftp4j这是一个基本类库,用起来有些不爽,首先是受检查异常太多太多,这是合理的,把异常留给使用者灵活处理,其次是提供的客户单API太基础,还不够强悍。下面是我针对实际中最常用的功能所作的一个工具类。
在对待异常的方式上,将检查异常全转换为运行时异常,并对一些潜在操作的错误进行检查,提供了原API中没有的一些功能,批量下载、任务侦听器、检查FTP上文件或目录是否存在以及类型。
下面是实现代码:
package lavasoft.common.ftp;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
import lavasoft.common.PathToolkit;
import java.io.File;
import java.util.List;
/**
* TTP客户端工具
*
* @author leizhimin 2009-11-30 10:20:17
*/
public final class FTPToolkit {
private FTPToolkit() {
}
/**
* 创建FTP连接
*
* @param host 主机名或IP
* @param port ftp端口
* @param username ftp用户名
* @param password ftp密码
* @return 一个客户端
*/
public static FTPClient makeFtpConnection(String host, int port, String username, String password) {
FTPClient client = new FTPClient();
try {
client.connect(host, port);
client.login(username, password);
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
return client;
}
/**
* FTP下载文件到本地一个文件夹,如果本地文件夹不存在,则创建必要的目录结构
*
* @param client FTP客户端
* @param remoteFileName FTP文件
* @param localFolderPath 存的本地目录
*/
public static void download(FTPClient client, String remoteFileName, String localFolderPath) {
int x = isExist(client, remoteFileName);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
File localFolder = new File(localFolderPath);
if (localFolder.isFile()) {
throw new FTPRuntimeException("所要的下载保存的地方是一个文件,无法保存!");
} else {
if (!localFolder.exists())
localFolder.mkdirs();
}
if (x == FTPFile.TYPE_FILE) {
String localfilepath = PathToolkit.formatPath4File(localFolderPath + File.separator + new File(remoteFileName).getName());
try {
if (listener != null)
client.download(remoteFileName, new File(localfilepath), listener);
else
client.download(remoteFileName, new File(localfilepath));
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
} else {
throw new FTPRuntimeException("所要下载的文件" + remoteFileName + "不存在!");
}
}
/**
* FTP上传本地文件到FTP的一个目录下
*
* @param client FTP客户端
* @param localfile 本地文件
* @param remoteFolderPath FTP上传目录
*/
public static void upload(FTPClient client, File localfile, String remoteFolderPath) {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
try {
client.changeDirectory(remoteFolderPath);
if (!localfile.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "不存在!");
if (!localfile.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "是一个文件夹!");
if (listener != null)
client.upload(localfile, listener);
else
client.upload(localfile);
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* FTP上传本地文件到FTP的一个目录下
*
* @param client FTP客户端
* @param localfilepath 本地文件路径
* @param remoteFolderPath FTP上传目录
*/
public static void upload(FTPClient client, String localfilepath, String remoteFolderPath) {
File localfile = new File(localfilepath);
upload(client, localfile, remoteFolderPath);
}
/**
* 批量上传本地文件到FTP指定目录上
*
* @param client FTP客户端
* @param localFilePaths 本地文件路径列表
* @param remoteFolderPath FTP上传目录
*/
public static void uploadListPath(FTPClient client, List<String> localFilePaths, String remoteFolderPath) {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
try {
client.changeDirectory(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
for (String path : localFilePaths) {
File file = new File(path);
if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "不存在!");
if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "是一个文件夹!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* 批量上传本地文件到FTP指定目录上
*
* @param client FTP客户端
* @param localFiles 本地文件列表
* @param remoteFolderPath FTP上传目录
*/
public static void uploadListFile(FTPClient client, List<File> localFiles, String remoteFolderPath) {
try {
client.changeDirectory(remoteFolderPath);
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
for (File file : localFiles) {
if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "不存在!");
if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "是一个文件夹!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* 判断一个FTP路径是否存在,如果存在返回类型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、FTPFile.TYPE_LINK=2)
* 如果文件不存在,则返回一个-1
*
* @param client FTP客户端
* @param remotePath FTP文件或文件夹路径
* @return 存在时候返回类型值(文件0,文件夹1,连接2),不存在则返回-1
*/
public static int isExist(FTPClient client, String remotePath) {
remotePath = PathToolkit.formatPath4FTP(remotePath);
int x = -1;
FTPFile[] list = null;
try {
list = client.list(remotePath);
} catch (Exception e) {
return -1;
}
if (list.length > 1) return FTPFile.TYPE_DIRECTORY;
else if (list.length == 1) {
FTPFile f = list[0];
if (f.getType() == FTPFile.TYPE_DIRECTORY) return FTPFile.TYPE_DIRECTORY;
//假设推理判断
String _path = remotePath + "/" + f.getName();
try {
int y = client.list(_path).length;
if (y == 1) return FTPFile.TYPE_DIRECTORY;
else return FTPFile.TYPE_FILE;
} catch (Exception e) {
return FTPFile.TYPE_FILE;
}
} else {
try {
client.changeDirectory(remotePath);
return FTPFile.TYPE_DIRECTORY;
} catch (Exception e) {
return -1;
}
}
}
/**
* 关闭FTP连接,关闭时候像服务器发送一条关闭命令
*
* @param client FTP客户端
* @return 关闭成功,或者链接已断开,或者链接为null时候返回true,通过两次关闭都失败时候返回false
*/
public static boolean closeConnection(FTPClient client) {
if (client == null) return true;
if (client.isConnected()) {
try {
client.disconnect(true);
return true;
} catch (Exception e) {
try {
client.disconnect(false);
} catch (Exception e1) {
e1.printStackTrace();
return false;
}
}
}
return true;
}
}
package lavasoft.common;
import java.io.File;
/**
* 路径处理工具,操作系统自适应
*
* @author leizhimin 2009-11-30 16:01:34
*/
public final class PathToolkit {
private PathToolkit() {
}
/**
* 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的文件路径分隔符。
* 本方法操作系统自适应
*
* @param path 文件路径
* @return 格式化后的文件路径
*/
public static String formatPath4File(String path) {
String reg0 = "\\\\+";
String reg = "\\\\+|/+";
String temp = path.trim().replaceAll(reg0, "/");
temp = temp.replaceAll(reg, "/");
if (temp.length() > 1 && temp.endsWith("/")) {
temp = temp.substring(0, temp.length() - 1);
}
temp = temp.replace('/', File.separatorChar);
return temp;
}
/**
* 格式化文件路径,将其中不规范的分隔转换为标准的分隔符
* 并且去掉末尾的"/"符号(适用于FTP远程文件路径或者Web资源的相对路径)。
*
* @param path 文件路径
* @return 格式化后的文件路径
*/
public static String formatPath4FTP(String path) {
String reg0 = "\\\\+";
String reg = "\\\\+|/+";
String temp = path.trim().replaceAll(reg0, "/");
temp = temp.replaceAll(reg, "/");
if (temp.length() > 1 && temp.endsWith("/")) {
temp = temp.substring(0, temp.length() - 1);
}
return temp;
}
/**
* 获取FTP路径的父路径,但不对路径有效性做检查
*
* @param path FTP路径
* @return 父路径,如果没有父路径,则返回null
*/
public static String genParentPath4FTP(String path) {
String pp = new File(path).getParent();
if (pp == null) return null;
else return formatPath4FTP(pp);
}
}
package lavasoft.common.ftp;
/**
* FTP操作类型
*
* @author leizhimin 2009-11-30 11:16:59
*/
public enum FTPOptType {
UP("上传"),
DOWN("下载"),
LIST("浏览"),
DELFILE("删除文件"),
DELFOD("删除文件夹"),
RENAME("上传");
private String optname;
FTPOptType(String optname) {
this.optname = optname;
}
public String getOptname() {
return optname;
}
}
package lavasoft.common.ftp;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
/**
* FTP监听器,做了简单实现,可以使用commons logger替换System.out.println
*
* @author leizhimin 2009-11-30 11:05:33
*/
public class MyFtpListener implements FTPDataTransferListener {
private FTPOptType optType;
public static MyFtpListener instance(FTPOptType optType) {
return new MyFtpListener(optType);
}
private MyFtpListener(FTPOptType optType) {
this.optType = optType;
}
public void started() {
System.out.println(optType.getOptname() + ":FTP启动喽。。。。。。");
}
public void transferred(int length) {
System.out.println(optType.getOptname() + ":FTP传输喽。。。。。。");
}
public void completed() {
System.out.println(optType.getOptname() + ":FTP完成喽。。。。。。");
}
public void aborted() {
System.out.println(optType.getOptname() + ":FTP中止喽。。。。。。");
}
public void failed() {
System.out.println(optType.getOptname() + ":FTP挂掉喽。。。。。。");
}
}
package lavasoft.common.ftp;
/**
* FTP异常
*
* @author leizhimin 2009-11-30 10:28:03
*/
public class FTPRuntimeException extends RuntimeException {
public FTPRuntimeException() {
super();
}
public FTPRuntimeException(String message) {
super(message);
}
public FTPRuntimeException(String message, Throwable cause) {
super(message, cause);
}
public FTPRuntimeException(Throwable cause) {
super(cause);
}
}
package lavasoft;
import it.sauronsoftware.ftp4j.FTPClient;
import lavasoft.common.ftp.FTPToolkit;
/**
* 简单测试下
*
* @author leizhimin 2009-11-30 12:25:42
*/
public class Test {
public static void main(String args[]) throws Exception {
String ftpip = "192.168.104.113";
int ftpport = 21;
String ftpuser = "vcomkp.ftpadmin";
String ftppswd = "ftp";
FTPClient client = FTPToolkit.makeFtpConnection(ftpip, ftpport, ftpuser, ftppswd);
FTPToolkit.upload(client, "C:\\Dynamicclrr4.zip", "/aaa/bbb/ccc");
FTPToolkit.download(client, "/Dynamicclrr4.zip", "D:\\");
FTPToolkit.closeConnection(client);
}
}
上传:FTP启动喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP完成喽。。。。。。
上传:FTP启动喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP完成喽。。。。。。
Process finished with exit code 0
本程序在RedHat AS 5上使用vsftpd测试通过,一般来说windows下测试通过的ftp工具,一般在Linux下通不过,因为Windows没有太多权限控制。而Linux控制非常严格,常常因为没有权限而导致操作失败,这时候,要对Linux的FTP用户给读写的权限才能正确执行以上代码。
另外说明一点,在判断ftp服务器上一个文件是否存在的isExist方法,具有一定一个局限性,体现在两方面:一是对特定组件版本的依赖性,不同版本list等方法实现不同,对错误路径的处理也不同。二是,不同的ftp服务器对发送list命令反馈的结果不尽相同。
这也是为什么很多ftp组件工具不去实现isExist的方法。对使用本方法测试有异常不符合事实的情况下,需要根据特定的环境测试重新实现isExist方法即可。
这里我给出了一个测试:
我用的vsftp目录结构如下
/aaa
/aaa/bbb/ccc
/aaa/bbb/ccc/Dynamicclrr4.zip
调用方法测试:
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc")); //目录
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc/Dynamicclrr4.zip")); //文件
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc/xxxxxx.zip")); //不存在的文件
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/pppppdd")); //不存在的目录
1
0
-1
-1
Process finished with exit code 0
可见,打印的结果与实际情况相符。
ftp4j该项目主页:
http://www.sauronsoftware.it/projects/ftp4j/
http://www.sauronsoftware.it/projects/ftp4j/manual.php
ftp4j这是一个基本类库,用起来有些不爽,首先是受检查异常太多太多,这是合理的,把异常留给使用者灵活处理,其次是提供的客户单API太基础,还不够强悍。下面是我针对实际中最常用的功能所作的一个工具类。
在对待异常的方式上,将检查异常全转换为运行时异常,并对一些潜在操作的错误进行检查,提供了原API中没有的一些功能,批量下载、任务侦听器、检查FTP上文件或目录是否存在以及类型。
下面是实现代码:
package lavasoft.common.ftp;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
import lavasoft.common.PathToolkit;
import java.io.File;
import java.util.List;
/**
* TTP客户端工具
*
* @author leizhimin 2009-11-30 10:20:17
*/
public final class FTPToolkit {
private FTPToolkit() {
}
/**
* 创建FTP连接
*
* @param host 主机名或IP
* @param port ftp端口
* @param username ftp用户名
* @param password ftp密码
* @return 一个客户端
*/
public static FTPClient makeFtpConnection(String host, int port, String username, String password) {
FTPClient client = new FTPClient();
try {
client.connect(host, port);
client.login(username, password);
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
return client;
}
/**
* FTP下载文件到本地一个文件夹,如果本地文件夹不存在,则创建必要的目录结构
*
* @param client FTP客户端
* @param remoteFileName FTP文件
* @param localFolderPath 存的本地目录
*/
public static void download(FTPClient client, String remoteFileName, String localFolderPath) {
int x = isExist(client, remoteFileName);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
File localFolder = new File(localFolderPath);
if (localFolder.isFile()) {
throw new FTPRuntimeException("所要的下载保存的地方是一个文件,无法保存!");
} else {
if (!localFolder.exists())
localFolder.mkdirs();
}
if (x == FTPFile.TYPE_FILE) {
String localfilepath = PathToolkit.formatPath4File(localFolderPath + File.separator + new File(remoteFileName).getName());
try {
if (listener != null)
client.download(remoteFileName, new File(localfilepath), listener);
else
client.download(remoteFileName, new File(localfilepath));
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
} else {
throw new FTPRuntimeException("所要下载的文件" + remoteFileName + "不存在!");
}
}
/**
* FTP上传本地文件到FTP的一个目录下
*
* @param client FTP客户端
* @param localfile 本地文件
* @param remoteFolderPath FTP上传目录
*/
public static void upload(FTPClient client, File localfile, String remoteFolderPath) {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
try {
client.changeDirectory(remoteFolderPath);
if (!localfile.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "不存在!");
if (!localfile.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + localfile.getPath() + "是一个文件夹!");
if (listener != null)
client.upload(localfile, listener);
else
client.upload(localfile);
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* FTP上传本地文件到FTP的一个目录下
*
* @param client FTP客户端
* @param localfilepath 本地文件路径
* @param remoteFolderPath FTP上传目录
*/
public static void upload(FTPClient client, String localfilepath, String remoteFolderPath) {
File localfile = new File(localfilepath);
upload(client, localfile, remoteFolderPath);
}
/**
* 批量上传本地文件到FTP指定目录上
*
* @param client FTP客户端
* @param localFilePaths 本地文件路径列表
* @param remoteFolderPath FTP上传目录
*/
public static void uploadListPath(FTPClient client, List<String> localFilePaths, String remoteFolderPath) {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
try {
client.changeDirectory(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
for (String path : localFilePaths) {
File file = new File(path);
if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "不存在!");
if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + path + "是一个文件夹!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* 批量上传本地文件到FTP指定目录上
*
* @param client FTP客户端
* @param localFiles 本地文件列表
* @param remoteFolderPath FTP上传目录
*/
public static void uploadListFile(FTPClient client, List<File> localFiles, String remoteFolderPath) {
try {
client.changeDirectory(remoteFolderPath);
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
MyFtpListener listener = MyFtpListener.instance(FTPOptType.UP);
for (File file : localFiles) {
if (!file.exists()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "不存在!");
if (!file.isFile()) throw new FTPRuntimeException("所要上传的FTP文件" + file.getPath() + "是一个文件夹!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new FTPRuntimeException(e);
}
}
/**
* 判断一个FTP路径是否存在,如果存在返回类型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、FTPFile.TYPE_LINK=2)
* 如果文件不存在,则返回一个-1
*
* @param client FTP客户端
* @param remotePath FTP文件或文件夹路径
* @return 存在时候返回类型值(文件0,文件夹1,连接2),不存在则返回-1
*/
public static int isExist(FTPClient client, String remotePath) {
remotePath = PathToolkit.formatPath4FTP(remotePath);
int x = -1;
FTPFile[] list = null;
try {
list = client.list(remotePath);
} catch (Exception e) {
return -1;
}
if (list.length > 1) return FTPFile.TYPE_DIRECTORY;
else if (list.length == 1) {
FTPFile f = list[0];
if (f.getType() == FTPFile.TYPE_DIRECTORY) return FTPFile.TYPE_DIRECTORY;
//假设推理判断
String _path = remotePath + "/" + f.getName();
try {
int y = client.list(_path).length;
if (y == 1) return FTPFile.TYPE_DIRECTORY;
else return FTPFile.TYPE_FILE;
} catch (Exception e) {
return FTPFile.TYPE_FILE;
}
} else {
try {
client.changeDirectory(remotePath);
return FTPFile.TYPE_DIRECTORY;
} catch (Exception e) {
return -1;
}
}
}
/**
* 关闭FTP连接,关闭时候像服务器发送一条关闭命令
*
* @param client FTP客户端
* @return 关闭成功,或者链接已断开,或者链接为null时候返回true,通过两次关闭都失败时候返回false
*/
public static boolean closeConnection(FTPClient client) {
if (client == null) return true;
if (client.isConnected()) {
try {
client.disconnect(true);
return true;
} catch (Exception e) {
try {
client.disconnect(false);
} catch (Exception e1) {
e1.printStackTrace();
return false;
}
}
}
return true;
}
}
package lavasoft.common;
import java.io.File;
/**
* 路径处理工具,操作系统自适应
*
* @author leizhimin 2009-11-30 16:01:34
*/
public final class PathToolkit {
private PathToolkit() {
}
/**
* 格式化文件路径,将其中不规范的分隔转换为标准的分隔符,并且去掉末尾的文件路径分隔符。
* 本方法操作系统自适应
*
* @param path 文件路径
* @return 格式化后的文件路径
*/
public static String formatPath4File(String path) {
String reg0 = "\\\\+";
String reg = "\\\\+|/+";
String temp = path.trim().replaceAll(reg0, "/");
temp = temp.replaceAll(reg, "/");
if (temp.length() > 1 && temp.endsWith("/")) {
temp = temp.substring(0, temp.length() - 1);
}
temp = temp.replace('/', File.separatorChar);
return temp;
}
/**
* 格式化文件路径,将其中不规范的分隔转换为标准的分隔符
* 并且去掉末尾的"/"符号(适用于FTP远程文件路径或者Web资源的相对路径)。
*
* @param path 文件路径
* @return 格式化后的文件路径
*/
public static String formatPath4FTP(String path) {
String reg0 = "\\\\+";
String reg = "\\\\+|/+";
String temp = path.trim().replaceAll(reg0, "/");
temp = temp.replaceAll(reg, "/");
if (temp.length() > 1 && temp.endsWith("/")) {
temp = temp.substring(0, temp.length() - 1);
}
return temp;
}
/**
* 获取FTP路径的父路径,但不对路径有效性做检查
*
* @param path FTP路径
* @return 父路径,如果没有父路径,则返回null
*/
public static String genParentPath4FTP(String path) {
String pp = new File(path).getParent();
if (pp == null) return null;
else return formatPath4FTP(pp);
}
}
package lavasoft.common.ftp;
/**
* FTP操作类型
*
* @author leizhimin 2009-11-30 11:16:59
*/
public enum FTPOptType {
UP("上传"),
DOWN("下载"),
LIST("浏览"),
DELFILE("删除文件"),
DELFOD("删除文件夹"),
RENAME("上传");
private String optname;
FTPOptType(String optname) {
this.optname = optname;
}
public String getOptname() {
return optname;
}
}
package lavasoft.common.ftp;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
/**
* FTP监听器,做了简单实现,可以使用commons logger替换System.out.println
*
* @author leizhimin 2009-11-30 11:05:33
*/
public class MyFtpListener implements FTPDataTransferListener {
private FTPOptType optType;
public static MyFtpListener instance(FTPOptType optType) {
return new MyFtpListener(optType);
}
private MyFtpListener(FTPOptType optType) {
this.optType = optType;
}
public void started() {
System.out.println(optType.getOptname() + ":FTP启动喽。。。。。。");
}
public void transferred(int length) {
System.out.println(optType.getOptname() + ":FTP传输喽。。。。。。");
}
public void completed() {
System.out.println(optType.getOptname() + ":FTP完成喽。。。。。。");
}
public void aborted() {
System.out.println(optType.getOptname() + ":FTP中止喽。。。。。。");
}
public void failed() {
System.out.println(optType.getOptname() + ":FTP挂掉喽。。。。。。");
}
}
package lavasoft.common.ftp;
/**
* FTP异常
*
* @author leizhimin 2009-11-30 10:28:03
*/
public class FTPRuntimeException extends RuntimeException {
public FTPRuntimeException() {
super();
}
public FTPRuntimeException(String message) {
super(message);
}
public FTPRuntimeException(String message, Throwable cause) {
super(message, cause);
}
public FTPRuntimeException(Throwable cause) {
super(cause);
}
}
package lavasoft;
import it.sauronsoftware.ftp4j.FTPClient;
import lavasoft.common.ftp.FTPToolkit;
/**
* 简单测试下
*
* @author leizhimin 2009-11-30 12:25:42
*/
public class Test {
public static void main(String args[]) throws Exception {
String ftpip = "192.168.104.113";
int ftpport = 21;
String ftpuser = "vcomkp.ftpadmin";
String ftppswd = "ftp";
FTPClient client = FTPToolkit.makeFtpConnection(ftpip, ftpport, ftpuser, ftppswd);
FTPToolkit.upload(client, "C:\\Dynamicclrr4.zip", "/aaa/bbb/ccc");
FTPToolkit.download(client, "/Dynamicclrr4.zip", "D:\\");
FTPToolkit.closeConnection(client);
}
}
上传:FTP启动喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP完成喽。。。。。。
上传:FTP启动喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP传输喽。。。。。。
上传:FTP完成喽。。。。。。
Process finished with exit code 0
本程序在RedHat AS 5上使用vsftpd测试通过,一般来说windows下测试通过的ftp工具,一般在Linux下通不过,因为Windows没有太多权限控制。而Linux控制非常严格,常常因为没有权限而导致操作失败,这时候,要对Linux的FTP用户给读写的权限才能正确执行以上代码。
另外说明一点,在判断ftp服务器上一个文件是否存在的isExist方法,具有一定一个局限性,体现在两方面:一是对特定组件版本的依赖性,不同版本list等方法实现不同,对错误路径的处理也不同。二是,不同的ftp服务器对发送list命令反馈的结果不尽相同。
这也是为什么很多ftp组件工具不去实现isExist的方法。对使用本方法测试有异常不符合事实的情况下,需要根据特定的环境测试重新实现isExist方法即可。
这里我给出了一个测试:
我用的vsftp目录结构如下
/aaa
/aaa/bbb/ccc
/aaa/bbb/ccc/Dynamicclrr4.zip
调用方法测试:
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc")); //目录
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc/Dynamicclrr4.zip")); //文件
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/ccc/xxxxxx.zip")); //不存在的文件
System.out.println(FTPToolkit.isExist(client, "/aaa/bbb/pppppdd")); //不存在的目录
1
0
-1
-1
Process finished with exit code 0
可见,打印的结果与实际情况相符。
发表评论
-
java在cmd下编译和执行引用jar的类
2013-08-08 16:27 109211、将上面的ojdbc14.jar文件,与调用程序复制到系统D ... -
Failed to load the JNI shared library jvm.dll
2012-09-24 09:48 1325jdk和eclipse必须同为32位或者同为64位,问题解决。 ... -
快速建站:免费的18个开源Java CMS
2012-09-03 10:18 1975快速建站:免费的18个开 ... -
正则表达式测试工具
2012-08-27 16:36 1039这是我自己通过swing制作的正则表达式测试工具,exe文件也 ... -
exe4j制作exe文件
2012-08-27 16:30 2135exe4j的使用方法: 首先 ... -
Map的keySet方法序列化BUG问题及其解决方法
2012-08-20 11:53 4367Map的keySet在进行序列化时,报错:java.io.No ... -
Java日期转换SimpleDateFormat格式大全
2012-08-07 14:02 218524小时制 Date date = new Date(); ... -
java资源加载解析
2012-07-06 22:25 0java开发中往往要加载各种各样的资源文件,如property ... -
JRE环境查看代码
2012-07-05 09:43 1219在java开发过程中,有时候会遇到查看我当前项目的jre版本、 ... -
JNI技术实战
2012-07-05 10:40 1241JNI是Java Native Interface的缩写,中文 ... -
java文件选择对话框,文件名不可编辑
2012-06-29 15:32 3645思想:获取文件选择对话框中“文件名”显示栏的组件,设置组件不可 ... -
Java通过new Date() 得到的时间与系统时间差N个小时的解决办法
2012-06-20 10:17 1641一般情况下不会出现如题所示的问题,既然出现了我们就要想办法解决 ... -
jdk环境变量配置
2012-01-16 17:09 1366使用JDK一共需要配置三 ... -
Java.lang.StackOverflowError
2011-12-09 18:29 1310Class A{ A a=new A(); } 为什么 ... -
java设置文件属性(隐藏,只读...)
2011-12-09 13:54 18771. 当Java.io中,如果文件的操作的时候,判断 ... -
大整数相乘
2011-10-13 15:21 1266一、乘数和被乘数为long类型的 public class B ... -
java 大文件以二进制保存到数据库
2011-10-12 14:25 11852一、创建表 oracle: create table bao ...
相关推荐
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
ftp4j是一个FTP客户端Java类库,实现了FTP客户端应具有的大部分功能文件(包括上传和下 载),浏览远程FTP服务器上的目录和文件,创建、删除、重命,移动远程目录和文件。ftp4j提供多种方式连接到远程FTP服务器包括...
一、PORT(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。 当需要传送数据时,客户端在命令链路上用 PORT命令告诉服务器:“我打开了****端口,...
该工具应该可以帮助您学习和理解 ,因此它不会隐藏任何细节,但是使它们更易于掌握。 这是一个全基于的项目 具有服务器端支持的基于的服务器 + ++ +基于HTML客户端,具有超级简单的无转换器设置 待办事项:OCPP...
基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...
基于JAVA的UDP服务器模型源代码,内含UDP服务器端模型和UDP客户端模型两个小程序,向JAVA初学者演示UDP C/S结构的原理。 简单聊天软件CS模式 2个目标文件 一个简单的CS模式的聊天软件,用socket实现,比较简单。 ...
5. 客户端运行工具:目前的系统采用浏览器作为客户端,为了支持Ajax开发框架,应该选择使用IE 5以上版本浏览器。 本网站以xp为Web平台,JSP+Ajax+Servlet+JavaBean+Hibernate为网站实现技术,建立基于MySQL数据库...
·外挂工具功能,最多可以外挂20个外部程序,如Oicq、Ftp、金山快译等。 ·高效的网页搜索器,将大量国内外搜索引擎分门别类集成到一起,这样只要输入一次搜索关键字,就可以使用多个搜索引擎进行同时搜索,...