/ *
我在localhost上运行FTP服务器。当我使用ftpClient.retrieveFile()方法下载文件时,其ReplyCode为550。我阅读了commons-net的API并找到了550 ReplyCode,其定义为“public static final int FILE_UNAVAILABLE 550”。但是我无法从代码中找到问题。
谢谢你的帮助。
* /
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
ftpClient.connect("192.168.1.102",2121);
ftpClient.login("myusername", "12345678");
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
String remoteFileName = "ftpserver.zip";//this file in the rootdir
fos = new FileOutputStream("f:/down.zip");
ftpClient.setBufferSize(1024);
ftpClient.enterLocalPassiveMode();
ftpClient.enterLocalActiveMode();
ftpClient.retrieveFile(remoteFileName, fos);
System.out.println("retrieveFile?"+ftpClient.getReplyCode());
fos.close();
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP异常", e);
}
}
最佳答案
我发现Apache RetrieveFile(...)有时无法使用超过特定限制的文件大小。为了克服这个问题,我将改用retrieveFileStream()。在下载之前,我已经设置了正确的FileType并将Mode设置为PassiveMode
所以代码看起来像
....
ftpClientConnection.setFileType(FTP.BINARY_FILE_TYPE);
ftpClientConnection.enterLocalPassiveMode();
ftpClientConnection.setAutodetectUTF8(true);
//Create an InputStream to the File Data and use FileOutputStream to write it
InputStream inputStream = ftpClientConnection.retrieveFileStream(ftpFile.getName());
FileOutputStream fileOutputStream = new FileOutputStream(directoryName + "/" + ftpFile.getName());
//Using org.apache.commons.io.IOUtils
IOUtils.copy(inputStream, fileOutputStream);
fileOutputStream.flush();
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(inputStream);
boolean commandOK = ftpClientConnection.completePendingCommand();
....