• 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);
    }
    
  • 相关阅读:
    HTML中使用Vue+Dhtmlxgantt制作任务进度图
    Vue中使用js-xlsx导出Data数据到Excel
    Vue生命周期
    ajax调用免费的天气API
    maven无法自动下载依赖包settings.xml文件配置
    idea打开项目没有src目录
    java jdk idea maven安装及配置
    CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://conda.anaconda.org/pytorch
    The procedure entry point OPENSSL_sk_new_reserve could not be located in the dynamic link library
    Nuget打包没有注释显示
  • 原文地址:https://www.cnblogs.com/aotian/p/9552110.html
Copyright © 2020-2023  润新知