/*
* 上传目录
* client:FTP客户端对象
* parentUrl:父节点URL
* file:目录
* del:
*/
private void uploadFolder(FTPClient client, URL parentUrl, File file,
boolean del) throws Exception {
client.changeDirectory(parentUrl.getPath());
String dir = file.getName(); // 当前目录名称
URL url = getURL(parentUrl, dir);
if (!exists(client, url.getPath())) { // 判断当前目录是否存在
client.createDirectory(dir); // 创建目录
}
client.changeDirectory(dir);
File[] files = file.listFiles(); // 获取当前文件夹所有文件及目录
for (int i = 0; i < files.length; i++) {
file = files[i];
if (file.isDirectory()) { // 如果是目录,则递归上传
uploadFolder(client, url, file, del);
} else { // 如果是文件,直接上传
client.changeDirectory(url.getPath());
client.upload(file);
if (del) { // 删除源文件
file.delete();
}
}
}
}
/*
* 删除目录
* client:FTP客户端对象
* url:FTP URL
*/
private void deleteFolder(FTPClient client, URL url) throws Exception {
String path = url.getPath();
client.changeDirectory(path);
FTPFile[] files = client.list();
String name = null;
for (FTPFile file : files) {
name = file.getName();
// 排除隐藏目录
if (".".equals(name) || "..".equals(name)) {
continue;
}
if (file.getType() == FTPFile.TYPE_DIRECTORY) { // 递归删除子目录
deleteFolder(client, getURL(url, file.getName()));
} else if (file.getType() == FTPFile.TYPE_FILE) { // 删除文件
client.deleteFile(file.getName());
}
}
client.changeDirectoryUp();// 反回上一级目录
client.deleteDirectory(url.getPath()); // 删除当前目录
}
/**
* 下载文件夹
*
* @param client
* FTP客户端对象
* @param url
* FTP URL
* @param localDir
* 本地存储目录
* @throws Exception
*/
private void downloadFolder(FTPClient client, URL url, String localDir)
throws Exception {
String path = url.getPath();
client.changeDirectory(path);
// 在本地创建当前下载的文件夹
File folder = new File(localDir + "/" + new File(path).getName());
if (!folder.exists()) {
folder.mkdirs();
}
localDir = folder.getAbsolutePath();
FTPFile[] files = client.list();
String name = null;
for (FTPFile file : files) {
name = file.getName();
// 排除隐藏目录
if (".".equals(name) || "..".equals(name)) {
continue;
}
if (file.getType() == FTPFile.TYPE_DIRECTORY) { // 递归下载子目录
downloadFolder(client, getURL(url, file.getName()), localDir);
} else if (file.getType() == FTPFile.TYPE_FILE) { // 下载文件
client.download(name, new File(localDir + "/" + name));
}
}
client.changeDirectoryUp();
}
/**
* 上传文件或目录
* @param dir目标文件
* @param del是否删除源文件,默认为false
* @param files文件或目录对象数组
* @param del:是否删除源文件,true删除,false不删除
* @throws Exception
*/
public void upload(String dir, boolean del, File... files) throws Exception {
if (StringUtils.isEmpty(dir) || StringUtils.isEmpty(files)) {
return;
}
FTPClient client = null;
try {
client = getClient();
mkdirs(client, dir); // 创建文件
for (File file : files) {
if (file.isDirectory()) { // 上传目录
uploadFolder(client, getURL(dir), file, del);
} else {
client.upload(file); // 上传文件
if (del) { // 为true删除源文件
file.delete();
}
}
}
} finally {
logout(client);
}
}
/**
* 上传文件或目录
* @param dir目标文件
* @param files文件或目录对象数组
* @throws Exception
*/
public void upload(String dir, File... files) throws Exception {
upload(dir, false, files);
}
/**
* 删除文件或目录
* @param dirs
* @throws Exception
*/
public void delete(String... dirs) throws Exception {
if (StringUtils.isEmpty(dirs)) {
return;
}
FTPClient client = null;
try {
client = getClient();
int type = -1;
for (String dir : dirs) {
client.changeDirectory("/"); // 切换至根目录
type = getFileType(client, dir); // 获取当前类型
if (type == 0) { // 删除文件
client.deleteFile(dir);
} else if (type == 1) { // 删除目录
deleteFolder(client, getURL(dir));
}
}
} finally {
logout(client);
}
}
/**
* 下载文件或目录
*
* @param localDir
* 本地存储目录
* @param dirs
* 文件或者目录
* @throws Exception
*/
public void download(String localDir, String... dirs) throws Exception {
if (StringUtils.isEmpty(dirs)) {
return;
}
FTPClient client = null;
try {
client = getClient();
File folder = new File(localDir);
if (!folder.exists()) { // 如果本地文件夹不存在,则创建
folder.mkdirs();
}
int type = -1;
String localPath = null;
for (String dir : dirs) {
client.changeDirectory("/"); // 切换至根目录
type = getFileType(client, dir); // 获取当前类型
if (type == 0) { // 文件
localPath = localDir + "/" + new File(dir).getName();
client.download(dir, new File(localPath));
} else if (type == 1) { // 目录
downloadFolder(client, getURL(dir), localDir);
}
}
} finally {
logout(client);
}
}
FTP-使用ftp4j操作FTP_2
时间: 2024-08-06 12:34:58
FTP-使用ftp4j操作FTP_2的相关文章
cmd 下登陆ftp及相关操作
cmd 下登陆ftp及相关操作 2011-08-09 20:34:28| 分类: 小技巧|字号 订阅 一.举例 假设FTP地址为“ 61.129.83.39”(大家试验的时候不要以这个FTP去试,应该可能密码要改掉.) 1:“开始”-“运行”-输入“FTP”进去cmd界面 2.open 61.129.83.39 如果你的FTP服务器不是用的21默认端口,假如端口是9900,那么此步的命令应在后面空格加9900,即为 open 61.129.83.39 9900 3:它会提示输入用户
小蚂蚁学cURL笔记(3)——cURL对Ftp服务器的操作
对ftp服务器的下载操作方法. 内容很简单,就是几个设置,直接上代码. //初始化 $curlobj=curl_init(); //文件的准确路径url curl_setopt($curlobj,CURLOPT_URL,"http://......"); //不输出head头文件 curl_setopt($curlobj,CURLOPT_HEADER,0); //执行后不打印 curl_setopt($curlobj,CURLOPT_RETURNTRANSFER,1); //重点来了
FTP-使用ftp4j操作FTP_1
说明:jar包版本ftp4j-1.7.2.jar 一.创建项目 项目名称:ftpdemo二.添加jar包 1.在项目中创建lib目录 /lib 2.在lib目录下添加jar包 ftp4j-1.7.2.jar junit-4.4.jar 三.添加属性文件 1.在项目中创建conf目录 /conf 2.在conf目录添加属性文件 属性文件名称:ftpinfo.properties
提供一个对FTP文件各种操作的类
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; namespace BaseInfo { public enum Ftpud { up = 0, down = 1, }; /// <summary> /// /// </summary> public class ftpOperater { private string ftpS
org.apache.commons.net工具下 对ftp的简单操作
/** * 连接ftp服务器 * @param ip IP地址 * @param port 端口号 * @param username 用户名 * @param password 密码 * @param ftpPath ftp子目录 * @throws IOException */ public int connectServer(String ip, int port, String username, String password, String ftpPath) throws IOExc
FTP创建于操作
1,FTP服务创建于配置http://jingyan.baidu.com/article/0a52e3f4230067bf63ed7268.html, 2,FTP操作类 using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Win
FTP服务器常规操作
导读 FTP协议是Internet文件传输的基础,它是由一系列规格说明文档组成,目标是提高文件的共享性,提供非直接使用远程计算机,使存储介质对用户透明和可靠高效地传送数据.下面就由我给大家简单介绍一下FTP服务器的常规操作. 软件包安装 Red Hat Linux自带VSFTP服务器软件,不需要另行安装.如果在安装系统时没有安装VSFTP服务器,可以用rpm安装,也可以利用tar安装.新版的软件包可以到VSFTP的网站http://vsftp.beasts.org/下载.例如安装vsftp-2.
ftp服务通信操作
1.将本地虚拟机网卡设置ip--->2.将虚拟机系统的网卡ip设置--->3.虚拟机设置特定网络模式vm8nat模式: (1) (2) (3) 保证正常互ping 通信, 4.在虚拟机系统中打开vsftp server服务------>见博客站内(FTP服务器--vsftpd的安装和配置) 5.在虚拟机系统中打开ssh server服务------>见博客站内(如何开启SSH SERVER服务) 总结: 即可登录ftp服务,telnet 192.168.2.2 22 (注:保证
ftp 命令行操作 常用命令
> ftp <host> [port] > pwd # 查看当前目录 > dir # 查看FTP服务器中的文件及目录 > mkdir <dirname> > cd <dirname> > bin # 采用二进制传输,会加快上传和下载速度 > lcd <local dirname> > !dir # 查看本地文件夹的文件和目录 > put <filename> # 上传文件, 文件要在上面设