我们使用以下Apache Commons Net FTP代码连接到FTP服务器,轮询某些目录中的文件,如果找到文件,则将其检索到本地计算机:
try {
logger.trace("Attempting to connect to server...");
// Connect to server
FTPClient ftpClient = new FTPClient();
ftpClient.setConnectTimeout(20000);
ftpClient.connect("my-server-host-name");
ftpClient.login("myUser", "myPswd");
ftpClient.changeWorkingDirectory("/loadables/");
// Check for failed connection
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
ftpClient.disconnect();
throw new FTPConnectionClosedException("Unable to connect to FTP server.");
}
// Log success msg
logger.trace("...connection was successful.");
// Change to the loadables/ directory where we poll for files
ftpClient.changeWorkingDirectory("/loadables/");
// Indicate we're about to poll
logger.trace("About to check loadables/ for files...");
// Poll for files.
FTPFile[] filesList = oFTP.listFiles();
for(FTPFile tmpFile : filesList)
{
if(tmpFile.isDirectory())
continue;
FileOutputStream fileOut = new FileOutputStream(new File("tmp"));
ftpClient.retrieveFile(tmpFile.getName(), fileOut);
// ... Doing a bunch of things with output stream
// to copy the contents of the file down to the local
// machine. Ommitted for brevity but I assure you this
// works (except when the WAR decides to hang).
//
// This was used because FTPClient doesn't appear to GET
// whole copies of the files, only FTPFiles which seem like
// file metadata...
}
// Indicate file fetch completed.
logger.trace("File fetch completed.");
// Disconnect and finish.
if(ftpClient.isConnected())
ftpClient.disconnect();
logger.trace("Poll completed.");
} catch(Throwable t) {
logger.trace("Error: " + t.getMessage());
}
我们计划每一分钟、每一分钟运行它。当部署到Tomcat (7.0.19)时,这段代码可以很好地加载,并且可以顺利地开始工作。但是,每次在某个时刻,它似乎都会挂起。我的意思是:
catalina.out
,并且我的特定于应用程序的日志没有任何抛出异常的迹象因此,JVM仍在运行。Tomcat仍在运行,我部署的WAR也仍在运行,但它只是挂起。有时它运行2小时后挂起;有时它运行几天后挂起。但是当它挂起时,它会在显示About to check loadables/ for files...
(我确实在日志中看到)和File fetch completed.
(我看不到)的行之间执行。
这告诉我,挂起发生在文件的实际轮询/获取期间,这使我能够找到与FTPClient死锁有关的this question的相同方向。这让我想知道这些是不是相同的问题(如果是,我会很高兴地删除这个问题!)。然而,我不相信它们是相同的(我在我的日志中没有看到相同的例外)。
一位同事提到,这可能是“被动”vs.“主动”FTP的事情。由于不太清楚其中的区别,我对FTPClient字段ACTIVE_REMOTE_DATA_CONNECTION_MODE
、PASSIVE_REMOTE_DATA_CONNECTION_MODE
等感到有点困惑,也不知道这是一个潜在的问题。
因为我在这里捕获的是Throwable
s作为最后的手段,所以如果出现问题,我会期望在日志中看到一些东西。因此,我觉得这确实是一个悬而未决的问题。
有什么想法吗?不幸的是,我对FTP内部结构的了解还不够多,无法做出明确的诊断。这会是服务器端的东西吗?与FTP服务器相关?
发布于 2012-03-15 01:30:26
这可能是一系列的事情,但你朋友的建议是值得的。
试试ftpClient.enterLocalPassiveMode();
,看看它是否有帮助。
我还建议将断开连接放在finally
中,这样它就不会留下任何连接。
发布于 2013-05-09 00:48:59
昨天,我没有睡觉,但我想我已经解决了问题。
可以使用FTPClient.setBufferSize()增加缓冲区大小;
/**
* Download encrypted and configuration files.
*
* @throws SocketException
* @throws IOException
*/
public void downloadDataFiles(String destDir) throws SocketException,
IOException {
String filename;
this.ftpClient.connect(ftpServer);
this.ftpClient.login(ftpUser, ftpPass);
/* 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.setFileType(FTP.BINARY_FILE_TYPE);
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);
//I don't know what useful are these methods in this step
// I just put it for try
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
this.ftpClient.setAutodetectUTF8(true);
this.ftpClient.enterLocalPassiveMode();
ftpClient.retrieveFile(
DefaultValuesGenerator.LINPAC_ENC_DIRPATH + filename,
fos
);
}finally{
fos.flush();
fos.close(); }
}
}
if (fos != null) {
fos.close();
}
}
我希望这段代码能对某些人有用!
发布于 2015-08-22 03:30:32
我必须在登录后包含以下内容,以便调用s.listFiles并在不‘挂起’并最终失败的情况下进行传输:
s.login(username, password);
s.execPBSZ(0);
s.execPROT("P");
https://stackoverflow.com/questions/9706968
复制相似问题