EL Kamel Malek
EL Kamel Malek

Reputation: 101

Download File from remote Ftp Server (file downloaded empty)

I'am traying to download a file from a remote ftp server using Java (FTPClient). The file has been downloaded successfully but it's empty and i didnt find a solution for my problem

this is my code

FTPClient ftpClient = new FTPClient();
                 try {

                        ftpClient.connect(ip, port);
                        ftpClient.login(user, pass);
                        ftpClient.enterLocalPassiveMode();
                        ftpClient.epsv();
                        ftpClient.mlsd();

                        File downloadFile = new File("contextFolder/test.txt");
                        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
                        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);

                        byte[] bytesArray = new byte[4096];
                        int length;
                        //copy the file content in bytes 
                        while ((length = inputStream.read(bytesArray)) > 0){

                            outputStream.write(bytesArray, 0, length);

                        }

                        Boolean success = ftpClient.completePendingCommand();

                        outputStream.close();
                        inputStream.close();
                        if (success) {
                            System.out.println("File "+remoteFile+" has been downloaded successfully.");
                        }


            }catch(Exception ex){}

I need to save the file into the contextFolder with the name test.txt :) Thank You all :)


This is the exception that i'm getting

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.read(Unknown Source)
    at org.apache.commons.net.io.CRLFLineReader.readLine(CRLFLineReader.java:58)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:314)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:483)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:556)
    at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:605)
    at org.apache.commons.net.ftp.FTP.pasv(FTP.java:956)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:806)
    at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1853)
    at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1844)
    at com.ericsson.etl.module.Activity1.execute(Activity1.java:49)
    at com.ericsson.etl.SequenceProcessor.doActivities(SequenceProcessor.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

Upvotes: 2

Views: 4539

Answers (2)

Daniel Lerps
Daniel Lerps

Reputation: 5375

Try outputStream.flush() before you close it.

If that doesn't help you should check if any exceptions are thrown via ex.printStackTrace() in the try block. You often find the reason there (e.g. mising write permissions, connection error, ...).

Upvotes: 0

Stephen C
Stephen C

Reputation: 718798

Are you getting the "File XXX has been downloaded" message? If not, the likely cause is that an exception is being thrown, and silently discarded by this:

} catch (Exception ex){}

You should NEVER catch and exceptions like that.

Upvotes: 1

Related Questions