Question

I download an image file from an FTP Server using FTPClient. My look like this:

public class FtpDownloadDemo
{
 public static void main(String[] args)
 {
  FTPClient client = new FTPClient();
  FileOutputStream fos = null;

  try
  {
    client.connect("ftp.domain.com");
    client.login("user", "pass");

    //
    // The remote filename to be downloaded.
    //
    String filename = "side.jpg";
    fos = new FileOutputStream(filename);

    //
    // Download file from FTP server
    //
    client.retrieveFile("/" + filename, fos);
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
  finally
  {
    try
    {
      if (fos != null)
      {
        fos.close();
      }
      client.disconnect();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }

 }
}

When the file is downloaded the output looks distorted as shown below;

enter image description here

Is there a way to check if all the bytes has been received else wait till all is received?

Was it helpful?

Solution

I strongly suspect that the real problem is that the transfer is done in ASCII, rather than binary, mode.

Try calling client.setFileType(FTP.BINARY_FILE_TYPE); before initiating the transfer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top