zoukankan      html  css  js  c++  java
  • java实现连接ftp服务器并下载文件到本地

    1.pom.xml引入jar包

            <!--ftp-->
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>3.6</version>
            </dependency>

    2.连接ftp服务器方法

         /**
         * 连接ftp服务器
         * @param ip  ftp地址
         * @param port  端口
         * @param username 账号
         * @param password 密码
         * @return
         * @throws IOException
         */
        public static FTPClient ftpConnection(String ip,String port, String username, String password) throws IOException {
            FTPClient ftpClient = new FTPClient();
            try {
                ftpClient.connect(ip, Integer.parseInt(port));
                ftpClient.login(username, password);
                int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
                if(!FTPReply.isPositiveCompletion(replyCode)) {
                    ftpClient.disconnect();
                    logger.error("--ftp连接失败--");
                    System.exit(1);
                }
                ftpClient.enterLocalPassiveMode();//这句最好加告诉对面服务器开一个端口
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ftpClient;
        }

    3.断开ftp服务方法

        /**
         * 断开FTP服务器连接
         * @param ftpClient  初始化的对象
         * @throws IOException
         */
        public static void close(FTPClient ftpClient) throws IOException{
            if(ftpClient!=null && ftpClient.isConnected()){
                ftpClient.logout();
                ftpClient.disconnect();
            }
        }

    4.下载ftp服务器知道路径的文件到本地方法

         /**
         * 下载ftp服务器文件方法
         * @param ftpClient FTPClient对象
         * @param newFileName 新文件名
         * @param fileName 原文件(路径+文件名)
         * @param downUrl  下载路径
         * @return
         * @throws IOException
         */
        public static boolean downFile(FTPClient ftpClient, String newFileName, String fileName, String downUrl) throws IOException {
            boolean isTrue = false;
            OutputStream os=null;
            File localFile = new File(downUrl + "/" + newFileName);
            if (!localFile.getParentFile().exists()){//文件夹目录不存在创建目录
                localFile.getParentFile().mkdirs();
                localFile.createNewFile();
            }
            os = new FileOutputStream(localFile);
            isTrue = ftpClient.retrieveFile(new String(fileName.getBytes(),"ISO-8859-1"), os);
            os.close();
            return isTrue;
        }

    5.调用测试main

    public static void main(String[] args) throws IOException{
            FTPClient ftpClient = this.ftpConnection("172.*.*.*","*","username","password");boolean flag = downFile(ftpClient,"文件名","/路径/+文件名","本地路径");
            close(ftpClients);
            System.out.println(flag );//flag=true说明下载成功
        }
  • 相关阅读:
    某大神C#框架后台发送信息的查找及破解
    多平台下Modbus通信协议库的设计(一)
    wpf 窗口程序下将datagrid导出为excel
    luogu P2598 [ZJOI2009]狼和羊的故事 |网络流最小割
    luogu P3171 [CQOI2015]网络吞吐量 |网络流最大流
    luogu P2469 [SDOI2010]星际竞速 |网络流费用流
    luogu P2172 [国家集训队]部落战争 |网络流最少路径覆盖
    luogu P2045 方格取数加强版 |最大费用最大流
    luogu P6327 区间加区间sin和 |线段树
    luogu P2402 奶牛隐藏 |网络流最大流+二分
  • 原文地址:https://www.cnblogs.com/xianshen/p/13084681.html
Copyright © 2011-2022 走看看