博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java实现FTP相关操作
阅读量:6927 次
发布时间:2019-06-27

本文共 3765 字,大约阅读时间需要 12 分钟。

hot3.png

/** * Created by 刘万祥 on 2016/4/29. * */public class FtpTool {    /*ftp用户名*/    private String userName;    /*ftp密码*/    private String password;    /*ftp服务器ip*/    private String ip;    private FTPClient ftpClient;    public FtpTool(String userName, String password, String ip){        this.userName = userName;        this.password = password;        this.ip = ip;    }        /**         * 上传文件         *         * @param  f 要上传的文件         * @param  uploadDir 上传文件的路径         *         * @return boolean b 上传结果         * */        public boolean putFile(File f,String uploadDir) {            InputStream instream = null;            boolean result = false;            try{                try{                    ftpClient.changeWorkingDirectory(uploadDir);                    instream = new BufferedInputStream(new FileInputStream(f));                    result = ftpClient.storeFile(f.getName(), instream);                }finally{                    if(instream!=null){                        instream.close();                    }                }            }catch(IOException e){                e.printStackTrace();            }            return result;        }        /**         * 从ftp服务器下载文件         *         * @param  f 要获取的ftp服务器上的文件         * @param  localPath 本地存放的路径         *         * @return boolean 文件下载是否成功         * */        public boolean getFile(File f  , String localPath){            OutputStream outStream = null;            boolean result = false;            try{                try{                    outStream = new BufferedOutputStream(new FileOutputStream(new File(localPath)));                    String path = f.getPath();                    path = path.replaceAll("\\\\", "/");                    String filepath = path.substring(0, path.lastIndexOf("/")+1)+"";                    String fileName = path.substring(path.lastIndexOf("/")+1)+"";                    boolean b = ftpClient.changeWorkingDirectory(filepath);                    if(b){                        result = ftpClient.retrieveFile(fileName, outStream);                    }                }finally{                    if(outStream != null){                        outStream.close();                    }                }            }catch(IOException e){                e.printStackTrace();            }            return result;        }        /**         * 获取ftp链接         *         * @return ftpClient         * */        public FTPClient getFTPClient(){            try {                ftpClient = new FTPClient();                ftpClient.connect(ip);                ftpClient.login(userName, password);            } catch (SocketException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }            return ftpClient;        }        /**         *  关闭ftpClient链接         *         *  @param ftpClient 要关闭的ftpClient对象         *         * */        public void closeFTPClient(FTPClient ftpClient){            try {                try{                    ftpClient.logout();                }finally{                    ftpClient.disconnect();                }            } catch (IOException e) {                e.printStackTrace();            }        }    public static void main(String[] args) {        FtpTool test=new FtpTool("liuwx","123","127.0.0.1");        FTPClient   client= test.getFTPClient();        System.out.println(client.isAvailable());        System.out.println(client.isConnected());//        File    file=new File("D:/160429.txt");//        boolean flag= test.putFile(file,"/");//        System.out.println(flag);//        System.out.println(test.getFile(new File("160429.txt"), "D:/123/160429.txt"));    }}

转载于:https://my.oschina.net/u/1170781/blog/670655

你可能感兴趣的文章
面试题整理_01
查看>>
CentOS7定制封装发行版-基于CentOS minimal
查看>>
windows的一大堆补丁,如何批量按装呢
查看>>
keepalived高可用lvs集群,主/备和主/主模型
查看>>
写在SQL注入后
查看>>
shell之函数
查看>>
hp 380G5 安装centos 7
查看>>
jsoncpp 用法
查看>>
linux 使用pssh批量部署tomcat
查看>>
使用Statistics命令查看Netapp存储实时性能统计数据
查看>>
计费程序(客户端)
查看>>
【系列8】使用Dockerfile创建带MongoDB的Centos Docker镜像
查看>>
shell 提取文件名和目录名的一些方法
查看>>
安装PostGIS-2.1.8
查看>>
使用putty远程linux服务
查看>>
SaltStack之sls文件
查看>>
Java程序性能优化(1)
查看>>
linux默认语言修改成中文,中文修改成英文
查看>>
nodejs 基础
查看>>
OpenStack入门修炼之环境准备(6)
查看>>