Я использую FTP-клиент 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();
}
}