FTPClient FTPFile详情不可用

huangapple go评论76阅读模式
英文:

FTPClient FTPFile details not available

问题

以下是您提供的代码的翻译部分:

我正在尝试从远程FTP服务器获取文件列表。 ftpClient.listFiles() 返回了 null,我不得不将setUnparseableEntries设置为true以获取文件列表。即使如此,文件列表也没有任何名称等信息,它只有 rawlisting,其他都是 null。因此,我无法执行 ftpFile.getName。以下是代码:

public FTPFile[] process() throws Exception {
	 String message = null;
	 FTPFile[] files = null;
	
	 FTPClient ftpClient = new FTPClient();
	
	 FTPClientConfig config = new FTPClientConfig();
	 config.setServerTimeZoneId("America/Chicago");
	 config.setUnparseableEntries(true);
	 ftpClient.configure(config);
	
	 if ( !connectToServer() ) return null;
	
	 if ( !changeDirectory() ) {
		disconnectFromServer();
		return null;
	 }
	 files = getListofFiles();
	 disconnectFromServer();
	
	 return files;
}

private boolean connectToServer() {
	 boolean result = true;
	 String message = null, url = null;
	
	 // 尝试连接到目标服务器
	 try {
		 url = fo.getServerInfo().getConnectionURL();
		 LOGGER.debug("连接至: " + url);
		 ftpClient.connect(fo.getServerInfo().getHostName(),
				fo.getServerInfo().getHostPort());
		 ftpClient.enterLocalPassiveMode();
	 } catch(SocketException e) {
		 result = false;
		 message = "无法连接到服务器: " + url;
	 } catch(IOException e) {
		 result = false;
		 message = "无法连接到服务器: " + url;
	 }
	 if ( !result ) return result;
	
	 // 连接尝试后,您应该检查回复代码以验证成功。
	 Integer replyCode = ftpClient.getReplyCode();
	
	 if ( !FTPReply.isPositiveCompletion(replyCode) ) {
		message = "回复代码 - " + replyCode.toString() + " 是负面的。";
		try {
			ftpClient.disconnect();
		} catch(Exception e) {
			message = "无法从服务器断开连接。";
			LOGGER.error(message);
		}
	 } else {
		message = "回复代码 - " + replyCode.toString() + " 是正面的。";
	 }
	
	 Boolean logonOk = false;
	 try {
		logonOk = ftpClient.login(fo.getServerInfo().getUserName(), 
				fo.getServerInfo().getUserPassword());
	 } catch(IOException e) {
		message = "登录尝试期间发生 IOException。";
		LOGGER.error(message);
	 }
	 if ( !logonOk ) {
		result = false;
		message = "登录不成功。";
	 } else {
		message = "登录成功。";
		LOGGER.error(message);
		executionMessageLog.add(message);
	 }
		
	 if ( !result ) return result;
	
	 // 尝试登录到目标服务器
	
	 return result;
}

以下方法试图获取文件列表我可以使用 `listNames` 看到文件名同时 `listFiles` 显示了文件列表但是名称修改日期为空只在 rawlisting 中有值格式为 "04-01-20  11:31AM                 8975 test.TXT"那么如何从原始列表中获取名称和修改日期为什么我不能使用 `getName` 获取 `FTPFile` 的名称

