首页 > 其他 > 详细

使用FTPClient连接文件服务器并做相应操作(代码)

时间:2015-11-13 19:30:33      阅读:332      评论:0      收藏:0      [点我收藏+]

没有写摘要的习惯,就直接在这里写了。

首先搭建一个文件服务器,参考http://my.oschina.net/simpleton/blog/530081

然后就是客户端工具类代码


/**
 * 
 */
package com.common;

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

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

/**
 * FTP客户端工具类
 * @author luolin
 *
 * @version $id:FTPClientUtils.java,v 0.1 2015年11月13日 下午4:18:07 luolin Exp $
 */
public class FTPClientUtil {
    /** FTP客户端实例 */
    private FTPClient ftpClient;

    /**
     * 私有构造器
     */
    private FTPClientUtil() {
    }

    /**
     * 内部类维护单例,防止并发问题
     * @author luolin
     *
     * @version $id:FTPClientExample.java,v 0.1 2015年11月13日 下午2:34:08 luolin Exp $
     */
    private static class SingletonFactory {
        private static FTPClientUtil instance = new FTPClientUtil();
    }

    /**
     * 获取实例
     * @return {@link FTPClientExample}
     */
    public static FTPClientUtil getInstance() {
        return SingletonFactory.instance;
    }

    /**
     * 连接文件服务器
     * @param addr 文件服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @throws Exception 
     */
    public void connect(String addr, int port, String username, String password) throws Exception {
        ftpClient = new FTPClient();
        // 连接
        ftpClient.connect(addr, port);
        // 登录
        ftpClient.login(username, password);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        // 判断文件服务器是否可用??
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            ftpClient.disconnect();
        }
    }

    /**
     * 连接文件服务器
     * @param addr 文件服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param workingDirectory 目标连接工作目录
     * @throws Exception 
     */
    public void connect(String addr, int port, String username, String password, String workingDirectory)
                                                                                                         throws Exception {
        connect(workingDirectory, port, workingDirectory, workingDirectory);
        changeWorkingDirectory(workingDirectory);
    }

    /**
     * 关闭连接,使用完连接之后,一定要关闭连接,否则服务器会抛出 Connection reset by peer的错误
     * @throws IOException
     */
    public void closeConnection() throws IOException {
        ftpClient.disconnect();
    }

    /**
     * 切换工作目录
     * @param directory 目标工作目录
     * @throws IOException
     */
    public void changeWorkingDirectory(String directory) throws IOException {
        // 切换到目标工作目录
        if (!ftpClient.changeWorkingDirectory(directory)) {
            ftpClient.makeDirectory(directory);
            ftpClient.changeWorkingDirectory(directory);
        }
    }

    /** 
     * @param file 上传的文件或文件夹 
     * @throws Exception 
     */
    public void upload(File file) throws Exception {

        if (file == null) {
            throw new RuntimeException("上传文件为空");
        }
        // 是文件,直接上传
        if (!file.isDirectory()) {
            storeFile(new File(file.getPath()));
            return;
        }

        ftpClient.makeDirectory(file.getName());
        ftpClient.changeWorkingDirectory(file.getName());
        // 文件夹,递归上传所有文件
        for (File item : file.listFiles()) {
            if (!item.isDirectory()) {
                storeFile(item);
                continue;
            }
            upload(item);
            ftpClient.changeToParentDirectory();
        }
    }

    /**
     * 删除文件
     * @param fileName 要删除的文件地址
     * @return true/false
     * @throws Exception
     */
    public boolean delete(String fileName) throws Exception {
        return ftpClient.deleteFile(fileName);
    }

    /**
     * 存储文件
     * @param file {@link File}
     * @throws Exception
     */
    private void storeFile(File file) throws Exception {
        FileInputStream input = new FileInputStream(file);
        ftpClient.storeFile(file.getName(), input);
        input.close();
    }

    /**
     * 下载文件到指定目录
     * @param ftpFile 文件服务器上的文件地址
     * @param dstFile 输出文件的路径和名称
     * @throws Exception 
     */
    public void downLoad(String ftpFile, String dstFile) throws Exception {
        File file = new File(dstFile);
        FileOutputStream fos = new FileOutputStream(file);
        ftpClient.retrieveFile(ftpFile, fos);
        fos.flush();
        fos.close();
    }

    /**
     * 从文件服务器获取文件流
     * @param ftpFile 文件服务器上的文件地址
     * @return {@link InputStream}
     * @throws IOException
     */
    public InputStream retrieveFileStream(String ftpFile) throws IOException {
        return ftpClient.retrieveFileStream(ftpFile);
    }
}



下面也给出我测试的时候,写的demo的代码


/**
 * 
 */
package com.eay.ftp;

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

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

/**
 * FTP客户端工具类示例
 * @author ll
 *
 * @version $id:FTPClientExample.java,v 0.1 2015年11月13日 上午11:32:24 ll Exp $
 */
