使用FTP4J上传下载删除文件及文件夹
2013-12-30 12:53
387 查看
1. 依赖库
ftp4j-1.5.jar
2. 共用方法
上传下载方法需要引用的方法:
public static URL newURL(URL parentUrl, String child)throws MalformedURLException {
String path = parentUrl.getPath();
if(!path.endsWith("/")){
path += "/";
}
path += child;
return new URL(parentUrl, path);
}
3. 下载文件
public static void download(URL url, String username, String password, File file){
download(url, username, password, "UTF-8", file);
}
public static void download(URL url, String username, String password, String encoding, File file){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
// client.changeDirectory(url.getPath());
client.download(url.getFile(), file);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
4. 下载文件夹
public static void downloadFolder(URL url, String username, String password, File folder){
downloadFolder(url, username, password, "UTF-8", folder);
}
public static void downloadFolder(URL url, String username, String password, String encoding, File folder){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
downloadFolder(client, url, folder);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
private static void downloadFolder(FTPClient client, URL url, File folder)throws Exception{
client.changeDirectory(url.getPath());
FTPFile[] ftpFiles = client.list();
if(!folder.exists()){
folder.mkdirs();
}
for(FTPFile ftpFile : ftpFiles){
String name = ftpFile.getName();
if(ftpFile.getType() == FTPFile.TYPE_FILE){
client.changeDirectory(url.getPath());
File f = new File(folder, name);
client.download(name, f);
}else if(ftpFile.getType() == FTPFile.TYPE_DIRECTORY){
downloadFolder(client, newURL(url, name), new File(folder, name));
}
}
}
5. 上传文件
public static void upload(URL url, String username, String password, File file){
upload(url, username, password, "UTF-8", file);
}
public static void upload(URL url, String username, String password, String encoding, File file){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.changeDirectory(url.getPath());
client.upload(file);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
6. 上传文件夹
public static void uploadFolder(URL url, String username, String password, File folder){
uploadFolder(url, username, password, "UTF-8", folder);
}
public static void uploadFolder(URL url, String username, String password, String encoding, File folder){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.changeDirectory(url.getPath());
String[] files = folder.list();
for(String file : files){
File f = new File(folder, file);
if(f.isDirectory()){
uploadFolder(client, url, f);
}else{
client.changeDirectory(url.getPath());
client.upload(f);
}
}
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
private static void uploadFolder(FTPClient client, URL parentUrl, File folder)throws Exception{
client.changeDirectory(parentUrl.getPath());
if(!existsFtpDir(client, folder.getName())){
client.createDirectory(folder.getName());
}
client.changeDirectory(folder.getName());
String[] fileList = folder.list();
if(fileList == null){
return ;
}
for(String file : fileList){
File f = new File(folder, file);
if(f.isDirectory()){
uploadFolder(client, newURL(parentUrl, folder.getName()), f);
}else if(f.isFile()){
client.changeDirectory(parentUrl.getPath());
client.changeDirectory(folder.getName());
client.upload(f);
}
}
}
private static boolean existsFtpDir(FTPClient client, String dir)throws Exception{
FTPFile[] ftpFiles = client.list();
for(FTPFile ftpFile : ftpFiles){
if((ftpFile.getType() == FTPFile.TYPE_DIRECTORY) && (dir.equals(ftpFile.getName()))){
return true;
}
}
return false;
}
7. 判断是否存在文件或文件夹
public static boolean exists(URL url, String username, String password){
return exists(url, username, password, "UTF-8");
}
public static boolean exists(URL url, String username, String password, String encoding){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
String[] welcome = client.connect(url.getHost(), port);
logger.info("welcome = " + welcome.length);
//登录验证
client.login(username, password);
String path = url.getPath();
logger.info("path = " + path);
if(path.indexOf(".") != -1){//文件
int index = path.lastIndexOf("/");
String p = path.substring(0, index);
String f = path.substring(index + 1);
logger.info("p = " + p + ", f = " + f);
client.changeDirectory(p);
FTPFile[] ftpFiles = client.list();
for(FTPFile ftpFile : ftpFiles){
if((ftpFile.getType() == FTPFile.TYPE_FILE) && (ftpFile.getName().equals(f))){
return true;
}
}
return false;
}else{//文件夹
client.changeDirectory(path);
}
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
return false;
}
return true;
}
8. FTP服务器之间复制文件夹
public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, String sourceEncoding, URL destUrl, String destUsername, String destPassword, String destEncoding, String tmpDir)throws IOException{
downloadFolder(sourceUrl, sourceUsername, sourcePassword, sourceEncoding, new File(tmpDir));
uploadFolder(destUrl, destUsername, destPassword, destEncoding, new File(tmpDir));
FileUtil.deleteFolder(tmpDir);
}
public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, URL destUrl, String destUsername, String destPassword, String tmpDir)throws IOException{
copyFolder(sourceUrl, sourceUsername, sourcePassword, "UTF-8", destUrl, destUsername, destPassword, "UTF-8", tmpDir);
}
9.删除文件
public static void delete(URL url, String username, String password){
delete(url, username, password, "UTF-8");
}
public static void delete(URL url, String username, String password, String encoding){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.deleteFile(url.getPath());
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
10. 简单用法
uploadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/test"));
downloadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/dbtest"));
ftp4j-1.5.jar
2. 共用方法
上传下载方法需要引用的方法:
public static URL newURL(URL parentUrl, String child)throws MalformedURLException {
String path = parentUrl.getPath();
if(!path.endsWith("/")){
path += "/";
}
path += child;
return new URL(parentUrl, path);
}
3. 下载文件
public static void download(URL url, String username, String password, File file){
download(url, username, password, "UTF-8", file);
}
public static void download(URL url, String username, String password, String encoding, File file){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
// client.changeDirectory(url.getPath());
client.download(url.getFile(), file);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
4. 下载文件夹
public static void downloadFolder(URL url, String username, String password, File folder){
downloadFolder(url, username, password, "UTF-8", folder);
}
public static void downloadFolder(URL url, String username, String password, String encoding, File folder){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
downloadFolder(client, url, folder);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
private static void downloadFolder(FTPClient client, URL url, File folder)throws Exception{
client.changeDirectory(url.getPath());
FTPFile[] ftpFiles = client.list();
if(!folder.exists()){
folder.mkdirs();
}
for(FTPFile ftpFile : ftpFiles){
String name = ftpFile.getName();
if(ftpFile.getType() == FTPFile.TYPE_FILE){
client.changeDirectory(url.getPath());
File f = new File(folder, name);
client.download(name, f);
}else if(ftpFile.getType() == FTPFile.TYPE_DIRECTORY){
downloadFolder(client, newURL(url, name), new File(folder, name));
}
}
}
5. 上传文件
public static void upload(URL url, String username, String password, File file){
upload(url, username, password, "UTF-8", file);
}
public static void upload(URL url, String username, String password, String encoding, File file){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.changeDirectory(url.getPath());
client.upload(file);
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
6. 上传文件夹
public static void uploadFolder(URL url, String username, String password, File folder){
uploadFolder(url, username, password, "UTF-8", folder);
}
public static void uploadFolder(URL url, String username, String password, String encoding, File folder){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.changeDirectory(url.getPath());
String[] files = folder.list();
for(String file : files){
File f = new File(folder, file);
if(f.isDirectory()){
uploadFolder(client, url, f);
}else{
client.changeDirectory(url.getPath());
client.upload(f);
}
}
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
private static void uploadFolder(FTPClient client, URL parentUrl, File folder)throws Exception{
client.changeDirectory(parentUrl.getPath());
if(!existsFtpDir(client, folder.getName())){
client.createDirectory(folder.getName());
}
client.changeDirectory(folder.getName());
String[] fileList = folder.list();
if(fileList == null){
return ;
}
for(String file : fileList){
File f = new File(folder, file);
if(f.isDirectory()){
uploadFolder(client, newURL(parentUrl, folder.getName()), f);
}else if(f.isFile()){
client.changeDirectory(parentUrl.getPath());
client.changeDirectory(folder.getName());
client.upload(f);
}
}
}
private static boolean existsFtpDir(FTPClient client, String dir)throws Exception{
FTPFile[] ftpFiles = client.list();
for(FTPFile ftpFile : ftpFiles){
if((ftpFile.getType() == FTPFile.TYPE_DIRECTORY) && (dir.equals(ftpFile.getName()))){
return true;
}
}
return false;
}
7. 判断是否存在文件或文件夹
public static boolean exists(URL url, String username, String password){
return exists(url, username, password, "UTF-8");
}
public static boolean exists(URL url, String username, String password, String encoding){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
String[] welcome = client.connect(url.getHost(), port);
logger.info("welcome = " + welcome.length);
//登录验证
client.login(username, password);
String path = url.getPath();
logger.info("path = " + path);
if(path.indexOf(".") != -1){//文件
int index = path.lastIndexOf("/");
String p = path.substring(0, index);
String f = path.substring(index + 1);
logger.info("p = " + p + ", f = " + f);
client.changeDirectory(p);
FTPFile[] ftpFiles = client.list();
for(FTPFile ftpFile : ftpFiles){
if((ftpFile.getType() == FTPFile.TYPE_FILE) && (ftpFile.getName().equals(f))){
return true;
}
}
return false;
}else{//文件夹
client.changeDirectory(path);
}
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
return false;
}
return true;
}
8. FTP服务器之间复制文件夹
public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, String sourceEncoding, URL destUrl, String destUsername, String destPassword, String destEncoding, String tmpDir)throws IOException{
downloadFolder(sourceUrl, sourceUsername, sourcePassword, sourceEncoding, new File(tmpDir));
uploadFolder(destUrl, destUsername, destPassword, destEncoding, new File(tmpDir));
FileUtil.deleteFolder(tmpDir);
}
public static void copyFolder(URL sourceUrl, String sourceUsername, String sourcePassword, URL destUrl, String destUsername, String destPassword, String tmpDir)throws IOException{
copyFolder(sourceUrl, sourceUsername, sourcePassword, "UTF-8", destUrl, destUsername, destPassword, "UTF-8", tmpDir);
}
9.删除文件
public static void delete(URL url, String username, String password){
delete(url, username, password, "UTF-8");
}
public static void delete(URL url, String username, String password, String encoding){
FTPClient client = new FTPClient();
client.setCharset(encoding);
client.setType(FTPClient.TYPE_BINARY);
//连接到指定的FTP服务器(域名或IP) 不指定端口,则使用默认端口21
try {
int port = url.getPort();
if(port < 1){
port = 21;
}
client.connect(url.getHost(), port);
//登录验证
client.login(username, password);
client.deleteFile(url.getPath());
client.logout();
//安全退出
client.disconnect(true);
} catch (Exception e) {
logger.warn(e.getMessage(), e);
}
}
10. 简单用法
uploadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/test"));
downloadFolder(new URL("ftp://localhost:21/test/"), "admin", "123", new File("d:/dbtest"));
相关文章推荐
- java实现对hdfs文件系统的上传,下载,删除,创建文件夹的操作演示
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
- C#实现多文件上传,写到文件夹中,获取文件信息以及下载文件和删除文件
- 实现 ftp 文件/文件夹的上传下载以及删除
- Java 使用ftp的方式上传文件到linux,并实现删除,下载
- FTP文件上传(自定义文件夹)、删除、下载、读取
- [sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表
- 使用java操作FastDFS上传,下载,删除文件
- android下使用FTP上传文件、下载文件、新建目录、删除文件工具类
- 如何在Linux中使用sFTP上传或下载文件与文件夹
- asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类
- 使用ganymed-ssh2-build.jar来操作linux服务器(上传、下载文件,远程创建目录,远程删除文件等等)
- apache FtpClient上传下载删除文件夹及文件
- FTPClient的使用(二) ftp(文件夹和文件)下载,上传,复制备份
- git(3)----git常用上传、下载、删除文件(或文件夹)命令
- Spring MVC 中文件上传/删除 删除空文件夹方法 + 下载方法
- 使用FtpWebRequest 类操作(上传、下载和删除)FTP上的XML文件
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- 使用java操作FastDFS上传,下载,删除文件