资讯专栏INFORMATION COLUMN

java 写的 ftp 下载工具

leone / 1626人阅读

摘要:需要用到写一个的工具,因为只有一点点基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。对于下载工具,代码如下这个工具是为了配合另外一个工具做集群上传用的,所以里面的把和流分开了,也是为了方便另外一个工具使用。

需要用到 java 写一个 ftp 的工具,因为只有一点点 java 基础,但是由于好几年不用,几乎算是不会了,只好一点点来搞,还好能捡起来。

不过因为是在 Linux 下使用 javac 编译,不是在 WIN 下使用 IDE 来做这些事情,所以在运行和编译上又费了一些时间,不过正是因为这样对 JAVA 的一些编译、运行的知识又了解了一些。

对于 ftp 下载工具,代码如下:

import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.net.SocketException;   

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


public class FtpClient {
    private String         host;   
    private int            port;   
    private String         username;   
    private String         password;   

    private boolean        binaryTransfer = true;   
    private boolean        passiveMode    = true;   
    private String         encoding       = "UTF-8";   
    private int            clientTimeout  = 3000;   
    private boolean flag=true;
    private FTPClient ftpClient = null;

    public String getHost() {   
        return host;   
    }   

    public void setHost(String host) {   
        this.host = host;   
    }   

    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;   
    }   

    public boolean isBinaryTransfer() {   
        return binaryTransfer;   
    }   

    public void setBinaryTransfer(boolean binaryTransfer) {   
        this.binaryTransfer = binaryTransfer;   
    }   

    public boolean isPassiveMode() {   
        return passiveMode;   
    }   

    public void setPassiveMode(boolean passiveMode) {   
        this.passiveMode = passiveMode;   
    }   

    public String getEncoding() {   
        return encoding;   
    }   

    public void setEncoding(String encoding) {   
        this.encoding = encoding;   
    }   

    public int getClientTimeout() {   
        return clientTimeout;   
    }   

    public void setClientTimeout(int clientTimeout) {   
        this.clientTimeout = clientTimeout;   
    }   

    public FtpClient(String Host) {
        this.username = "anonymous";
        this.encoding = "utf-8";
        this.binaryTransfer = true;
        this.binaryTransfer = true;
        this.port = 21;
        this.host = Host;
        try {
            this.ftpClient = getFTPClient();
        } catch (Exception e) {
            System.out.println("Create FTPClient error!");
        }
    }

    private FTPClient getFTPClient() throws IOException {   
        FTPClient ftpClient = new FTPClient(); 
        ftpClient.setControlEncoding(encoding);

        connect(ftpClient);
        if (passiveMode) {   
            ftpClient.enterLocalPassiveMode();   
        }   
        setFileType(ftpClient); 

        try {   
            ftpClient.setSoTimeout(clientTimeout);   
        } catch (SocketException e) {   
            throw new IOException("Set timeout error.", e);   
        }   

        return ftpClient;   
    }   

    private void setFileType(FTPClient ftpClient) throws IOException {   
        try {   
            if (binaryTransfer) {   
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);   
            } else {   
                ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE);   
            }   
        } catch (IOException e) {   
            throw new IOException("Could not to set file type.", e);   
        }   
    }   

    public boolean connect(FTPClient ftpClient) throws IOException {   
        try {   
            ftpClient.connect(host, port);   

            int reply = ftpClient.getReplyCode();   

            if (FTPReply.isPositiveCompletion(reply)) {   
                if (ftpClient.login(username, password)) {   
                    setFileType(ftpClient);   
                    return true;   
                }   
            } else {   
                this.ftpClient.disconnect();   
                throw new IOException("FTP server refused connection.");   
            }   
        } catch (IOException e) {   
            if (this.ftpClient.isConnected()) {   
                try {   
                    this.ftpClient.disconnect();
                } catch (IOException e1) {   
                    throw new IOException("Could not disconnect from server.", e);   
                }   

            }   
            throw new IOException("Could not connect to server.", e);   
        }   
        return false;   
    }   

    private void disconnect() throws IOException {   
        try {   
            this.ftpClient.logout();   
        } catch (IOException e) {   
            System.out.println("logout may timeout!");
        } finally {
            if (this.ftpClient.isConnected()) {   
                this.ftpClient.disconnect();   
            }   
        }  
    }   

    public InputStream getStream(String serverFile) throws IOException {
        InputStream inStream = null;
        try {
            inStream = this.ftpClient.retrieveFileStream(serverFile);
            System.out.println("inStream get over!");
            return inStream;
        } catch (IOException e) {
            System.out.println("get stream exception");
            return null;
        }
    }

    public boolean writeStream(InputStream input, String localFile) throws IOException {
        FileOutputStream fout = new FileOutputStream(localFile);
        int ch = 0;
        if(input == null){
            System.out.println("input is null");
            return false;
        }
        try {
            ch = input.read();
            while(ch != -1){
                fout.write(ch);
                ch = input.read();
            }
            System.out.println("write over!");
            return flag;
        } catch (IOException e) {
            throw new IOException("Couldn"t get file from server.", e);
        } 
    }

    public boolean isExist(String remoteFilePath)throws IOException{

        try{
            File file=new File(remoteFilePath);

            String remotePath=remoteFilePath.substring(0,(remoteFilePath.indexOf(file.getName())-1));
            String[] listNames = this.ftpClient.listNames(remotePath);   
            System.out.println(remoteFilePath);
            for(int i=0;i

这个工具是为了配合另外一个 Hadoop 工具做 集群上传用的,所以里面的把 input 和 output 流分开了,也是为了方便另外一个工具使用。

补充一点,如何在 linux 配置运行:

如果这样的代码需要在 linux 下环境运行,首先要配置好响应的包,例如

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

这个包在 apache 的网站上直接下载就行,解压后找到对应的 jar 包,在编译的时候进行引用:

export FTPPATH="${路径}/xxx.jar"
javac -classpath $CLASSPATH:$FTPPATH FtpClient.java

同样,在运行的时候也要指定 classpath:

java -classpath $CLASSPATH:$FTPPATH FtpClient

建议不要把$FTPPATH 包含在 CLASSPATH 中,用什么包就引用什么环境变量就行了,没必要一股脑都添加进去,就像我们没必要 import 所有的包一样。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64279.html

相关文章

  • 一、python与pycharm的安装

    摘要:是面向对象语言这意味着支持面向对象的风格或代码封装在对象的编程技术。在上执行命令,就可以进入到的交互模式,并显示出版本等信息。选择的版本,需要下载安装包,然后进行安装。 一、Python简介 Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。 Python 是交互式语言: 这意味着,您可以在一个Python提示符,直接互动执行写你的程...

    awokezhou 评论0 收藏0
  • HADOOP集群文件上传下载

    摘要:对上的文件进行上传和下载是对集群的基本操作,在权威指南一书中,对文件的上传和下载都有代码的实例,但是对如何配置客户端却是没有讲得很清楚,经过长时间的搜索和调试,总结了一下,如何配置使用集群的方法,以及自己测试可用的对集群上的文件进行操作的程 对HDFS上的文件进行上传和下载是对集群的基本操作,在《HADOOP权威指南》一书中,对文件的上传和下载都有代码的实例,但是对如何配置HADOOP...

    nevermind 评论0 收藏0
  • Nginx 搭建图片服务器

    摘要:搭建图片服务器本章内容通过和搭建图片服务器。第二个部分是为了更好的体验上传,批量上传,回显功能的富文本编辑器。总结搭建服务器的思维实现上传图片的功能上传图片的功能源码搭建图片服务器到这里就结束了,有什么不足的地方,请赐教。 Nginx 搭建图片服务器 本章内容通过Nginx 和 FTP 搭建图片服务器。在学习本章内容前,请确保您的Linux 系统已经安装了Nginx和Vsftpd。 N...

    jas0n 评论0 收藏0
  • 【译】PHP:40+开发工具推荐

    摘要:今天,就为开发者介绍个方便的工具。对开发者来说,是一个非常有用的工具,它提供了超过个有用的函数。该工具检查输入源代码和报告任何违反给定的标准。框架是一个开发的工具。它侧重于安全性和性能,绝对是最安全的开发框架之一。 PHP是为Web开发设计的服务器脚本语言,但也是一种通用的编程语言。超过2.4亿个索引域使用PHP,包括很多重要的网站,例如Facebook、Digg和WordPress。...

    dreambei 评论0 收藏0

发表评论

0条评论

leone

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<