代码拉取完成,页面将自动刷新
内部封装了 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>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。