commons.net.FTPClient在linux不可下载
在做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*); System.out.println(files*); } 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(); } } }
同样的问题,如何解决啊?
在linux时文件的路径发生改变。