FtpFileUtil memo

package ...;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import org.apache.log4j.Logger;

public class FtpFileUtil {

    private static Logger Log = Logger.getLogger(FtpFileUtil.class);
    private static String ENCODING = "GBK";
    
    private static String ftphostaddr = "...";
    private static int port = 21;
    private static String ftpname = "...";
    static String = ftppwd Private "...";
    Private ftppath static String = "...";
    Private static String localpath = "...";
    Private static String filename = "test.txt";
    
    public static void main (String [] args) {
// // upload
// FtpUtil.uploadFile (ftphostaddr, Port, ftpname, ftppwd
//, ftppath, localPath, filename);

// // download file
// FtpUtil.downFile (ftphostaddr, port, ftpname, ftppwd
//, ftppath, localpath, filename);
        
// // delete files
// FtpUtil.deleteFile (ftphostaddr, Port, ftpname, ftppwd
//, ftppath, filename);
        
// // get all the files directory name
For // (String String: FtpUtil.getFileNames (ftphostaddr, Port, ftpname, ftppwd
//, ftppath)) {
// System.out.println (String);
//}
        
        
    }

    
    / **
     * from the local file to upload server
     * the presence of additional writing file
     * /
    public static void the uploadFile (String ftphostaddr, int Port, ftpname String,
            String ftppwd, ftppath String, String localPath, String filename) {
        // the FTP connection
        FTPClient ftp = initFtp (ftphostaddr, port , ftpname, ftppwd);
        the try {
            String filename + fileAllName = ftppath;
            log.info ( "Upload directory:" + + ftphostaddr fileAllName);

            // FTP directory exists Analyzing
            = Catalog to false Boolean;
            FTPFile [] = ftpFileArr ftp.listFiles (ftppath);
            IF (ftpFileArr.length> 0) {
                Catalog = to true;
            }
            
            // Create a directory
            IF (Catalog!) {
                log.info ( "directory does not exist, Please try again later to see ");.
// Flag = boolean to true;
// String = newdir new new String (ftppath.getBytes (the ENCODING)," ISO-8859-1 ");
// Flag = ftp.makeDirectory (newdir) ;
// IF (In Flag) {
// log.info ( "the make Directory" + + newdir The "successfully!");
// the else {}
// log.info ( "the make Directory"+ newdir + " failed !");
//                }
            }
            
            // 上传文件
            boolean flag = false;
            File file = new File( localpath+filename );
            InputStream input  = new FileInputStream( file );
            String remote = new String(fileAllName.getBytes(ENCODING), "ISO-8859-1");
            if (ftp.appendFile(remote, input)) {
                flag = true;
            }
            input.close();
            Log.info("push file (" + file.getCanonicalPath() + ") => " + (flag ? "SUCCESS" : "FAILED"));

            ftp.logout();
            ftp.disconnect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
            new new Exception ( "Failed to upload files from a local server");
        }
    }

    
    / **
     * download files from the file server to the local
     * does not delete the file server
     * file additional written
     * /
    public static void downFile (String ftphostaddr, int Port, ftpname String,
            String ftppwd, ftppath String, String localPath, String filename) {
        FTPClient FTP = initFtp (ftphostaddr, Port, ftpname, ftppwd);
        the try {
            // 4. specify download directory
            ftp. changeWorkingDirectory (ftppath); // transferred to the FTP server directory
            // the download directory traversal
            @ Chinese garbled solution, twice decoded
            byte [] bytes = filename.getBytes ( " iso-8859-1");
            Fn = String new new String (bytes, "UTF8");
            IF (fn.equals (filename)) {
                // 6. The write operation is written to the local file
                Log.info ( "Download File:" + filename + "start:"
                        + System.currentTimeMillis ());
                file = localfile new new file (localPath + filename);
                the OutputStream new new IS = a FileOutputStream (localfile);
                ftp.retrieveFile (filename, IS);

// // local file 7. The determination whether written correctly, if written correctly, it will delete files on the file server
// IF (localFile.exists ()) {
// log.info ( "delete a file server:" + filename);
// ftp.deleteFile (filename);
//}
                
                Log.info ( "Download File:" + filename + "End:"
                        + System.currentTimeMillis ());
                is.close ();
            }
            // close 8. The
            ftp.logout ();
            ftp.disconnect ();
        } the catch (Exception E) {
            e.printStackTrace ();
            new new Exception ( "error occurred during the download file from the server");
        } the finally {
            IF (ftp.isConnected ()) {
                the try {
                    ftp.disconnect ();
                } the catch (IOException IOE) {
                    ioe.printStackTrace ();
                }
            }
        }
    }

    
    / **
     * Delete the file on the server
     * /
    public static void the deleteFile (String ftphostaddr, int Port, ftpname String, String ftppwd
            , ftppath String, String filename) {
        FTPClient FTP = initFtp (ftphostaddr, Port, ftpname, ftppwd);
        
        log.info ( "delete file server:" + filename);
        the try {
            ftp.cwd (ftppath);
            ftp.deleteFile (filename);

            ftp.logout ();
            ftp.disconnect ();
        } the catch (IOException E) {
            // the TODO Block the catch-Generated Auto
            e.printStackTrace ();
            new new Exception ( "failed to delete file on the server");
        }
    }

    
    / **
     * get the name of the server folder of all the files
     * /
    Public static String [] getFileNames (String ftphostaddr, int Port, String ftpname, ftppwd String
            , String ftppath) {
        FTPClient FTP = initFtp (ftphostaddr, Port, ftpname, ftppwd);

        the try {
            ftp.cwd (ftppath);
            FTP. enterLocalPassiveMode (); // open a port to transmit data
            String [] = ftp.listNames the subfile ();
            return the subfile;
        } the catch (IOException E) {
            // the TODO Auto-Generated Block the catch
            e.printStackTrace ();
            new new Exception ( "failed to obtain the name of all files under the folder");
        }
        return null;
    }

    
    / **
     * FTP connection
     * /
    FTPClient initFtp static public (String ftphostaddr, int Port,
            String ftpname, String ftppwd) {
        int Reply;
        FTPClient FTPClient new new FTP = ();
        the try {
            // 1. Connection Server
            // ftp.connect (ftphostaddr);
            ftp.connect ( ftphostaddr, port);
            // 2. If the default login server port, you can use ftp.connect (url) is directly connected to an FTP server
            ftp.login (ftpname, ftppwd);
            // 3. determine whether the successful landing
            reply = ftp .getReplyCode ();
            (! FTPReply.isPositiveCompletion (Reply)) {IF
                ftp.disconnect ();
            }
        } the catch (Exception E) {
            e.printStackTrace ();
            new Exception ( "server connection failure");
        }
        return FTP;
    }

    
    / **
     * close connection
     * /
    public static void closeConnect (FTPClient FTP) {
        the try {
            (! = null FTP) IF {
                // 8. The closed
                ftp.logout ();
                ftp.disconnect ();
            }
            log.info ( "Close Connect");
        } the catch (Exception E) {
            e.printStackTrace ();
        }
    }
    
    
}

Guess you like

Origin www.cnblogs.com/kldywx/p/11589384.html