1 Star 1 Fork 0

疯狂的妞妞 / commons-net

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

commons-net

内部封装了 ftp 协议与 http 协议的工具代码。

简介

就是单纯的二次封装,目的不在于封装了多么强劲的功能,而是为了方便维护, 在项目的代码, 与第三方框架之间,建立一个缓冲地带, 如果有一天,第三方框架需要进行升级,我们只需要少量的调整即可。

Apache 的 Httpclient 的问题尤为明显,在 3 到 4 的版本切换过程中,我们几乎重构了全部的代码。

当然,使用这一套代码,肯定也能节省更多的代码。


/**
 * @author Mr.css
 * @version 2022-11-14 10:23
 */
public class TestFtp {

    public static void main1(String[] args) throws IOException {
        FtpProperties ftpProperties = new FtpProperties();
        ftpProperties.setHost("127.0.0.1");
        ftpProperties.setPassword("djw");
        ftpProperties.setUsername("djw");
		FtpClientPooled ftpClient = new FtpClientPooled(ftpProperties);

        String[] names;
        try (Ftp ftp = ftpClient.build()) {
            System.out.println(ftp.pwd());
            names = ftp.cd("ftp/hes").listNames();
            System.out.println(Arrays.toString(names));
        }

        File root = new File("C:\\Users\\ASUS\\Desktop\\test");
        int cnt = 1;
        while (cnt-- > 0) {
            int finalCnt = cnt;
            new Thread(() -> {
                for (String name : names) {
                    File out = new File(root, finalCnt + "_" + name);
                    try (Ftp ftp = ftpClient.build();
                         FileOutputStream os = new FileOutputStream(out)) {
                        ftp.cd("ftp/hes").download(name, os);
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println(finalCnt);
                    }
                }
            }).start();
        }
    }

    public static void main(String[] args) throws IOException {
        FtpProperties properties = new FtpProperties();
        properties.setHost("127.0.0.1");
        properties.setPort(21);
        properties.setUsername("djw");
        properties.setPassword("djw");
        properties.setPassiveMode(true);
        properties.setEncoding(Charset.defaultCharset().name());

        FtpClient ftpClient = new FtpClient(properties);

        Ftp ftp = ftpClient.build();

        System.out.println(ftp.pwd());
        String[] names = ftp.listNames();
        System.out.println(Arrays.toString(names));

        ftp.cd("ftp");

        try(FileOutputStream os = new FileOutputStream(new File("C:\\Users\\ASUS\\Desktop\\testtest.xls"))){
            ftp.download("abc20230706094050.xlsx", os);
        }
    }
}

依赖

<dependencies>
    <!--http-->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.13</version>
    </dependency>

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

    <!--apache连接池-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.8.0</version>
    </dependency>
</dependencies>
MIT License Copyright (c) 2022 Mr.css Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

就是单纯的二次封装,目的不在于封装了多么强劲的功能,而是为了方便维护, 在项目的代码, 与第三方框架之间,建立一个缓冲地带, 如果有一天,第三方框架需要进行升级,我们只需要少量的调整即可。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/seaboot/commons-net.git
git@gitee.com:seaboot/commons-net.git
seaboot
commons-net
commons-net
master

搜索帮助