gpt4 book ai didi

java - 如何使用Java从FTP下载文件?

转载 作者:行者123 更新时间:2023-11-30 03:31:37 27 4
gpt4 key购买 nike

使用此代码我总是得到一个空文件。
我与它有什么关系?
login 始终为true。 (ofc,这里不是真实密码)

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.*;

public class Logs {
public static void main(String[] args) {
FTPClient client = new FTPClient();

try {
client.connect("myac.cs-server.pro", 121);
boolean login = client.login("a3ro", "passWordIsSecret");
System.out.println(login);
String remoteFile1 = "myac_20150304.log";
File downloadFile1 = new File("C:\\Users\\Aero\\Desktop\\test\\myac.log");
OutputStream outputStream1 =
new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = client.retrieveFile(remoteFile1, outputStream1);
System.out.println(success);
outputStream1.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

最佳答案

使用FileOutputStream:

String filename = "test.txt";
FileOutputStream fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);

关于java - 如何使用Java从FTP下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873802/

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