FTP Tools

2010-03-29  来源:本站原创  分类:Java  人气:310 

package com.example.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.FTPClientConfig;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;

public class FtpUtil {
        //ftp Client
        private FTPClient ftpClient = null;
    //ftp Server address
    private String hostname;
    //ftp The server default port
    public  int defaultport = 21;
    // Sign-in name
    private String username;
    // Login password
    private String password;
    // Need access to the remote directory
    private String remoteDir;

        /**
         *  Use the host name, port, user name, password, host directory, Chinese time zone.
         * @param hostname
         * @param port
         * @param username
         * @param password
         * @param remoteDir
         * @param is_zhTimeZone
         */
        public FtpUtil(String hostname,int port,String username,String password,String remoteDir,boolean is_zhTimeZone) {
                this.hostname=hostname;
        this.username=username;
        this.password=password;
        this.remoteDir=remoteDir==null?"":remoteDir;
        this.ftpClient = new FTPClient();
        if(is_zhTimeZone) {
            this.ftpClient.configure(FtpUtil.Config());
            this.ftpClient.setControlEncoding("GBK");
        }
        // Login
        this.login();
        // Modify the directory
        this.changeDir(this.remoteDir);
        // Set the file type
        this.setFileType(FTPClient.ASCII_FILE_TYPE);
        ftpClient.setDefaultPort(port);
        }

        /**
         *  On the client settings, date format
         * @return
         */
        private static FTPClientConfig Config() {
        FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        conf.setRecentDateFormatStr("MM Month dd, HH  :mm");
        return conf;
    }

        /**
     *  Log on to the FTP server
     */
    public void login() {
        try {
            ftpClient.connect(this.hostname);
            ftpClient.login(this.username, this.password);
        } catch (FTPConnectionClosedException e) {
            System.out.println("FTP The connection has been closed  ");
        } catch (IOException e) {
            System.out.println(" Input/output error  ");
        }
        System.out.println(" Connect to the FTP server:  " + this.hostname + "  Successful, start logon  ");
    }

    /**
     *  Change working directory
     * @param remoteDir-- The directory path
     */
    public void changeDir(String remoteDir) {
        try {
            this.remoteDir = remoteDir;
            ftpClient.changeWorkingDirectory(remoteDir);
        } catch (IOException e) {
                System.out.println(" Input/output error  ");
        }
        System.out.println(" Change working directory to  :"+remoteDir);
    }

