使用ftpclient从ftp服务器上传下载文件

  1. maven依赖
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.3</version>
</dependency>
  1. 代码如下
  public class RichUtil{
    private static Logger logger = LoggerFactory.getLogger(RichUtil.class); //slf4j
    public static FTPClient ftp;
private static boolean flag = false;

/**
 * 
 * @param path
 *            上传到ftp服务器哪个路径下
 * @param addr
 *            地址
 * @param port
 *            端口号
 * @param username
 *            用户名
 * @param password
 *            密码
 * @return
 * @throws Exception
 */
private static boolean connect(String path, String addr, int port,
        String username, String password) throws Exception {
    boolean result = false;
    ftp = new FTPClient();
    int reply;
    ftp.connect(addr, port);
    ftp.login(username, password);
    ftp.setFileType(FTPClient.BINARY\_FILE\_TYPE);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return result;
    }
    ftp.changeWorkingDirectory(path);
    result = true;
    return result;
}

/**
 * 连接ftp
 * 
 * @param type
 *            1:upload,2:download
 * @return
 */
private static boolean doConnect(int type) {
    try {
        String folder = "";
        if (type == 1) {
            // upload
            folder = "上传文件夹";

        } else if (type == 2) {
            // download
            folder = "下载文件夹";

        }
        String server = "ftp服务器IP";
        int port = "ftp端口号";
        String uname = "ftp用户名";
        String upwd = "ftp用户密码";

        return connect(folder, server, port, uname, upwd);
    } catch (Exception e) {
        logger.info("ftp服务器连接失败:" + e.getMessage());
        return false;
    }

}

/**
 * 关闭ftp连接
 */
private static void ftpDisconnect() {
    try {
        ftp.disconnect();
        flag=false;
    } catch (IOException e) {
        logger.info("ftp连接关闭失败,"+e.getMessage());
    }
}

/**
 * 
 * @param file
 *            上传的文件或文件夹
 * @throws Exception
 */
public static void upload(File file) throws Exception {
    if (flag == false) {
        boolean f = RichUtil.doConnect(1);
        flag = f;
    }
    if (flag == false)
        return; // deny

    /********* next step ****/
    if (file.isDirectory()) {
        ftp.makeDirectory(file.getName());
        ftp.changeWorkingDirectory(file.getName());
        String\[\] files = file.list();
        for (int i = 0; i < files.length; i++) {
            File file1 = new File(file.getPath() + File.separator + files\[i\]);
            if (file1.isDirectory()) {
                upload(file1);
                ftp.changeToParentDirectory();
            } else {
                File file2 = new File(file.getPath() + File.separator + files\[i\]);
                FileInputStream input = new FileInputStream(file2);
                ftp.storeFile(file2.getName(), input);
                input.close();
            }
        }
    } else {
        File file2 = new File(file.getPath());
        FileInputStream input = new FileInputStream(file2);
        ftp.storeFile(file2.getName(), input);
        input.close();
    }
    ftpDisconnect();
}

/**
 * 下载ftp图片
 * 
 * @param remoteFileName
 * @param localFile
 * @throws Exception
 */
public static void download(String remoteFileName, String localFile)
        throws Exception {
    if (flag == false) {
        boolean f = RichUtil.doConnect(2);
        flag = f;
    }
    if (flag == false)
        return; // deny

    FileOutputStream fos = null;

    try {  
        fos = new FileOutputStream(localFile); 
        ftp.setBufferSize(1024);
        // 设置文件类型(二进制)
        ftp.setFileType(FTPClient.BINARY\_FILE\_TYPE);
        ftp.retrieveFile(remoteFileName, fos);
    } catch (IOException e) {
        logger.info("ftp下载出错,"+e.getMessage()); 
    }finally {
        IOUtils.closeQuietly(fos);
        ftpDisconnect();
    }
}

}

0%