推荐一个java操作ftp的工具类

@

写在前面

作为经常使用电脑整理文件的童鞋,应该都使用过从ftp服务器上传下载文件,那么今天就了解下如何通过java程序操作ftp服务的文件

首先你要知道ftp的ip,路径,端口,有操作权限的账号和密码

1 导入jar包

 commons-net-3.6.jar

这个jar包用来设置编码,经过测试,不加也可用

2 工具类中主要方法

2.1 登陆ftp

	/**
	 * 验证登录
	 * @param ip
	 * @param port
	 * @param name
	 * @param pwd
	 * @return
	 */
	public boolean login(String ip,int port, String name, String pwd) {
		try {
			ftp = new FTPClient();
			ftp.connect(ip, port);
			System.out.println(ftp.login(name, pwd));
			if(!ftp.login(name, pwd)){
				return false;
			}
			ftp.setCharset(Charset.forName("UTF-8"));
			ftp.setControlEncoding("UTF-8");

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

注意:获取远程文件目录,上传和下载方法基于登陆方法

2.2 获取远程文件目录

	/**
	 * 获取ftp某一文件(路径)下的文件名字,用于查看文件列表
	 * @param ip
	 * @param port
	 * @param name
	 * @param pwd
	 * @param remotedir 远程地址目录
	 * @return
	 */
    public boolean getFilesName(String ip,int port, String name, String pwd, String remotedir) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //获取ftp里面,指定文件夹 里面的文件名字,存入数组中
            FTPFile[] files = ftp.listFiles(remotedir);
            //打印出ftp里面,指定文件夹 里面的文件名字
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
        return true;
    }

2.3 上传文件

	/**
     * 上传文件 方法一
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 远程地址文件路径
     * @param localpath 本地文件路径
     * @return
     */
    public boolean putFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //将本地的 localpath 文件上传到ftp的根目录文件夹下面,并重命名为 remotepath中的名字
        	 return ftp.storeFile(remotepath, new FileInputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
    }
    
    /**
     * 上传文件的第二种方法,优化了传输速度
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 远程地址文件路径
     * @param localpath 本地文件路径
     * @return
     */
    public boolean putFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            os = ftp.storeFileStream(remotepath);
            fis = new FileInputStream(new File(localpath));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
        	this.close();
		}
        return true;
    }

2.4 下载文件

	/**
     * 下载文件 方法一
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 远程地址文件路径
     * @param localpath 本地文件路径
     * @return
     */
    public boolean getFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
        	if(!login(ip, port, name, pwd)){
				return false;
			}
            //将ftp资源中 remotepath 文件下载到本地目录文件夹下面,并重命名为 localpath 中的名字
        	return ftp.retrieveFile(remotepath, new FileOutputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
        	this.close();
        }
    }
	
    /**
     * 下载文件的第二种方法,优化了传输速度
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 远程地址文件路径
     * @param localpath  本地文件路径
     * @return
     */
	public boolean getFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
		try {
			if(!login(ip, port, name, pwd)){
				return false;
			}
			is = ftp.retrieveFileStream(remotepath);
			fos = new FileOutputStream(new File(localpath));
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = is.read(b)) != -1) {
				fos.write(b,0,len);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}finally {
			this.close();
		}
		return true;
	}

3 源码

当然上面代码只是重要的部分,如果有问题可去github自行下载 charmsongo

如果有什么更好的方法欢迎留言

posted @ 2018-12-08 10:51  charmsongo  阅读(2602)  评论(0编辑  收藏  举报