How to download file from FTP using Java?
11,300
Solution 1
Use FileOutputStream
:
String filename = "test.txt";
FileOutputStream fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);
Solution 2
Use something like this:
InputStream inputStream = client.retrieveFileStream(remoteFileNameHere);
To retrieve the remote file input stream.
Then you can use to copy the stream to desired file:
FileOutputStream out = new FileOutputStream(targetFile);
org.apache.commons.io.IOUtils.copy(in, out);
Author by
sashaaero
Updated on June 17, 2022Comments
-
sashaaero almost 2 years
With this code iI always get a empty file.
What I have to do with it?
login
is alwaystrue
. (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(); } } } }