public FtpClientUtil(String server, int port,String userName,String userPassword)
{
this .server=server;
this .port=port;
this .userName=userName;
this .userPassword=userPassword;
}
/**
* 鏈接到服務(wù)器
* @return
*/
public boolean open()
{
if (ftpClient!= null &&ftpClient.serverIsOpen())
return true ;
try
{
ftpClient= new FtpClient();
ftpClient.openServer(server,port);
ftpClient.login(userName, userPassword);
ftpClient.binary();
return true ;
}
catch (Exception e)
{
e.printStackTrace();
ftpClient= null ;
return false ;
}
}
/**
* 返回FTP目錄下的文件列表
* @param ftpDirectory
* @return
*/
public List<String> getFileNameList(String ftpDirectory)
{
List<String> list = new ArrayList<String>();
if (!open())
return list;
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(ftpDirectory));
String filename = "" ;
while ((filename=dis.readLine())!= null )
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
|