igjhrkehrf 发表于 2016-6-9 11:47:43

使用commons-net包实现ftp服务器的访问

import   java.io.InputStream;
importjava.util.Properties;

importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPClientConfig;
importorg.apache.commons.net.ftp.FTPReply;
importorg.uranus.util.StringUtils;

/** */ /**
* 基于apache组织的commonNet开源组件实现ftp客户端<br>
* 本程序运行依赖于一个config成员变量属性,其是一个属性(Properties)对象<br>
* 系统要求这个config对象,必需有如下属性key:<br>
* server(ftp服务器ip地址或域名)<br>
* username(登录用户名)<br>
* password(登录密码)<br>
* 如下属性key是可选的:<br>
* systemKey(ftp服务器的操作系统key,如window)<br>
* serverLanguageCode(ftp服务器使用的语言,如zh)<br>
* <br>
* 本程序是线程安全,在每一个上传时都将创建ftp连接,上传结束后再关闭ftp连接<br>
*
* 例子:<br>
* InputStream localIn = new FileInputStream("c:\\计算机.txt");<br>
*
* java.util.Properties config = new Properties();<br>
* config.setProperty("server", "192.168.5.120");<br>
* config.setProperty("username", "upload");<br>
* config.setProperty("password", "upload");<br>
*
* FtpClientCommonNetImpl client = new FtpClientCommonNetImpl();<br>
* client.setConfig(config);<br>
* client.upload(localIn, "/aaa/计算机.txt", true);<br>
*
*
*@authorzhangdb
*
*/
public   classFtpClientCommonNetImplimplementsIFtpClient{
//ftp配置
privateProperties config;

/** */ /**
* 连接到ftp服务器
*
*@paramserver
*@paramuserName
*@parampassword
*@return
*@throwsFtpException
*/
protectedFTPClient connectFtpServer(String server, String userName,
String password)throwsFtpException{
//创建ftp客户端对象
FTPClient ftp=   newFTPClient();
try   {
ftp.configure( this .getFTPClientConfig());
//连接ftp服务器
ftp.connect(server);
//登录
ftp.login(userName, password);

//返回值
intreply=ftp.getReplyCode();
if(( ! FTPReply.isPositiveCompletion(reply))){
ftp.disconnect();
throw   newFtpException( " 登录ftp服务器失败,请检查server[ "   +server
+   " ]、username[ "   +userName+   " ]、password[ "   +password
+   " ]是否正确! " );
}

returnftp;
}   catch(Exception ex){
throw   newFtpException(ex);
}
}

/** */ /**
* 关闭连接
*
*@paramftp
*/
protected   voiddisconnectFtpServer(FTPClient ftp){
try   {
ftp.logout();
ftp.disconnect();
}   catch(Exception ex){
throw   newFtpException(ex);
}
}

/** */ /**
* 上传
*/
public   voidupload(InputStream localIn, String remoteFilePath)
throwsFtpException{
String server=   this .config.getProperty( " server " );
String userName=   this .config.getProperty( " username " );
String password=   this .config.getProperty( " password " );
FTPClient ftp=   this .connectFtpServer(server, userName, password);

try   {
booleanresult=ftp.storeFile( this
.enCodingRemoteFilePath(remoteFilePath), localIn);
if( ! result){
throw   newFtpException( " 文件上传失败! " );
}
}   catch(Exception ex){
throw   newFtpException(ex);
}   finally   {
this .disconnectFtpServer(ftp);
}
}

/** */ /**[来源www.iocblog.net]
* 上传结束以后关闭输入流
*
*@paramlocalIn
*@paramremoteFilePath
*@paramafterUploadCloseInputStream
*@throwsFtpException
*/
public   voidupload(InputStream localIn, String remoteFilePath,
booleanafterUploadCloseInputStream)throwsFtpException{
try   {
//上传
this .upload(localIn, remoteFilePath);
}   finally   {
if(afterUploadCloseInputStream){
if(localIn!=   null ){
try   {
localIn.close();
}   catch(Exception ex){
throw   newFtpException(ex);
}
}
}
}
}[来源www.iocblog.net]

/** */ /**
* 得到配置
*
*@return
*/
protectedFTPClientConfig getFTPClientConfig(){
//创建配置对象
FTPClientConfig conf=   newFTPClientConfig( this .config.getProperty(
" systemKey " , FTPClientConfig.SYST_NT));
conf.setServerLanguageCode( this .config.getProperty(
" serverLanguageCode " ," zh " ));
returnconf;
}

/** */ /**
* 远程文件路径编码(上传到ftp上的文件路径)
*
*@paramremoteFilePath
*@return
*/
protectedString enCodingRemoteFilePath(String remoteFilePath){
returnStringUtils.gbkToIso8859EnCoding(remoteFilePath);
}

publicProperties getConfig(){
returnconfig;
}

public   voidsetConfig(Properties config){
this .config=config;
}

}
页: [1]
查看完整版本: 使用commons-net包实现ftp服务器的访问