我正在使用 FTP Client Apache commons net,当我尝试从服务器下载许多文件时收到此消息。
227 Entering Passive Mode (192,168,6,101,13,102)
RETR /mnt/hda/data/2013_05_15_20_50.edy
150 Opening BINARY mode data connection for '/mnt/hda/data/2013_05_15_20_50.edy' (15877 bytes).
226 Transfer complete.
TYPE I
200 Type set to I.
PASV
425 Can't open passive connection: Cannot assign requested address.
第一个文件没有问题,但对于下一个文件,我得到 “425 无法打开被动连接:无法分配请求的地址”错误。
我注意到,当我尝试下载时,使用 filezilla 连接会丢失一会儿,但 filezilla 会自动重新连接。
有没有办法检查当前连接的状态?我使用下一个代码:
/**
* Download encrypted and configuration files.
*
* @throws SocketException
* @throws IOException
*/
public void downloadDataFiles(String destDir) throws SocketException,
IOException {
String filename;
log.debug("ftpServer: " + ftpServer);
this.ftpClient.connect(ftpServer);
this.ftpClient.login(ftpUser, ftpPass);
ftpClient.setControlKeepAliveTimeout(30000); // set timeout to 5 minutes
/* CHECK NEXT 4 Methods (included the commented)
* they were very useful for me!
* and icreases the buffer apparently solve the problem!!
*/
ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
//log.debug("Buffer Size:" + ftpClient.getBufferSize());
this.ftpClient.setBufferSize(1024 * 1024);
//log.debug("Buffer Size:" + ftpClient.getBufferSize());
/*
* get Files to download
*/
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setAutodetectUTF8(true);
this.ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient
.listFiles(DefaultValuesGenerator.LINPAC_ENC_DIRPATH);
/*
* Download files
*/
for (FTPFile ftpFile : ftpFiles) {
// Check if FTPFile is a regular file
if (ftpFile.getType() == FTPFile.FILE_TYPE) {
try{
filename = ftpFile.getName();
// Download file from FTP server and save
fos = new FileOutputStream(destDir + filename);
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//TODO: check if connection still alive.
ftpClient.setControlKeepAliveTimeout(30000); // set timeout to 5 minutes
//download Files
ftpClient.retrieveFile(
DefaultValuesGenerator.LINPAC_ENC_DIRPATH + filename,
fos
);
}finally{
fos.flush();
fos.close(); }
}
}
if (fos != null) {
fos.close();
}
}