zoukankan      html  css  js  c++  java
  • Java FTP 基本操作

    最近工作中用到了 FTP 相关的操作,所以借此机会了解了下具体内容。

    FTP基础

    关于 FTP 基础推荐阅读《使用 Socket 通信实现 FTP 客户端程序》,其中需要特别注意的是主动模式和被动模式,这一部分在日常使用中经常被忽略,但生产环境中可能会出问题,关键在于防火墙对端口的控制。

    • 主动模式:服务器采用 21 和 20 端口,客户端采用大于 1024 的随机端口,连接指令和文件传输指令由服务端发送。
    • 被动模式:服务端采用 21 和大于 1024 的随机端口,客户端采用大于 1024 的随机端口,连接指令由客户端发送。

    程序操作 FTP 过程在上面推荐的文章中有所提及,大家可以看到过程还是比较复杂的,不过好在有 apache 的 commons-net 给我们提供了相关的工具类可以使用,本文使用的是 3.6 版本。以下通过代码进行说明,此代码仅演示功能,很多地方并不完善,如果用作生产请自行修改。

    Java FTP 上传

    /**
     * FTP发送至目标服务器
     * @apiNote 依赖apache commons-net 包
     * @param server
     */
    public static void sendToServerByFTP(String server, int port, String username, String password, 
                String encoding, String fileLocalPath, String fileRemotePath, String fileRemoteName) throws IOException {
        // 获取 FTPClient
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(username, password);
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            System.out.println("connected failed");
        }
    
        // 设置编码,当文件中存在中文且上传后文件乱码时可使用此配置项
        //ftpClient.setControlEncoding(encoding);
        // 切换为本地被动模式,可以解决FTP上传后文件为空的问题,但需要服务器将FTP服务添加至防火墙白名单
        //ftpClient.enterLocalPassiveMode();
    
        // 切换到指定目录
        ftpClient.changeWorkingDirectory(fileRemotePath);
    
        // 获取文件并上传
        File file = new File(fileLocalPath);
        InputStream inputStream = new FileInputStream(file);
    
        //文件名为中文名且上传后出现乱码时启用此项
        //String fileName = new String(fileRemoteName.getBytes(encoding), "ISO8859-1");
        boolean flag = ftpClient.storeFile(fileRemoteName, inputStream);
    
        // 关闭已占用资源
        inputStream.close();
        ftpClient.logout();
    }
    

    FTP 下载

    FTP 下载和上传基本步骤类似,依赖的方法由 storeFile 变为 retrieveFile

    public void downloadFile(String server, int port, String username, String password, 
                String serverPath, String localPath, String fileName) throws IOException {
        // 登录
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(username, password);
    
        // 验证登录情况
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            throw new RuntimeException("登录FTP服务器失败,错误代码:" + replyCode);
        }
        
        // 切换服务器至目标目录
        ftpClient.changeWorkingDirectory(serverPath);
    
        // 下载文件
        File file = new File(localPath);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ftpClient.retrieveFile(fileName, fileOutputStream);
        
        // 关闭资源占用
        fileOutputStream.close();
        ftpClient.logout();
    }
    

    FTP 删除

    public void deleteFile(String server, int port, String username, String password, 
                String serverPath, String fileName) throws IOException {
        // 登录
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(username, password);
    
        // 验证登录情况
        int replyCode = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(replyCode)) {
            throw new RuntimeException("登录FTP服务器失败,错误代码:" + replyCode);
        }
    
        ftpClient.changeWorkingDirectory(serverPath);
    
        ftpClient.deleteFile(fileName);
    }
    
  • 相关阅读:
    A1023 Have Fun with Numbers (20分)(大整数四则运算)
    A1096 Consecutive Factors (20分)(质数分解)
    A1078 Hashing (25分)(哈希表、平方探测法)
    A1015 Reversible Primes (20分)(素数判断,进制转换)
    A1081 Rational Sum (20分)
    A1088 Rational Arithmetic (20分)
    A1049 Counting Ones (30分)
    A1008 Elevator (20分)
    A1059 Prime Factors (25分)
    A1155 Heap Paths (30分)
  • 原文地址:https://www.cnblogs.com/aotian/p/9552110.html
Copyright © 2011-2022 走看看