利用FtpClient实现上传下载及获得文件目录

    sun代码中有个FtpClient,虽然没有把它用做公开的工具包,但我们也还是可以拿它来利用一下.

 

/**
 * FTP文件上传与下载
 * notice:
 * 之所以每次都要连接一次ftp是让它的目录重新返回到相对的根目录,
 * 如果复用上次的FtpClient则可能它当前在FTP的目录不是我们想要的
 * 目录,所以在FTP上传下载文件时,最好每次都重新登录一下FTP
 * @author lgh
 */
public class FTPClient {

    private FtpClient ftpClient;
    private String ip;
    private int port;
    private String username;
    private String password;

    public FTPClient() {
    }

    public FTPClient(String ip, int port, String username, String password) {
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
    }

    /**
     * 需要备份的文件
     * @param list
     * @return
     */
    private List needBackFile(List list, String relativeName) {
        List fileNames = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String temp = (String) list.get(i);
            if (temp.indexOf(relativeName) > 0) {
                fileNames.add(temp);
            }
        }
        return fileNames;
    }

    public static void main(String[] args) {
        FTPClient client = new FTPClient(".....", 21, "...", "....");

        try {
//            client.downloadFile("CRM/ccbcrm/", "D://", "CRMClientLog.log", "CRMClientLog.log");
//            client.uploadFile("", "D://", "CRMClientLog.log");
            List list = client.getList("csrtestftp/网络/", false);
        } catch (Exception ex) {
            Logger.getLogger(FTPClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 关闭FTP连接 
     * @throws java.lang.Exception
     */
    public void closeServer() throws Exception {
        ftpClient.closeServer();
    }

    /**
     * 连接ftp服务器
     * @param ip
     * @param port
     * @param user
     * @param pwd
     * @return
     * @throws Exception
     */
    public boolean connectServer(String ip, int port, String user, String pwd)
            throws Exception {
        boolean isSuccess = false;
        try {
            ftpClient = new FtpClient();
            ftpClient.openServer(ip, port);
            ftpClient.login(user, pwd);
            isSuccess = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return isSuccess;
    }

    /**
     * 获得远程下的目录
     * @param remotePath 远程目录
     * @param fullPath 是否需要完整路径
     * @return
     */
    public List getList(String remotePath, boolean fullPath) {
        List list = new ArrayList();
        try {
            if (connectServer(ip, port, username, password)) {
                BufferedReader br = new BufferedReader(new InputStreamReader(ftpClient.nameList(remotePath)));
                String temp = ftpClient.getResponseString();
                System.out.println(temp);
                String readLine = null;
                int lastIndex;
                if ((lastIndex = remotePath.lastIndexOf("/")) > 0||(lastIndex = remotePath.lastIndexOf("//")) > 0
                    ||(lastIndex = remotePath.lastIndexOf("\\")) > 0||(lastIndex = remotePath.lastIndexOf(File.separator)) > 0) {
                    remotePath = remotePath.substring(0, lastIndex);
                }   //去掉remotePath的后缀,可能是'/',也有可能是其他符号
                while ((readLine = br.readLine()) != null) {

                    if (!fullPath) {
                        list.add(readLine.substring(remotePath.length() + 1, readLine.length()));
                        System.out.println(readLine.substring(remotePath.length() + 1, readLine.length()));
                    } else {
                        list.add(readLine);
                        System.out.println(readLine);
                    }
                }
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    /**
     * 下载文件
     * @param remotePath
     * @param localPath
     * @param filename
     * @throws Exception
     */
    public void downloadFile(String remotePath, String localPath, String remoteFileName, String localFileName) throws Exception {
        try {
            if (connectServer(ip, port, username, password)) {
                if (remotePath.length() != 0) {
                    ftpClient.cd(remotePath);
                }
                ftpClient.binary();
                TelnetInputStream is = ftpClient.get(remoteFileName);
                File fp = new File(localPath);
                if (!fp.exists()) {
                    fp.mkdirs();
                }
                File file_out = new File(localPath + File.separator + localFileName);
                FileOutputStream os = new FileOutputStream(file_out);
                byte[] bytes = new byte[1024];
                int readBye;
                while ((readBye = is.read(bytes)) != -1) {
                    os.write(bytes, 0, readBye);
                }
                is.close();
                os.close();
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 上传文件
     * @param remotePath
     * @param localPath
     * @param filename
     * @throws Exception
     */
    public void uploadFile(String remotePath, String localPath, String filename) throws Exception {
        try {
            if (connectServer(ip, port, username, password)) {
                if (remotePath.length() != 0) {
                    ftpClient.cd(remotePath);
                }
                ftpClient.binary();
                TelnetOutputStream os = ftpClient.put(filename);
                File file_in = new File(localPath + File.separator + filename);
                FileInputStream is = new FileInputStream(file_in);
                byte[] bytes = new byte[1024];
                int readBye;
                while ((readBye = is.read(bytes)) != -1) {
                    os.write(bytes, 0, readBye);
                }
                is.close();
                os.close();
                ftpClient.closeServer();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值