com.eryansky.common.utils.ftp.FtpFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.eryansky.common.utils.ftp.FtpFactory.java

Source

/**
 *  Copyright (c) 2012-2014 http://www.eryansky.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.eryansky.common.utils.ftp;

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

import java.io.*;

/**
 * FTP Resource? <br>
 * @Resource(name = "payFtp")
 * 
 * @author &Eryan eryanwcp@gmail.com
 * @date 2012-3-20 ?1:30:39
 */
public class FtpFactory {

    // ?@Resource
    /**
     * ??
     */
    private String url;
    /**
     * FTP?
     */
    private int port;
    /**
     * ??
     */
    private String username;
    /**
     * ?
     */
    private String password;

    public FtpFactory() {
    }

    public FtpFactory(String url, int port, String username, String password) {
        this.url = url;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * ?FTP?.
     * 
     * @param path
     *            FTP??
     * @param filename
     *            FTP???
     * @param input
     *            ?
     * @return ?true?false
     */
    public boolean ftpUploadFile(String path, String filename, InputStream input) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("UTF-8");
        try {
            int reply;
            ftp.connect(url, port);
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            // 
            ftp.changeWorkingDirectory(path);
            ftp.setBufferSize(1024);
            ftp.setFileType(FTPClient.ASCII_FILE_TYPE);
            // 
            ftp.storeFile(filename, input);
            input.close();
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

    /**
     * ?FTP?.
     * 
     * @param path
     *            FTP??
     * @param filename
     *            FTP???
     * @param inputFilename
     *            ?
     * @return ?true?false
     */
    public boolean ftpUploadFile(String path, String filename, String inputFilename) {
        File file = new File(inputFilename);
        try {
            return ftpUploadFile(path, filename, new BufferedInputStream(new FileInputStream(file)));
        } catch (FileNotFoundException e) {
            return false;
        }
    }

    /**
     * FTP?.
     * 
     * @param remotePath
     *            FTP?
     * @param fileName
     *            ???
     * @param localPath
     *            ??
     * @return
     */
    public boolean ftpDownFile(String remotePath, String fileName, String localPath) {
        // ?
        boolean success = false;
        // FTPClient
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("GBK");
        try {
            int reply;
            // FTP?
            // ??ftp.connect(url)?FTP?
            ftp.connect(url, port);
            // ftp
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            // 
            ftp.changeWorkingDirectory(remotePath);
            // 
            FTPFile[] fs = ftp.listFiles();
            // ??
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    // ???
                    File localFile = new File(localPath + "/" + ff.getName());
                    // ?
                    OutputStream is = new FileOutputStream(localFile);
                    // 
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                    success = true;
                }
            }
            ftp.logout();
            // ?

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }
}