[Java]
Package com. metarnet. Execution. business. ftp;
Import java. io. File;
Import java. io. IOException;
Import java. text. ParseException;
Import org. apache. log4j. Logger;
Import com.w.isedt.net. ftp. FTPClient;
Import com.enterprisedt.net. ftp. FTPConnectMode;
Import com.enterprisedt.net. ftp. FTPException;
Import com.enterprisedt.net. ftp. FTPFile;
Import com.enterprisedt.net. ftp. FTPTransferType;
Import com. metarnet. Execution. Model. statics. FTP_information;
/**
* <P> ftp connection management (using apache commons-net-1.4.1 lib) </p>
* Qingfan
*/
Public class FTP_Client {
Public static final Logger logger = Logger. getLogger (FTP_Client.class );
Private FTPClient ftpClient = new FTPClient ();
Private static FTP_Client ftp_Client;
/**
* In Singleton mode, each instance can only have one FTP upload
* @ Return
*/
Public static FTP_Client getThis ()
{
If (ftp_Client = null)
{
Ftp_Client = new FTP_Client ();
}
Return ftp_Client;
}
/**
* Open the connection to FTP
* @ Return
* @ Throws IOException
* @ Throws FTPException
* @ Throws Exception
*/
Public boolean ConnectionFTP () throws IOException, FTPException
{
If (! FtpClient. connected ())
{
Try {
FtpClient. setRemoteHost (FTP_information.getThis (). getFTP_server ());
FtpClient. setRemotePort (FTP_information.getThis (). getFTP_port ());
FtpClient. setControlEncoding ("GB2312 ");
FtpClient. connect ();
FtpClient. setTimeout (1200 );
FtpClient. login (FTP_information.getThis (). getFTP_username (), FTP_information.getThis (). getFTP_password ());
FtpClient. setType (FTPTransferType. BINARY );
FtpClient. setConnectMode (FTPConnectMode. ACTIVE );
Logger. debug ("FTP connection successful! ");
} Catch (IOException e ){
Logger. error ("FTP_IO Connection Failed:", e );
Throw e;
} Catch (FTPException e ){
Logger. error ("FTP connection failed:", e );
Throw e;
}
}
Return ftpClient. connected ();
}
/**
* <P> upload files over ftp </p>
* @ Param srcUrl file to be uploaded
* @ Param targetFname: generate the target file
* @ Throws Exception
*/
Public String uploadFile (String srcUrl, String targetFname) throws Exception {
Try {
If (ConnectionFTP ())
{
If (! FtpClient. existsDirectory (FTP_information.getThis (). getFTP_UP_URL ()))
{
FtpClient. mkdir (FTP_information.getThis (). getFTP_UP_URL ());
}
If (OpenDir (FTP_information.getThis (). getFTP_UP_URL ()))
{
FtpClient. setType (FTPTransferType. BINARY); // sets the file type (BINARY)
Return ftpClient. put (srcUrl, targetFname); // upload
}
Else
{
Logger. debug ("failed to open directory:" + FTP_information.getThis (). getFTP_UP_URL ());
Return "failed to open directory:" + FTP_information.getThis (). getFTP_UP_URL ();
}
}
Else {
Logger. debug ("the FTP server is not enabled and the upload operation cannot be performed ");
Return "the FTP server is not enabled and cannot be uploaded ";
}
} Catch (Exception e ){
Logger. error ("exceptions occurred during FTP upload:", e );
Throw new Exception ("exceptions occurred during FTP upload", e );
}
}
/**
* Enter the desired directory
* @ Return
* @ Throws IOException
* @ Throws FTPException
* @ Throws Exception
*/
Public boolean OpenDir (String dirname)
{
Try {
String [] temp = dirname. split ("/");
If (temp. length> 0)
{
For (String tmp: temp ){
If (ConnectionFTP ())
{
FtpClient. chdir (tmp );
}
}
}
Else {
If (ConnectionFTP ())
{
FtpClient. chdir (dirname );
}
}
Return true;
} Catch (IOException e ){
Return false;
} Catch (FTPException e ){
Return false;
}
}
/**
* Getting the file list
* @ Return
* @ Throws FTPException
* @ Throws IOException
* @ Throws ParseException
* @ Throws Exception
*/
Public FTPFile [] getFileNameList () throws IOException, FTPException, ParseException
{
FTPFile [] fileList = ftpClient. dirDetails (FTP_information.getThis (). getFTP_UP_URL ());
FtpClient. chdir (FTP_information.getThis (). getFTP_UP_URL ());
Return fileList;
}
/**
* Download folder
* @ Param saveDir
*/
Public void Download (String saveDir)
{
Try {
If (ConnectionFTP ())
{
FTPFile [] list = getFileNameList ();
File file = new File (saveDir );
If (! File. exists ()&&! File. isDirectory ())
{
File. mkdir ();
}
For (FTPFile FileName: list ){
If (FileName. isFile ()){
FtpClient. get (saveDir, FileName. getName ());
Logger. debug ("download file:" + FileName. getName () + "complete ");
}
}
Logger. debug ("current folder:" + FTP_information.getThis (). getFTP_UP_URL () + "download completed ");
FtpClient. quit ();
}
} Catch (IOException e ){
Logger. error (e );
} Catch (FTPException e ){
Logger. error (e );
} Catch (ParseException e ){
Logger. error (e );
}
}
/**
* <P> delete an ftp file </p>
*/
Public boolean removeFile (String srcFname ){
If (ftpClient. connected ()){
Try {
If (ftpClient. existsFile (srcFname ))
{
FtpClient. delete (srcFname );
}
Else
{
Logger. debug ("the file to be deleted does not exist ");
}
Return true;
} Catch (Exception e ){
Logger. error ("FTP File Deletion exception:", e );
This. closeCon ();
}
}
Return false;
}
/**
* <P> destroy an ftp connection </p>
*/
Public void closeCon (){
If (ftpClient. connected ()){
Try {
FtpClient. quit ();
} Catch (Exception e ){
Logger. error ("FTP disconnection failed:", e );
}
}
}
}