JavaはFTP関連操作を実現する


/**
 * Created by   on 2016/4/29.
 *
 */
public class FtpTool {

    /*ftp */
    private String userName;
    /*ftp */
    private String password;
    /*ftp ip*/
    private String ip;
    private FTPClient ftpClient;


    public FtpTool(String userName, String password, String ip){
        this.userName = userName;
        this.password = password;
        this.ip = ip;

    }

        /**
         *  
         *
         * @param  f  
         * @param  uploadDir  
         *
         * @return boolean b  
         * */
        public boolean putFile(File f,String uploadDir) {
            InputStream instream = null;
            boolean result = false;
            try{
                try{
                    ftpClient.changeWorkingDirectory(uploadDir);
                    instream = new BufferedInputStream(new FileInputStream(f));
                    result = ftpClient.storeFile(f.getName(), instream);
                }finally{
                    if(instream!=null){
                        instream.close();
                    }
                }
            }catch(IOException e){
                e.printStackTrace();
            }

            return result;
        }

        /**
         *  ftp 
         *
         * @param  f  ftp 
         * @param  localPath  
         *
         * @return boolean  
         * */
        public boolean getFile(File f  , String localPath){
            OutputStream outStream = null;
            boolean result = false;
            try{
                try{
                    outStream = new BufferedOutputStream(new FileOutputStream(new File(localPath)));
                    String path = f.getPath();
                    path = path.replaceAll("\\\\", "/");
                    String filepath = path.substring(0, path.lastIndexOf("/")+1)+"";
                    String fileName = path.substring(path.lastIndexOf("/")+1)+"";
                    boolean b = ftpClient.changeWorkingDirectory(filepath);
                    if(b){
                        result = ftpClient.retrieveFile(fileName, outStream);
                    }
                }finally{
                    if(outStream != null){
                        outStream.close();
                    }
                }
            }catch(IOException e){
                e.printStackTrace();
            }
            return result;
        }
        /**
         *  ftp 
         *
         * @return ftpClient
         * */
        public FTPClient getFTPClient(){

            try {
                ftpClient = new FTPClient();

                ftpClient.connect(ip);
                ftpClient.login(userName, password);
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ftpClient;
        }

        /**
         *   ftpClient 
         *
         *  @param ftpClient  ftpClient 
         *
         * */
        public void closeFTPClient(FTPClient ftpClient){
            try {
                try{
                    ftpClient.logout();
                }finally{
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    public static void main(String[] args) {
        FtpTool test=new FtpTool("liuwx","123","127.0.0.1");
        FTPClient   client= test.getFTPClient();
        System.out.println(client.isAvailable());
        System.out.println(client.isConnected());

//        File    file=new File("D:/160429.txt");
//        boolean flag= test.putFile(file,"/");
//        System.out.println(flag);

//        System.out.println(test.getFile(new File("160429.txt"), "D:/123/160429.txt"));

    }
}