gpt4 book ai didi

java - commons net ftp 问题

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:43 24 4
gpt4 key购买 nike

我遇到的问题是,我的 FTP 连接似乎正确且未收到任何错误,但文件并未放置在 ftp 服务器上。

我正在使用 commons-net-ftp。

代码:

        int retCode = 0;

FTPClient client = new FTPClient();
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

InputStream input = null;
try
{
int replyCode;

client.connect(pHostName);

replyCode = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode))
{
client.disconnect();
logInfo("ftpFile() - FTP Server refused connection");
retCode = 1;
}
else
{
if(client.login(pUserName, pPassword))
{
//default is FTP.ASCII_FILE_TYPE
if(this.isBinaryTransfer())
{
client.setFileType(FTP.BINARY_FILE_TYPE);
}

// Use passive mode as default because most of us are
// behind firewalls these days.
client.enterLocalPassiveMode();

input = new FileInputStream(pLocalFileName);

if(this.isRemoveRemoteFile())
{
client.deleteFile(pRemoteFileName);
client.getReply();
this.logReplyStringInfo(client.getReplyStrings(), "Removing Remote File");
}

if(this.isOverwriteFile())
{
replyCode = client.sendCommand("-O");
this.logReplyStringInfo(client.getReplyStrings(), "Overwrite File");
}

if(!client.storeFile(pRemoteFileName, input))
{
logError("ftpFile() - Not able to store the file on the server" );
retCode = 6;
}

input.close();
client.logout();
client.disconnect();
}
else
{
client.logout();
client.disconnect();
retCode = 3;
}
}
}
catch (FileNotFoundException fileNotFoundException)
{
logError("ftpFile(String, String, String, String, String)", fileNotFoundException); //$NON-NLS-1$

fileNotFoundException.printStackTrace();
retCode = 5;
}
catch (FTPConnectionClosedException e)
{
logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

retCode = 4;
e.printStackTrace();
}
catch (IOException e)
{
logError("ftpFile(String, String, String, String, String)", e); //$NON-NLS-1$

e.printStackTrace();
e.printStackTrace();
retCode = 2;
}
finally
{
if (client.isConnected())
{
try
{
if(null != input)
{
input.close();
}
client.disconnect();
}
catch (IOException f)
{
logWarning("ftpFile(String, String, String, String, String) - exception ignored", f); //$NON-NLS-1$
}
}
}

日志文件跟踪:

2010-10-12 10:57:53,527 INFO [STDOUT] 230 Logged in successfully
2010-10-12 10:57:53,527 INFO [STDOUT] PASV
2010-10-12 10:57:53,576 INFO [STDOUT] 227 Entering Passive Mode (216,27,89,17,10,231)
2010-10-12 10:57:53,624 INFO [STDOUT] STOR SharperImageFeed2.txt
2010-10-12 10:57:53,681 INFO [STDOUT] 150 "/SharperImageFeed2.txt" file ready to receive in ASCII mode
2010-10-12 10:57:54,337 INFO [STDOUT] 226 Transfer finished successfully.
2010-10-12 10:57:54,337 INFO [STDOUT] QUIT
2010-10-12 10:57:54,384 INFO [STDOUT] 221 Windows FTP Server (WFTPD, by Texas Imperial Software) says goodbye

关于问题是什么有什么建议吗?有什么可以运行的测试吗?

我可以使用 FileZilla ftp 和上传文件。


做进一步的测试,当我从我的本地开发环境运行时,我能够执行一个成功的 FTP 放置 - 这是 Windows (Eclipse/JBoss) 但是,当 FTP 从生产服务器 (Linux/JBoss) 运行时,跟踪表示成功,但没有在 FTP 服务器上放置任何内容。

最佳答案

Commons FTP 的一些命令要求您调用 completePendingCommand:

 if(!client.completePendingCommand()) {
client.logout();
client.disconnect();
System.err.println("File transfer failed.");
System.exit(1);
}

尝试在 storeFile 之后添加上面的内容。更多信息在这里:

http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html#completePendingCommand()

关于java - commons net ftp 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com