在开发过程中,偶尔会遇到网络文件在FTP服务上存储着,对于这种情况想要下载到本地还有些麻烦,我们直接上世界上最简单的代码。
1.提前引入包
<!--hutool万能工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.4</version>
</dependency>
<!-- 网络编程 -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
2.逻辑代码实现
public static void main(String[] args) throws Exception{
String dir = "/";
String fileName = "xxx.flv";
Ftp ftp = new Ftp("192.168.1.99",21);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ftp.download(dir, fileName, byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
FileOutputStream fileOutputStream = new FileOutputStream("C:/Users/Administrator/Desktop/"+fileName);
fileOutputStream.write(bytes);
}