views:

41

answers:

1

I am doing an FTP Get in Java with FTP Client which if I run on windows works fine but when I run the same on a linux box it get me the file with some modifications.

I have a test.tar.gz file (bunch of text files) which is of size 2872578 but it shows up as 2872541 when I run my java program on linux. Anyone faced a similar problem?

//write files to local FS

  OutputStream output = null;
  for(int i = 0; i < files.length; i++)
  {
   if(files[i].getName().compareTo(file) == 0 || files[i].getName().compareTo("*") ==0)
   if(!files[i].getName().startsWith(".") && files[i].getType() != 1)
        {
    try {
     if(targetdir != null)
     output = new FileOutputStream(new File(targetdir + files[i].getName()));
     else
      output = new FileOutputStream(new File(files[i].getName()));
     System.out.println("Creating: " + files[i].getName());
     client.retrieveFile(files[i].getName(), output);
    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } 
        }  
  } 
+2  A: 

You need to tell the FTP server that you want to transfer in binary mode; if you don't, line endings will be converted to that of the native system, which you definitely don't want for a compressed file.

How you put the transfer in binary mode, I can't say - I can't see what class you're using - but assuming this is a pre-existing class, there is most likely a method for that purpose on the class doing the transfer (or, alternatively, an overload with a parameter specifying this).

Michael Madsen
Thank you for your replies Michael and spender, transferring in binary mode worked! If anyone faces a similar problem, I am using the FTP Client class in apache commons library and it can be set using: FTPClient client = new FTPClient();client.setFileType(FTP.BINARY_FILE_TYPE);