package com.enterprisedt.net.ftp.mytest;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.util.debug.Level;
import com.enterprisedt.util.debug.Logger;
import com.enterprisedt.net.ftp.*;
import java.util.List;
import java.io.*;
import java.text.*;
/*
说明:
一、使用edtftpj 2.0.1开源包
二、实现了以下功能
1、上传指定文件夹(包括子文件和文件夹)
2、下载FTP上指定的文件夹
3、上传指定文件夹下的所有文件到FTP指定目录下
4、删除指定文件夹下的所有文件(包括子文件夹里面的文件,但由于不知怎么删除目录,所以暂没有做删除目录的功能)
5、删除指定文件
6、判断FTP上目录是否存在
7、上传单个文件
8、下载单个文件
*/
public class FtpEptUtil {
private String ftpServer;
private String ftpPort;
private String ftpUserName;
private String ftpPassword;
private FTPClient ftpClient;
private boolean isLogin = false;
public void connServer(String pFtpServer, String pFtpPort,
String pFtpUserName, String pFtpPassword) throws
Exception {
this.ftpServer = pFtpServer;
if (pFtpPort.trim().equals("")) {
this.ftpPort = "21";
} else {
this.ftpPort = pFtpPort;
}
if (pFtpUserName.trim().equals("")) {
this.ftpUserName = "Anonymous";
} else {
this.ftpUserName = pFtpUserName;
}
this.ftpPassword = pFtpPassword;
try {
ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
ftpClient.setRemoteHost(ftpServer);
ftpClient.setRemotePort(Integer.parseInt(ftpPort));
ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就要可以传中文文件名了
System.out.println("开始登录");
ftpClient.connect();
ftpClient.login(ftpUserName, ftpPassword);
System.out.println("登录成功");
ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
System.out.println("已转入根目录");
isLogin = true;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
//上传指定文件夹到ftp服务器上
public String uploadFolder(String folderName, String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
try {
File file = new File(folderName);
if (file.isDirectory()) {
ftpClient.chdir("\\");
ftpClient.setType(FTPTransferType.BINARY);
if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}
if (!checkFolderIsExist(file.getName())) {
ftpClient.mkdir(file.getName());
}
ftpClient.chdir(file.getName());
ftpPath = ftpPath + "\\" + file.getName();
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// System.out.println("==============================");
// System.out.println("ftpPath:"+ftpPath);
// System.out.println("files["+i+"]:"+files[i].getPath());
// System.out.println("==============================");
if (files[i].isDirectory()) {
uploadFolder(files[i].getPath(), ftpPath);
} else {
if (files[i].isFile()) {
try {
//===========================方法一开始================================
//ftpClient.chdir("\\");
//ftpClient.chdir(ftpPath);
//ftpClient.put(files[i].getPath(),files[i].getName());
//===========================方法一结束=================================
//
//===========================方法二开始================================
uploadFile(files[i].getPath(), ftpPath);
//===========================方法二结束================================
} catch (Exception ee) {
strMsg += "upload file<<:" +
files[i].getPath() +
">> error!Message:" + ee.getMessage() +
"\r\n";
}
}
}
}
if (!strMsg.equals("")) {
throw new Exception(strMsg);
}
} else {
System.out.println("file:" + file.getName());
throw new Exception(folderName + " is not a folder'name!");
}
} catch (Exception e) {
strMsg += e.getMessage() + "\r\n";
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
//删除FTP服务器上找定的文件
//ftpPath上对于根目录的路径
public String deleteFile(String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
try {
ftpClient.delete(ftpPath);
} catch (FTPException ex) {
strMsg += "删除文件错误:" + ex.getMessage();
} catch (IOException ex) {
strMsg += "操作文件错误:" + ex.getMessage();
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
//删除FTP服务器上找定的文件夹
public String deleteFolder(String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
ftpClient.chdir("\\"); //进入目录
//列出目录下所有文件和文件夹
FTPFile[] ftpfiles = ftpClient.dirDetails(ftpPath);
for (int i = 0; i < ftpfiles.length; i++) {
FTPFile tempftpfile = ftpfiles[i];
// System.out.println("===================================");
// System.out.println("文件名:"+tempftpfile.getName());
// System.out.println("路径:"+tempftpfile.getPath());
// System.out.println("是否是止录:"+tempftpfile.isDir());
// System.out.println("最后修改日录:"+tempftpfile.lastModified());
// System.out.println("===================================");
if (tempftpfile.isDir()) {
ftpPath = ftpPath + "\\" + tempftpfile.getName();
System.out.println("1:" + ftpPath);
deleteFolder(ftpPath);
} else {
ftpClient.delete(ftpPath + "\\" + tempftpfile.getName());
}
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
//把指定目录下所有的文件上传到ftp服务器上
public void uploadAllFilesInFolder(String folderName, String ftpPath) throws
Exception {
if (isLogin) {
String