commons.net.FTPClient在linux不可下载

本文对比了使用Sun公司的sun.net.ftp.FtpClient与commons.net.FTPClient进行FTP下载的区别。发现commons.net.FTPClient虽然功能更强大,但在不同操作系统上存在兼容性问题,如在Linux环境下会出现程序'假死'的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做ftp下载时,我先用sun公司的sun.net.ftp.FtpClient;里面提供的方法不是很
多.而使用commons.net.FTPClinet时,里面帮我们处理了很多,比sun公司提供的要
强大多了,但不足的是,这个插件有很多bug.我同样下载文件时,在xp下里能下载,
但在linux下就出错(ftpClient.retrieveFile(file, bos))无异常也不执行后面
的程序,成'假死'状态.
public class ftpClientThread{
private String server = ReadConfigProperties.getFtpServer();
private String username ="user"
private String password = "password
private String localDirectory = "F:/";
private FTPClient ftpClient;
public static void main(String[] args) {
ftpClientThread a = new ftpClientThread();
a.connectServer();
Object[] files = a.getListFiles();
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
a.downloadFile((String) files[i]);
System.out.println(files[i]);
}
a.closeConnect();
}
public void connectServer() {
ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.login(username, password);
System.out.println("ftp login success");
if (directory.length() != 0) {
System.out.println(directory);
ftpClient.changeWorkingDirectory
(this.directory);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("ftp服务器连接失败");
e.printStackTrace();
}
}

public void closeConnect() {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.logout();
ftpClient.disconnect();
System.out.println("ftp disconnect
success");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void downloadFile(String file) {
BufferedOutputStream bos = null;
boolean success = false;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(
localDirectory, file)));
//linux下会'假死'在这,什么也不会发生
//retrieveFile在xp下面有时也会假死
success = ftpClient.retrieveFile(file, bos);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

if (!success) {
System.out.println(file + "下载失败");
}
}
}
sun公司的ftp下载:
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

public class ftpDown {
public static void main(String[] args) {
BufferedReader br=null;
PrintWriter pw=null;
try {
FtpClient fc=new FtpClient("www.bcc123ht.com");
fc.login("user", "password");
String ch;
File fi=new File("F:\\file\\index.html");
/*RandomAccessFile getFile=new RandomAccessFile(fi,"rw");
getFile.seek(0);*/
pw=new PrintWriter(fi);
TelnetInputStream fget=fc.get("index.html");
br=new BufferedReader(new InputStreamReader(fget));
//DataInputStream puts=new DataInputStream(fget);
while((ch=br.readLine())!=null){
pw.println(ch);
}
pw.flush();
fget.close();
//getFile.close();
fc.closeServer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pw!=null){
pw.close();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值