packageftp;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.TimeZone;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPClientConfig;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;importorg.apache.log4j.Logger;public classFTPTest_04 {privateFTPClient ftpClient;privateString strIp;private intintPort;privateString user;privateString password;private static Logger logger = Logger.getLogger(FTPTest_04.class.getName());/**
* Ftp构造函数*/
public FTPTest_04(String strIp, intintPort, String user, String Password) {this.strIp =strIp;this.intPort =intPort;this.user =user;this.password =Password;this.ftpClient = newFTPClient();
}/***@return判断是否登入成功
**/
public booleanftpLogin() {boolean isLogin = false;
FTPClientConfig ftpClientConfig= newFTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());this.ftpClient.setControlEncoding("GBK");this.ftpClient.configure(ftpClientConfig);try{if (this.intPort > 0) {this.ftpClient.connect(this.strIp, this.intPort);
}else{this.ftpClient.connect(this.strIp);
}//FTP服务器连接回答
int reply = this.ftpClient.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {this.ftpClient.disconnect();
logger.error("登录FTP服务失败!");returnisLogin;
}this.ftpClient.login(this.user, this.password);//设置传输协议
this.ftpClient.enterLocalPassiveMode();this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
logger.info("恭喜" + this.user + "成功登陆FTP服务器");
isLogin= true;
}catch(Exception e) {
e.printStackTrace();
logger.error(this.user + "登录FTP服务失败!" +e.getMessage());
}this.ftpClient.setBufferSize(1024 * 2);this.ftpClient.setDataTimeout(30 * 1000);returnisLogin;
}/*** @退出关闭服务器链接
**/
public voidftpLogOut() {if (null != this.ftpClient && this.ftpClient.isConnected()) {try{boolean reuslt = this.ftpClient.logout();//退出FTP服务器
if(reuslt) {
logger.info("成功退出服务器");
}
}catch(IOException e) {
e.printStackTrace();
logger.warn("退出FTP服务器异常!" +e.getMessage());
}finally{try{this.ftpClient.disconnect();//关闭FTP服务器的连接
}catch(IOException e) {
e.printStackTrace();
logger.warn("关闭FTP服务器的连接异常!");
}
}
}
}/***
* 上传Ftp文件
*@paramlocalFile 当地文件
*@paramromotUpLoadePath上传服务器路径 - 应该以/结束
**/
public booleanuploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream= null;boolean success = false;try{this.ftpClient.changeWorkingDirectory(romotUpLoadePath);//改变工作路径
inStream = new BufferedInputStream(newFileInputStream(localFile));
logger.info(localFile.getName()+ "开始上传.....");
success= this.ftpClient.storeFile(localFile.getName(), inStream);if (success == true) {
logger.info(localFile.getName()+ "上传成功");returnsuccess;
}
}catch(FileNotFoundException e) {
e.printStackTrace();
logger.error(localFile+ "未找到");
}catch(IOException e) {
e.printStackTrace();
}finally{if (inStream != null) {try{
inStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returnsuccess;
}/***
* 下载文件
*@paramremoteFileName 待下载文件名称
*@paramlocalDires 下载到当地那个路径下
*@paramremoteDownLoadPath remoteFileName所在的路径
**/
public booleandownloadFile(String remoteFileName, String localDires,
String remoteDownLoadPath) {
String strFilePath= localDires +remoteFileName;
BufferedOutputStream outStream= null;boolean success = false;try{this.ftpClient.changeWorkingDirectory(remoteDownLoadPath);
outStream= new BufferedOutputStream(newFileOutputStream(
strFilePath));
logger.info(remoteFileName+ "开始下载....");
success= this.ftpClient.retrieveFile(remoteFileName, outStream);if (success == true) {
logger.info(remoteFileName+ "成功下载到" +strFilePath);returnsuccess;
}
}catch(Exception e) {
e.printStackTrace();
logger.error(remoteFileName+ "下载失败");
}finally{if (null !=outStream) {try{
outStream.flush();
outStream.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}if (success == false) {
logger.error(remoteFileName+ "下载失败!!!");
}returnsuccess;
}/***
* @上传文件夹
*@paramlocalDirectory
* 当地文件夹
*@paramremoteDirectoryPath
* Ftp 服务器路径 以目录"/"结束
**/
public booleanuploadDirectory(String localDirectory,
String remoteDirectoryPath) {
File src= newFile(localDirectory);try{
remoteDirectoryPath= remoteDirectoryPath + src.getName() + "/";boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath);
System.out.println("localDirectory : " +localDirectory);
System.out.println("remoteDirectoryPath : " +remoteDirectoryPath);
System.out.println("src.getName() : " +src.getName());
System.out.println("remoteDirectoryPath : " +remoteDirectoryPath);
System.out.println("makeDirFlag : " +makeDirFlag);//ftpClient.listDirectories();
}catch(IOException e) {
e.printStackTrace();
logger.info(remoteDirectoryPath+ "目录创建失败");
}
File[] allFile=src.listFiles();for (int currentFile = 0;currentFile < allFile.length;currentFile++) {if (!allFile[currentFile].isDirectory()) {
String srcName=allFile[currentFile].getPath().toString();
uploadFile(newFile(srcName), remoteDirectoryPath);
}
}for (int currentFile = 0;currentFile < allFile.length;currentFile++) {if(allFile[currentFile].isDirectory()) {//递归
uploadDirectory(allFile[currentFile].getPath().toString(),
remoteDirectoryPath);
}
}return true;
}/***
* @下载文件夹
*@paramlocalDirectoryPath本地地址
*@paramremoteDirectory 远程文件夹
**/
public booleandownLoadDirectory(String localDirectoryPath,String remoteDirectory) {try{
String fileName= newFile(remoteDirectory).getName();
localDirectoryPath= localDirectoryPath + fileName + "//";newFile(localDirectoryPath).mkdirs();
FTPFile[] allFile= this.ftpClient.listFiles(remoteDirectory);for (int currentFile = 0;currentFile < allFile.length;currentFile++) {if (!allFile[currentFile].isDirectory()) {
downloadFile(allFile[currentFile].getName(),localDirectoryPath, remoteDirectory);
}
}for (int currentFile = 0;currentFile < allFile.length;currentFile++) {if(allFile[currentFile].isDirectory()) {
String strremoteDirectoryPath= remoteDirectory + "/"+allFile[currentFile].getName();
downLoadDirectory(localDirectoryPath,strremoteDirectoryPath);
}
}
}catch(IOException e) {
e.printStackTrace();
logger.info("下载文件夹失败");return false;
}return true;
}//FtpClient的Set 和 Get 函数
publicFTPClient getFtpClient() {returnftpClient;
}public voidsetFtpClient(FTPClient ftpClient) {this.ftpClient =ftpClient;
}public static void main(String[] args) throwsIOException {
FTPTest_04 ftp=new FTPTest_04("10.0.0.102",21,"admin","123456");
ftp.ftpLogin();
System.out.println("1");//上传文件夹
boolean uploadFlag = ftp.uploadDirectory("D:\\test02", "/"); //如果是admin/那么传的就是所有文件,如果只是/那么就是传文件夹
System.out.println("uploadFlag : " +uploadFlag);//下载文件夹
ftp.downLoadDirectory("d:\\tm", "/");
ftp.ftpLogOut();
}
}