开发者

Checing if FTPClient download has received all bytes

开发者 https://www.devze.com 2023-03-30 20:03 出处:网络
I download an image file from an FTP Server using FTPClient. My look like this: public class FtpDownloadDemo

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

public class FtpDownloadDemo
{
 public static void main(Strin开发者_开发技巧g[] 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;

Checing if FTPClient download has received all bytes

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消