这是本人第一篇原创,但是还是不知道写啥就索性把代码拷贝上来吧。
之所以用到这个知识,是由于希望通过一台web服务器页面方式访问与之链接的所有其他服务器的文件。
使用到的jar包来自 http://enterprisedt.com/products/edtftpj/
1、先对jar中的函数作简单介绍
2、再附上一个简单jar包,实现了一个更高层的封装使用
1、先对jar中的函数作简单介绍
核心的类 : FileTransferClient(旧版本时使用FTPClient )
创建ftp对象
FileTransferClient ftp = new FileTransferClient();
设置登录信息
ftp.setRemoteHost(host);
ftp.setUserName(username);
ftp.setPassword(password);
连接服务器并且登录
ftp.connect();
退出并断开连接
ftp.disconnect();
连接服务器并且手动登录
ftp.connect();
ftp.manualLogin();
设置为非自动登录
ftp.getAdvancedFTPSettings().setAutoLogin(false);
传输模式(默认binary)
设置为ascii
ftp.setContentType(FTPTransferType.ASCII);
高级设置,设置传输连接模式,主动和被动都是相对server而言
设置为主动模式:服务器发起数据连接
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.ACTIVE);
ftp.getAdvancedFTPSettings().setActivePortRange(61500, 61510);
被动模式:客户端发起数据连接,服务器监听
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);
返回当前目录下的文件和目录
String[] files = ftp.directoryNameList();
返回当前目录下的文件和目录的详细信息
String[] descriptions = ftp.directoryNameList("mydirname", true);
以对象形式返回当前目录下的文件和目录的详细信息,对象里面包括大小、是否为目录等。
FTPFile[] files = ftp.directoryList();
获取当前目录
String directory = ftp.getRemoteDirectory();
改变当前目录
ftp.changeDirectory(directory);
回到上层目录
ftp.changeToParentDirectory();
上传文件(简单用法)
ftp.uploadFile(localFilePath, remoteFileName);
下载文件(简单用法)
ftp.downloadFile(localFilePath, remoteFileName);
删除文件
deleteFile(remoteFileName)
服务器为德国场所,客户端为英国场所,此时可能会报错,通过如下设置客户端场所转换,来避免此类问题
Locale[] locales = new Locale[2];
locales[0] = = Locale.GERMAN;
locales[1] = Locale.getDefault();
ftp.getAdvancedFTPSettings().setParserLocales(locales);
2、再附上一个自己写的简单jar包代码,实现了一个更高层的封装使用
/*** ** 使用两个函数即可达到便利下载文件的目的 * setPara设置参数, * download2下载文件 ** */ package com.xxx import java.util.Date; import com.enterprisedt.net.ftp.*; import com.enterprisedt.util.debug.*; /******** * 功能描述:ftp客户端功能,负责ftp请求和处理 * * */ public class ftpProc { //开发环境下 debugFlag设置为true,其他环境设置为false private boolean debugFlag = true; FileTransferClient ftp = null; String localFilePath = "D:\\"; //投产环境一定要修改 //public static int ftpCount = 5; //控制实例的数量,在创建 该对象前判断 public String fileFullName = ""; public ftpProc() { } /*** * 功能描述:封装的下载文件方法(建立链接,下载文件到本地临时目录,退出连接) * 参数:远程路径+文件名 * */ public boolean download2(String remoteFilePath) { if("" == conn()) { return false; } else { String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/")+1); fileFullName = localFilePath+"/"+fileName ; if(false == download(fileFullName, remoteFilePath)) { return false; } else { if(false==bye()) { return false; } } } return true; } /***** * 功能描述:设置登录参数 * 主机 用户名 密码 本地目录 */ public boolean setPara(String host, String username, String password, String localPath){ try{ ftp = new FileTransferClient(); ftp.setRemoteHost(host); ftp.setUserName(username); ftp.setPassword(password); localFilePath = localPath; } catch(FTPException ftpE){ Debug(""+ftpE.getReplyCode()); Debug(""+ftpE.toString()); return false; } catch(Exception e) { Debug(e.getMessage()); return false; } return true; } /***功能描述: 连接 登录并显示当前目录 * */ public String conn() { String remoteDir = ""; try{ ftp.connect(); remoteDir = ftp.getRemoteDirectory(); } catch(FTPException ftpE){ Debug(""+ftpE.getReplyCode()); Debug(""+ftpE.toString()); return ""; } catch(Exception e) { Debug(e.getMessage()); return ""; } return remoteDir; } /*****功能描述:下载文件 * localPath : 本地路径 * remoteFilename: 远程文件名(全路径) */ public boolean download(String localPath, String remoteFilename) { try{ if(ftp.exists(remoteFilename)){ ftp.downloadFile(localPath, remoteFilename); } else return false; } catch(FTPException ftpE){ Debug(""+ftpE.getReplyCode()); Debug(""+ftpE.toString()); return false; } catch(Exception e) { Debug(e.getMessage()); return false; } return true; } /*****退出ftp * */ public boolean bye() { try{ ftp.disconnect(); } catch(FTPException ftpE){ Debug(""+ftpE.getReplyCode()); Debug(""+ftpE.toString()); return false; } catch(Exception e) { Debug(e.getMessage()); return false; } return true; } private void Debug(String msg){ if(true==debugFlag){ Date logTime = new Date(); System.out.println(logTime.toString() + ":[ftp]:"+msg); } } }