如何使用Java从FTP下载文件? [英] How to download file from FTP using Java?
本文介绍了如何使用Java从FTP下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用这段代码,我总是得到一个空文件。
我该怎么处理它?<br>
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();
尝试{
client.connect(myac.cs-server.pro,121);
布尔登录= client.login(a3ro,passWordIsSecret);
System.out.println(login);
字符串remoteFile1 =myac_20150304.log;
文件downloadFile1 =新建文件(C:\\ Users \\Aero\\\\\\\\\\\\\\\\\\);
OutputStream outputStream1 =
new BufferedOutputStream(new FileOutputStream(downloadFile1));
布尔成功= client.retrieveFile(remoteFile1,outputStream1);
System.out.println(成功);
outputStream1.close();
} catch(IOException e){
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch(IOException e){
e.printStackTrace();
}
}
}
}
解决方案使用
FileOutputStream
: 字符串filename =test.txt;
FileOutputStream fos = new FileOutputStream(filename);
client.retrieveFile(/+ filename,fos);
With this code iI always get a empty file.
What I have to do with it?
login
is always true
. (ofc, here is not real password)
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();
}
}
}
}
解决方案
Use FileOutputStream
:
String filename = "test.txt";
FileOutputStream fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);
这篇关于如何使用Java从FTP下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文