    /**
     *  Set the transport type of the file  [ Binary file or text file  ]
     * @param fileType--BINARY_FILE_TYPE,ASCII_FILE_TYPE
     */
    public void setFileType(int fileType){
        try{
            ftpClient.setFileType(fileType);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    /**
     *  Upload files
     * @param localFilePath-- The local file path  + File name
     * @param newFileName-- A new file name
     */
    public void uploadFile(String localFilePath,String newFileName){

        BufferedInputStream buffIn=null;
        try{
            buffIn=new BufferedInputStream(new FileInputStream(localFilePath));
            ftpClient.storeFile(newFileName, buffIn);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if(buffIn!=null)
                    buffIn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
       }

    /**
     *  Download the file  ( A single  )
     * @param remoteFileName -- The file name on the server
     * @param localFileName-- The local file name
     */
    public boolean downloadFile(String remoteFileName,String localFileName){
        BufferedOutputStream buffOut=null;
        try{
            buffOut=new BufferedOutputStream(new FileOutputStream(localFileName));
            ftpClient.retrieveFile(remoteFileName, buffOut);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            try{
                if(buffOut!=null)
                    buffOut.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     *  Close the FTP connection
     */
    public void close() {
        try{
            if(ftpClient!=null){
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *   Gets the path directory all files
     */
    public FTPFile[] getFileList(String path) {
        try {
                        FTPFile[] list=ftpClient.listFiles(path);
                        for(FTPFile ftpFile:list){
                                System.out.println("-----------------------:"+ftpFile.getName());
                        }
                        return list;
                } catch (IOException e) {
                        System.out.println(" Input/output error  -----------------");
                }
                return null;
    }

}
相关文章
  • gFTP: linux ftp tools under 2010-05-09

    gFTP is a use under the X Window Gtk development of multi-threaded FTP client tool, it runs under Microsoft Windows CuteFTP very similar to other FTP tools. Features: * Use the C language and have the text written in GTK + 1.2/2.0 interface, interfac

  • FTP Tools 2010-03-29

    package com.example.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.ap

  • FileZilla-Linux ftp upload tools to install and configure using the 2011-02-20

    FileZilla-Linux ftp upload tools to install and configure using the Used in the Windows system using the FTP file transfer tool FlashXFTP upload web page files, FlashXFTP web site upload tool to download files easily. So in the Ubuntu linux system us

  • unbuntu ftp installation and configuration service 2010-10-15

    ubuntu ftp server installation and configuration 2007-07-20 18:33 1. Download vsftpd # Apt-get install vsftpd PS: If you are prompted ---- Tip "E: dpkg was interrupted, you must manually run 'dpkg - configure-a' to correct the problem." The solu

  • nohup command for running in the background-linux 2010-02-04

    nohup ruby /sunrise/www/realworx-rails/current/lib/daemons/monitor.rb 14 4014 & id and port Unix / Linux under the General wanted a program running in the background, many of which are used & in the program at the end to let the program run automa

  • [Change] English tutorial wonderful putty 2010-03-31

    These words are from my years of experience in the use PuTTY come slowly, but also just introduced PuTTY, also includes a number of related software, such as: excellent FTP tools FileZilla, a powerful SFTP client WinSCP . Invent a number of assorted,

  • PuTTY Chinese Tutorial 2010-04-22

    These words are from my years of experience in the use PuTTY come slowly, but also just introduced PuTTY, also includes a number of related software, such as: excellent FTP tools FileZilla, a powerful SFTP client WinSCP . Invent a number of assorted,

  • Linux SSH command Daquan 2010-05-02

    Linux SSH command Daquan rm-rf mydir / * delete mydir directory * / cd mydir / * mydir directory entry * / cd - / * Top level directory * / cd ~ / * return the root directory * / mv tools tool / * the tools directory renamed the tool * / ln-s tool ba

  • linux install online live TV and can be installed under linux system software (5) 2010-05-27

    http://hi.baidu.com/naruto6006/blog/item/d9e4bd8a41e403d8fc1f1042.html Opera for Linux Download: http://www.onlinedown.net/soft/3564.htm Now my linux system to use the software and the software environment under windows give a comparison result. 1, o

  • In Windows XP use OpenSSH and PuTTY's public key authentication remote management Unubtu Server 10.04 LTS - the 2010-07-07

    NOTE: The code notes that the role of # only, the content can not be entered into the command line window Remote management Ubuntu, can in many ways, here only introduced SSH. Remote login via SSH to the server, so you feel like working on the same s

  • Transmit installation 2010-07-07

    FTP tools under MacOSX, charges the software. Supports the following protocols: FTP FTP with Implicit SSL FTP with TLS / SSL SFTP WebDAV WebDAV HTTPS Amazon S3 Here to provide a serial number: X3yX-r5gy-R77T-5xrx Using the SFTP connection, you can us

  • Linux on the installation and configuration using the Tomcat documentation 2010-07-27

    Linux on the installation and configuration using the Tomcat documentation Install the downloaded software will appeal <br /> Suppose you want to download the software, all downloads in / usr / directory. Jdk: jdk-6u1-linux-i586.bin Tomcat: apache-t

  • Solaris system will approach the process into the background 2010-08-26

    Solaris system will approach the process into the background Problem description: Solaris system in the process of transfer will be running in the background Solution: Transfer process in solaris will run in the background are two cases, one is not a

  • to deploy jsp linux web site (reproduced from the network) 2010-09-01

    1. Overview Overview Website deployment process. The deployment guide is about how to deploy in the server main site, as well as related hardware and software configurations, relevant terminology introduced. 2. Hardware and software environment 2.1 M

  • XFTP secondary synchronization server Gadgets (VC + +) 2010-10-12

    Result of the need, often through the ftp tool to upload some files to the server, this operation has been very annoying, but more annoying is that they would go on simultaneously to the other server (of course there is no manual synchronization, eli

  • Saying black chain and black chain method to prevent the 5 2010-11-04

    Saying black chain, black chain is the site put up their own in the other links in this chain chain of non-Links, but a hidden in the foreground, so check the other owners is not easy to detect a link, only through the source code view only it can be

  • LINUX nohup command out of the depth of input and output 2010-12-02

    Nohup command you may encounter problems, here describes the solution to the problem nohup command, Linux itself is the core of the operating system, which is the operating system kernel. Kernel is the completion of the basic operation of those proce

  • Nohup command you may encounter problems, here describes the solution to the problem nohup command, Linux itself is the core of the operating system, which is the operating system kernel. The kernel is the completion of the basic operations that 2010-12-02

    Nohup command you may encounter problems, here describes the solution to the problem nohup command, Linux itself is the core of the operating system, which is the operating system kernel. Kernel is the completion of the basic operation of those proce

  • Remote maintenance tool commonly used server-side profile 2010-12-03

    Maintenance later in the project, usually in remote server, how do we effectively maintain it, the following talk about some of the tools I use. A, windows server server: Generally not recommended to use windows as a server, we encountered before the

  • Linux system using SSH remote control security 2010-12-15

    Network is attacked, in many cases provides a Telnet service because the server is caused. Indeed, for UNIX systems, if you want to remotely manage it, we must use the remote terminals, and to use the remote terminal is natural to start the Telnet se