Suranga
Suranga

Reputation: 19

Android FTP Download

I am using the following code segment to download a file to the SD Card of an Android phone. In the Wireshark trace I see the file is transferred and the FTP transfer complete message as well. But the file in the SD card is zero size. All required permissions are set in the manifest file. Can anybody help? Not working with FileOutputStream either.

       try{
       ftpClient.connect(InetAddress.getByName("xxx.xxx.xxx.xxx"));        
       ftpClient.login("xxxx", "xxxxx");
       ftpClient.changeWorkingDirectory("xxxx");
       ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
       ftpClient.enterLocalPassiveMode();

       //FileOutputStream desFileStream = new FileOutputStream("/sdcard/test25.txt");
       BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/test25.txt"),8*1024);

         boolean status=ftpClient.retrieveFile("test.txt", desFileStream);

         if(status){
        Toast toast = Toast.makeText(getApplicationContext(), "Downlaoded", Toast.LENGTH_SHORT);
        toast.show();
         }

       //desFileStream.flush();

       ftpClient.logout();
       ftpClient.disconnect();

       }

       catch (IOException e){
        Toast toast = Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT);
        toast.show();
        }

Upvotes: 1

Views: 2420

Answers (2)

MByD
MByD

Reputation: 137282

You need to close the output stream after done writing to it:

BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream("/sdcard/test25.txt"),8*1024);
boolean status=ftpClient.retrieveFile("test.txt", desFileStream);
desFileStream.close();

Upvotes: 1

Jack
Jack

Reputation: 9242

Why is the .flush() call commented out? Have you tried .flush()'ing and .close()'ing ?

According to the documentation:

flush()

Flushes this stream to ensure all pending data is written out to the target stream.

Upvotes: 1

Related Questions