Apache FTPClient.retrieveFile方法会将远程文件的内容传输到本地文件中,但不会创建临时文件。
以下是一个使用Apache FTPClient.retrieveFile方法下载远程文件的示例代码:
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FTPDownloadExample {
public static void main(String[] args) {
String server = "ftp.example.com";
int port = 21;
String user = "username";
String password = "password";
String remoteFile = "/path/to/remote/file.txt";
String localFile = "/path/to/local/file.txt";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, password);
OutputStream outputStream = new FileOutputStream(localFile);
ftpClient.retrieveFile(remoteFile, outputStream);
outputStream.close();
System.out.println("File downloaded successfully.");
} catch (IOException ex) {
System.out.println("Error downloading file: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
在上述示例中,将远程文件的内容下载到本地文件中,而不是创建临时文件。