public class FTPClientExample {

    /** FTP客户端实例 */
    private FTPClient ftpClient;

    /**
     * 私有构造器
     */
    private FTPClientExample() {
    }

    /**
     * 内部类维护单例,防止并发问题
     * @author luolin
     *
     * @version $id:FTPClientExample.java,v 0.1 2015年11月13日 下午2:34:08 luolin Exp $
     */
    private static class SingletonFactory {
        private static FTPClientExample instance = new FTPClientExample();
    }

    /**
     * 获取实例
     * @return {@link FTPClientExample}
     */
    public static FTPClientExample getInstance() {
        return SingletonFactory.instance;
    }

    /**
     * 连接文件服务器
     * @param addr 文件服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @throws Exception 
     */
    public void connect(String addr, int port, String username, String password) throws Exception {
        ftpClient = new FTPClient();
        // 连接
        ftpClient.connect(addr, port);
        // 登录
        ftpClient.login(username, password);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        // 判断文件服务器是否可用??
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            ftpClient.disconnect();
        }
    }

    /**
     * 连接文件服务器
     * @param addr 文件服务器地址
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param workingDirectory 目标连接工作目录
     * @throws Exception 
     */
    public void connect(String addr, int port, String username, String password, String workingDirectory)
                                                                                                         throws Exception {
        ftpClient = new FTPClient();
        // 连接
        ftpClient.connect(addr, port);
        // 登录
        ftpClient.login(username, password);
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        // 判断文件服务器是否可用??
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            ftpClient.disconnect();
        }
        changeWorkingDirectory(workingDirectory);
    }

    /**
     * 关闭连接,使用完连接之后,一定要关闭连接,否则服务器会抛出 Connection reset by peer的错误
     * @throws IOException
     */
    public void closeConnection() throws IOException {
        ftpClient.disconnect();
    }

    /**
     * 切换工作目录
     * @param directory 目标工作目录
     * @throws IOException
     */
    public void changeWorkingDirectory(String directory) throws IOException {
        // 切换到目标工作目录
        if (!ftpClient.changeWorkingDirectory(directory)) {
            ftpClient.makeDirectory(directory);
            ftpClient.changeWorkingDirectory(directory);
        }
    }

    /** 
     * @param file 上传的文件或文件夹 
     * @throws Exception 
     */
    public void upload(File file) throws Exception {

        if (file == null) {
            throw new RuntimeException("上传文件为空");
        }
        // 是文件,直接上传
        if (!file.isDirectory()) {
            storeFile(new File(file.getPath()));
            return;
        }

        ftpClient.makeDirectory(file.getName());
        ftpClient.changeWorkingDirectory(file.getName());
        // 文件夹,递归上传所有文件
        for (File item : file.listFiles()) {
            if (!item.isDirectory()) {
                storeFile(item);
                continue;
            }
            upload(item);
            ftpClient.changeToParentDirectory();
        }
    }

    /**
     * 删除文件
     * @param fileName 要删除的文件地址
     * @return true/false
     * @throws Exception
     */
    public boolean delete(String fileName) throws Exception {
        return ftpClient.deleteFile(fileName);
    }

    /**
     * 存储文件
     * @param file {@link File}
     * @throws Exception
     */
    private void storeFile(File file) throws Exception {
        FileInputStream input = new FileInputStream(file);
        ftpClient.storeFile(file.getName(), input);
        input.close();
    }

    /**
     * 下载文件
     * @param ftpFile 文件服务器上的文件地址
     * @param dstFile 输出文件的路径和名称
     * @throws Exception 
     */
    public void downLoad(String ftpFile, String dstFile) throws Exception {
        File file = new File(dstFile);
        FileOutputStream fos = new FileOutputStream(file);
        ftpClient.retrieveFile(ftpFile, fos);
        fos.flush();
        fos.close();
    }

    /**
     * 从文件服务器获取文件流
     * @param ftpFile 文件服务器上的文件地址
     * @return {@link InputStream}
     * @throws IOException
     */
    public InputStream retrieveFileStream(String ftpFile) throws IOException {
        return ftpClient.retrieveFileStream(ftpFile);
    }

    public static void main(String[] args) throws Exception {
        FTPClientExample emp = FTPClientExample.getInstance();
        String addr = "192.168.111.60";
        int port = 21;
        String username = "admin";
        String password = "admin";
        emp.connect(addr, port, username, password);

        // 上传文件
        emp.changeWorkingDirectory("bss");
        emp.upload(new File("E:\\example.txt"));
        // 下载文件到指定目录
        //        emp.downLoad("bss\\example.txt", "E:\\example2.txt");

        // 删除文件
        //        emp.delete("bss\\example.txt");

        // 关闭连接,防止文件服务器抛出 Connection reset by peer的错误
        emp.closeConnection();

    }
}



使用FTPClient连接文件服务器并做相应操作(代码)

原文:http://my.oschina.net/simpleton/blog/530084

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!