```java
private FTPFile[] getListofFiles(){
	 String message = null;
	 FTPFile[] files = null;
	 try {
		String[] filenames = ftpClient.listNames(fileListInfo.getFilePath());
		files = ftpClient.listFiles(); /*只有 rawlisting,其他都是 null*/
	 }
	 catch(IOException e) {
		message = "获取文件列表尝试期间发生 IOException:";
		LOGGER.error(message);
		executionMessageLog.add(message);
		message = e.getMessage();
		LOGGER.error(message);
		executionMessageLog.add(message);
	 }
	 return files;
}
英文:

I am trying to get the list of files from remote FTP server. The ftpClient.listFiles() returned null and I had to set setUnparseableEntries to true to get the list of files. Even then the list of files do not have any information like name and only information it has is rawlisting and others are null. So I cannot do ftpFile.getName. Here is the code

public FTPFile[] process() throws Exception {
String message = null;
FTPFile[] files = null;
FTPClient ftpClient = new FTPClient();
FTPClientConfig config = new FTPClientConfig();
config.setServerTimeZoneId("America/Chicago");
config.setUnparseableEntries(true);
ftpClient.configure(config);
if ( !connectToServer() ) return null;
if ( !changeDirectory() ) {
disconnectFromServer();
return null;
}
files = getListofFiles();
disconnectFromServer();
return files;
}
private boolean connectToServer() {
boolean result = true;
String message = null, url = null;
// attempt to connect to the target server
try {
url = fo.getServerInfo().getConnectionURL();
LOGGER.debug("Connecting to: " + url);
ftpClient.connect(fo.getServerInfo().getHostName(),
fo.getServerInfo().getHostPort());
ftpClient.enterLocalPassiveMode();
} catch(SocketException e) {
result = false;
message = "Could not connect to server at " + url;
} catch(IOException e) {
result = false;
message = "Could not connect to server at " + url;
}
if ( !result ) return result;
// After connection attempt, you should check the reply code to verify success.
Integer replyCode = ftpClient.getReplyCode();
if ( !FTPReply.isPositiveCompletion(replyCode) ) {
message = "Reply Code - " + replyCode.toString() + " is negative.";
try {
ftpClient.disconnect();
} catch(Exception e) {
message = "Could not disconnect cleanly from server.";
LOGGER.error(message);
}
} else {
message = "Reply Code - " + replyCode.toString() + " is positive.";
}
Boolean logonOk = false;
try {
logonOk = ftpClient.login(fo.getServerInfo().getUserName(), 
fo.getServerInfo().getUserPassword());
} catch(IOException e) {
message = "IOException during logon attempt.";
LOGGER.error(message);
}
if ( !logonOk ) {
result = false;
message = "Logon UNsuccessful.";
} else {
message = "Logon successful.";
LOGGER.error(message);
executionMessageLog.add(message);
}
if ( !result ) return result;
// attempt to log onto the target server
return result;
}

Following method is trying to get the list of files. I could see the file name using listNames and also listFiles shows list of files but name, modified date are empty and only has value in rawlisting in the format "04-01-20 11:31AM 8975 test.TXT". So how to get the name and modified date from the raw listing and why I could not get FTPFile name using getName

private FTPFile[] getListofFiles(){
String message = null;
FTPFile[] files = null;
try {
String[] filenames = ftpClient.listNames(fileListInfo.getFilePath());
files = ftpClient.listFiles(); /*Has only rawlisting and others are null*/
}
catch(IOException e) {
message = "IOException during getListofFiles attempt:";
LOGGER.error(message);
executionMessageLog.add(message);
message = e.getMessage();
LOGGER.error(message);
executionMessageLog.add(message);
}
return files;
}

答案1

得分: 1

这是相当不寻常的格式。因此,Apache Commons Net库可能无法使用默认配置解析它。
您可能需要明确指定其中一个可用的解析器。可用的解析器位于`src\main\java\org\apache\commons\net\ftp\parser`中。或者,如果没有与您的服务器特别兼容的解析器,您可能需要构建自己的解析器(可以基于`ConfigurableFTPFileEntryParserImpl`)。
不过实际上,对于临时解决方案,更容易的方法是只解析您已经有的“rawlisting”。
英文:
04-01-20 11:31AM 8975 test.TXT

That's quite unusual format. So it's possible that Apache Commons Net library cannot parse it with the default configuration.

You might need to explicitly specify one of the available parsers. The available parsers are in src\main\java\org\apache\commons\net\ftp\parser. Or if there's no parser specifically compatible with your server, you might need to build your own (you can base it on ConfigurableFTPFileEntryParserImpl).

Though actually, for an ad-hoc solution, easier would be that you just parse the "rawlisting" you already have.

huangapple
  • 本文由 发表于 2020年5月29日 04:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定