- 浏览: 432185 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (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 110461、将上面的ojdbc14.jar文件,与调用程序复制到系统D ... -
Failed to load the JNI shared library jvm.dll
2012-09-24 09:48 1449jdk和eclipse必须同为32位或者同为64位,问题解决。 ... -
快速建站:免费的18个开源Java CMS
2012-09-03 10:18 2052快速建站:免费的18个开 ... -
正则表达式测试工具
2012-08-27 16:36 1158这是我自己通过swing制作的正则表达式测试工具,exe文件也 ... -
exe4j制作exe文件
2012-08-27 16:30 2190exe4j的使用方法: 首先 ... -
Map的keySet方法序列化BUG问题及其解决方法
2012-08-20 11:53 4423Map的keySet在进行序列化时,报错:java.io.No ... -
Java日期转换SimpleDateFormat格式大全
2012-08-07 14:02 228324小时制 Date date = new Date(); ... -
java资源加载解析
2012-07-06 22:25 0java开发中往往要加载各种各样的资源文件,如property ... -
JRE环境查看代码
2012-07-05 09:43 1310在java开发过程中,有时候会遇到查看我当前项目的jre版本、 ... -
JNI技术实战
2012-07-05 10:40 1297JNI是Java Native Interface的缩写,中文 ... -
java文件选择对话框,文件名不可编辑
2012-06-29 15:32 3719思想:获取文件选择对话框中“文件名”显示栏的组件,设置组件不可 ... -
Java通过new Date() 得到的时间与系统时间差N个小时的解决办法
2012-06-20 10:17 1711一般情况下不会出现如题所示的问题,既然出现了我们就要想办法解决 ... -
jdk环境变量配置
2012-01-16 17:09 1434使用JDK一共需要配置三 ... -
Java.lang.StackOverflowError
2011-12-09 18:29 1368Class A{ A a=new A(); } 为什么 ... -
java设置文件属性(隐藏,只读...)
2011-12-09 13:54 19411. 当Java.io中,如果文件的操作的时候,判断 ... -
大整数相乘
2011-10-13 15:21 1320一、乘数和被乘数为long类型的 public class B ... -
java 大文件以二进制保存到数据库
2011-10-12 14:25 11935一、创建表 oracle: create table bao ...
相关推荐
hhhhh安卓开发教程大全
avem-labs_Avem_1740990015.zip
微信群机器人管理系统源码 微信群机器人管理系统源码 支持同登陆多个微信 源码类型: C/S 开发环境: VS2010 SQL2008R2 菜单功能 1、支持同时登录多个微信 2、支持机器人聊天(笑话,成语接龙、故事会、智力等等) 3、支持签到 4、可自定义回复 5、可自定义红包语 6、支持定期发送公告(如群规,广告)等 1、WeChatRobots后台配置web版 2、数据库在WeiChartGroup.Net/app_data中,附加即可
https://upload.csdn.net/creation/uploadResources?spm=1003.2018.3001.4314
名字微控制器_STM32_课程_DeepBlue_1740989720.zip
S7-200Smart恒压供水程序示例与485通讯实践:操作指南与案例解析,S7-200 Smart可编程控制器恒压供水程序设计与实现,附带485通讯范例,S7-200Smart 恒压供水程序样例+485通讯样例 ,S7-200Smart; 恒压供水程序样例; 485通讯样例,S7-200Smart程序样例:恒压供水及485通讯应用示例
Java使用JNA、JNI两种不同方式调用DLL、SO动态库方式读写M1卡源码,支持读写M1卡扇区数据、修改IC卡扇区密钥、改写UID卡卡号等功能,支持Windows系统,同时支持龙芯Mips、LoongArch、海思麒麟鲲鹏飞腾Arm、海光兆芯x86_Amd64等架构平台的国产统信、麒麟等Linux系统,内有jna-4.5.0.jar包,vx13822155058 qq954486673
UDP协议接收和发送数据示例JAVA
本文介绍了范德堡大学深脑刺激器(DBS)项目,该项目旨在开发和临床评估一个系统,以辅助从规划到编程的整个过程。DBS是一种高频刺激治疗,用于治疗运动障碍,如帕金森病。由于目标区域在现有成像技术中可见性差,因此DBS电极的植入和编程过程复杂且耗时。项目涉及使用计算机辅助手术技术,以及一个定制的微定位平台(StarFix),该平台允许在术前进行图像采集和目标规划,提高了手术的精确性和效率。此外,文章还讨论了系统架构和各个模块的功能,以及如何通过中央数据库和网络接口实现信息共享。
图像识别”项目源码资源(Python和C++)
虚拟同步电机与并电网模型的Simulink仿真参数配置与直接使用指南,虚拟同步电机与并电网模型的Simulink仿真:参数齐全,直接使用,同步电机simulink仿真 并电网模型仿真 参数设置好了,可直接使用 ,虚拟同步电机; simulink仿真; 并电网模型仿真; 参数设置; 使用,虚拟同步电机Simulink仿真与并电网模型参数化应用
三菱FX3U与力士乐VFC-x610变频器通讯案例详解:PLC控制下的变频器操作与设置程序,含接线方式及昆仑通态触摸屏操作指南,三菱FX3U与力士乐VFC-x610变频器通讯案例详解:接线、设置与程序注解,实现频率设定、启停控制与实时数据读取功能。,三菱FX3U与力士乐VFC-x610变频器通讯程序三菱FX3U与力士乐VFC-x610变频器通讯案例程序,有注释。 并附送程序,有接线方式,设置。 器件:三菱FX3U的PLC,力士乐VFCx610变频器,昆仑通态,威纶通触摸屏。 功能:实现频率设定,启停控制,实际频率读取等。 ,三菱FX3U;力士乐VFC-x610变频器;通讯程序;案例程序;注释;接线方式;设置;频率设定;启停控制;实际频率读取;昆仑通态;威纶通触摸屏。,三菱FX3U与力士乐VFC-x610变频器通讯程序及案例:频率控制与读取实践
xmselect测试用例~~~~~~~~~~~~~~
总共包含 32 款 AAA 级科幻武器。四种武器类型,每种有 8 种不同的纹理变化! 所有内容均采用 PBR 材质,可直接用于开发游戏!
python词云生成器,将txt文本自动分割生成词云图
智慧园区,作为现代城市发展的新形态,旨在通过高度集成的信息化系统,实现园区的智能化管理与服务。该方案提出,利用智能手环、定制APP、园区管理系统及物联网技术,将园区的各类设施与设备紧密相连,形成一个高效、便捷、安全的智能网络。从智慧社区到智慧酒店,从智慧景区到智慧康养,再到智慧生态,五大应用板块覆盖了园区的每一个角落,为居民、游客及工作人员提供了全方位、个性化的服务体验。例如,智能手环不仅能实现定位、支付、求助等功能,还能监测用户健康状况,让科技真正服务于生活。而智慧景区的建设,更是通过大数据分析、智能票务、电子围栏等先进技术,提升了游客的游玩体验,确保了景区的安全有序。 尤为值得一提的是,方案中的智慧康养服务,展现了科技对人文关怀的深刻体现。通过智慧手环与传感器,自动感知老人身体状态,及时通知家属或医疗机构,有效解决了“空巢老人”的照护难题。同时,智慧生态管理系统的应用,实现了对大气、水、植被等环境要素的实时监测与智能调控,为园区的绿色发展提供了有力保障。此外,方案还提出了建立全域旅游营销平台,整合区域旅游资源,推动旅游业与其他产业的深度融合,为区域经济的转型升级注入了新的活力。 总而言之,这份智慧园区建设方案以其前瞻性的理念、创新性的技术和人性化的服务设计,为我们展示了一个充满智慧与活力的未来园区图景。它不仅提升了园区的运营效率和服务质量,更让科技真正融入了人们的生活,带来了前所未有的便捷与舒适。对于正在规划或实施智慧园区建设的决策者而言,这份方案无疑提供了一份宝贵的参考与启示,激发了他们对于未来智慧生活的无限遐想与憧憬。
使用 SignalR 在 .NET Core 8 最小 API 中构建实时通知,构建实时应用程序已成为现代 Web 开发中必不可少的部分,尤其是对于通知、聊天系统和实时更新等功能。SignalR 是 ASP.NET 的一个强大库,可实现服务器端代码和客户端 Web 应用程序之间的无缝实时通信。 参考文章:https://blog.csdn.net/hefeng_aspnet/article/details/145990801
自适应网址导航网站发布页单页网页模板html源码,超级好看自适应清新网址导航网站发布页单页网页模板html源码!无论电脑还是手机,这是一个网页单页源码!! 模板无后台模板,无需数据库,上传服务器直接能用。
不用花钱,不拐弯抹角。
三菱FX3U PLC画圆程序详解:子程序循环插补技术绘制高精度圆及其他图形,三菱FX3U PLC画圆程序详解:以子程序循环调用实现高精度图形插补技术,三菱FX3U的plc画圆程序,程序将圆分为360等份进行插补,才用子程序循环调用的方式,根据这个原理可自行编写多种图形的程序 ,三菱FX3U; PLC画圆程序; 圆等份插补; 子程序循环调用; 图形程序编写,三菱FX3U PLC画圆子程序插补,多图形绘制可自定义调用循环编程法