FileInputStream fis = null;
ProtocolCommandListener listener;
listener = new PrintCommandListener(new PrintWriter(System.out));
FTPClient client = new FTPClient();
client.addProtocolCommandListener(listener);
client.connect("ftptest.ftpserver.com");
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP ftptest.ftpserver.com refused connection.");
System.exit(1);
}
client.login("username", "password");
String filename = "D://mnt//home2//app//data//generatedReports//adminReports//07282012//ghostingReport.csv";
fis = new FileInputStream(filename);
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterRemotePassiveMode();
if(client.storeFile("//ghostingReport.csv", fis)){
System.out.println(client.completePendingCommand());
FTPFile[] listDir=client.listFiles();
for(int i=0;i<listDir.length;i++){
FTPFile ftpFile = listDir[0];
System.out.println(ftpFile.getName());
}
}
fis.close();
client.logout();
client.disconnect();
当我运行此代码时,我得到以下输出,但文件未传输 .
220-BlackMoon FTP服务器版本3.1.8第8版 - Build 1737 220 service ready USER username 331用户名okay,需要密码 . 通过密码230用户已登录,继续 . TYPE I 200命令没关系 . PASV 227进入被动模式(64,132,181,121,149,161)QUIT 221再见 .
我用的时候
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
它不支持421格式的异常 .
编辑:
解决了 . 使用以下代码 .
FileInputStream fis = null;
ProtocolCommandListener listener;
listener = new PrintCommandListener(new PrintWriter(System.out));
FTPClient client = new FTPClient();
client.addProtocolCommandListener(listener);
client.connect("ftptest.ftpserver.com");
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP ftptest.ftpserver.com refused connection.");
System.exit(1);
}
client.login("username", "password");
String filename = "D://mnt//home2//app//data//generatedReports//adminReports//07282012//ghostingReport.csv";
fis = new FileInputStream(filename);
client.storeFile("ghostingReport.csv", fis);
client.logout();
fis.close();
client.disconnect();