# commons-net **Repository Path**: seaboot/commons-net ## Basic Information - **Project Name**: commons-net - **Description**: 就是单纯的二次封装,目的不在于封装了多么强劲的功能,而是为了方便维护, 在项目的代码, 与第三方框架之间,建立一个缓冲地带, 如果有一天,第三方框架需要进行升级,我们只需要少量的调整即可。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-09-22 - **Last Updated**: 2025-08-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: HttpClient, FTP ## README # commons-net 内部封装了 ftp 协议与 http 协议的工具代码。 #### 简介 就是单纯的二次封装,目的不在于封装了多么强劲的功能,而是为了方便维护, 在项目的代码, 与第三方框架之间,建立一个缓冲地带, 如果有一天,第三方框架需要进行升级,我们只需要少量的调整即可。 Apache 的 Httpclient 的问题尤为明显,在 3 到 4 的版本切换过程中,我们几乎重构了全部的代码。 当然,使用这一套代码,肯定也能节省更多的代码。 ```java /** * @author Mr.css * @version 2022-11-14 10:23 */ public class FtpExample { public static void main1(String[] args) throws IOException { FtpProperties ftpProperties = new FtpProperties(); ftpProperties.setHost("127.0.0.1"); ftpProperties.setPassword("djw"); ftpProperties.setUsername("djw"); FtpPooledSource ftpSource = new FtpClientPooled(ftpProperties); String[] names; try (Ftp ftp = ftpSource.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 = ftpSource.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()); FtpSource ftpSource = new FtpSource(properties); Ftp ftp = ftpSource.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); } } } ``` #### 依赖 ```xml org.apache.httpcomponents httpclient 4.5.13 org.apache.httpcomponents httpmime 4.5.13 commons-net commons-net 3.8.0 org.apache.commons commons-pool2 2.8.0 ```