JAVA实现FTP文件及目录

使用开源项目:ftp4j

http://www.sauronsoftware.it/projects/ftp4j/

 

import
 java.io.File;

import
 it.sauronsoftware.ftp4j.FTPClient;

import
 it.sauronsoftware.ftp4j.FTPFile;


public
 
class
 JavaFtp {

private
 FTPClient client 
=
null
;

public
 
void
 init(){

try
 {
client 
=
 
new
 FTPClient();
client.connect(
"
192.168.1.248
"
, 
21
);
client.login(
"
db2inst1
"
, 
"
db2inst1
"
);
} 
catch
 (Exception e) {
e.printStackTrace();
}
}

public
 
static
 
void
 main(String[] args) {

try
 {
JavaFtp javaFtp
=
new
 JavaFtp();
javaFtp.init();
javaFtp.download(
"
E://test
"
, 
"
/test/
"
);
javaFtp.close();
} 
catch
 (Exception e) {
e.printStackTrace();
}
}

public
 
void
 download(String localpath,String clientpath){

try
 { 

if
(clientpath
!=
null
&&
clientpath.length()
>=
0
){
client.changeDirectory(clientpath);
}
FTPFile[] list 
=
 client.list();


for
(
int
 i
=
0
;i
<
list.length;i
++
){
FTPFile file 
=
 list[i];
System.out.println(file.getType());

if
(file.getType()
==
FTPFile.TYPE_DIRECTORY){
File temp
=
new
 File(localpath
+
File.separator
+
file.getName());

if
(
!
temp.exists()){
temp.mkdir();
}
download(localpath
+
File.separator
+
file.getName(),file.getName());
}

else
 
if
(file.getType()
==
FTPFile.TYPE_FILE){
File localfile
=
new
 File(localpath
+
File.separator
+
file.getName());
client.download(file.getName(), localfile);
}
} 
} 
catch
 (Exception e) {
e.printStackTrace();
}
}

public
 
void
 close(){

try
 { 
client.disconnect(
true
);
} 
catch
 (Exception e) {
e.printStackTrace();
}
}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值