4

I use the following piece of code to upload a photo to a ftp host. But the photo seems to be corrupted after being uploaded: There are narrow gray lines at the bottom of the photo.

The size of gray lines could be decreased by decreasing the Buffer Size of the FTPClient object.

import java.io.File;
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import sun.misc.Cleaner;

public class FtpConnectDemo1 {

  public static void main(String[] args) {
    FTPClient client = new FTPClient();

    try {
      client.connect("ftp.ftpsite.com");

      //
      // When login success the login method returns true.
      //
      boolean login = client.login("user@ftpsite.com", "pass");

      if (login) {
        System.out.println("Login success...");

        int replay = client.getReplyCode();

        if (FTPReply.isPositiveCompletion(replay)) {
          File file = new File("C:\\Users\\e.behravesh\\Pictures\\me2_rect.jpg");
          FileInputStream input = new FileInputStream(file);
          client.setFileType(FTP.BINARY_FILE_TYPE);

          if (!client.storeFile(file.getName(), input)) {
            System.out.println("upload failed!");
          }          

          input.close();
        }
        //
        // When logout success the logout method returns true.
        //
        boolean logout = client.logout();
        if (logout) {
          System.out.println("Logout from FTP server...");
        }
      } else {
        System.out.println("Login fail...");
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        //
        // Closes the connection to the FTP server
        //
        client.disconnect();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
ehsun7b
  • 4,796
  • 14
  • 59
  • 98
  • I have no idea why it would be important, but can you share what's the platform of the server (the client is Windows, isn't it)? Maybe these are some platform specific issues. – Grzegorz Oledzki May 22 '11 at 18:37
  • The client is Windows and Linux (Fedora) and the server is Linux too. I manage it via Cpanel. :( – ehsun7b May 22 '11 at 18:43

5 Answers5

2

Never ever heard of corruption of that type, but: are you uploading from behind a firewall? Try doing client.enterLocalPassiveMode(); before calling storeFile.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Yes I tried it when I was at office and behind the company's firewall. I'm going to check it now at home. – ehsun7b May 22 '11 at 18:44
  • It helped but the size of local file and uploaded file are still different. What does the method do? – ehsun7b May 22 '11 at 19:26
2

this is known error resolved in newest version of library:
http://commons.apache.org/net/changes-report.html#a3.0.1

0

probably late, but it could help somone to avoid waste time.

Check conf file and permitions!! In Unix using vsftp check that

write_enable=YES

stay uncomment.

Check with another FTP client if it posible to upload files.

molavec
  • 8,848
  • 1
  • 27
  • 22
0

FTP file sending is not atomic meaning that if there was a crash in the connection only partial file has been send. i would offer add change name to know when transfer is completed in the end of file send.

Hard Worker
  • 995
  • 11
  • 33
0

I've just tried your code on my local computer and it works. I didn't see any gray lines.

So I guess this is either a passive mode thing as Femi suggest or some network/firewall/lower-level problem.

Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106