FTPClient (commons net) Error loading

I use the following code snippet to upload a photo to the ftp host . But the photo seems damaged after loading: There are narrow gray lines at the bottom of the photo.

The size of gray lines can be reduced by reducing the size of the FTPClient object buffer.

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();
      }
    }
  }
}
+3
source share
5 answers

this is a known bug resolved in the latest version of the library:
http://commons.apache.org/net/changes-report.html#a3.0.1

+2
source

I've never heard of this type of corruption, but: are you logging in due to a firewall? Try to do client.enterLocalPassiveMode();before calling storeFile.

+2

, . .

, , , Femi, // .

0
source

maybe late, but it can help someone avoid unnecessary time.

Check conf file and permissions! On Unix, using vsftp check that

write_enable = YES

to remain without injury.

Check with another FTP client if possible to upload files.

0
source

FTP file transfer is not atomic, because if a connection fails, only a partial file is sent. I would suggest adding the name of the change to find out when the transfer is completed at the end of the file upload.

0
source

All Articles