0

I'm trying to upload an image from android to ftp server, but when i try to open the image that i uploaded i see the following message instead of the image "the image cannot be displayed because it contains errors"

and this is the code that i use

    public void uploadImage(String path){

     String server = "www.domainname.com";
        int port = 21;
        String user = "ftp-username";
        String pass = "ftp-password";

    FTPClient ftpClient = new FTPClient();
    try {

        ftpClient.connect(server, port);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();


        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File(path);

        long fileSize = firstLocalFile.length();

        Log.i("File Size",fileSize+"");

        String firstRemoteFile = "testfile1.jpg";
        InputStream inputStream = new FileInputStream(firstLocalFile);

        Log.i("uploading", "Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            Log.i("uploaded", "finished uploading first file");
        }

        // APPROACH #2: uploads second file using an OutputStream
        File secondLocalFile = new File(path);
        String secondRemoteFile = "testfile2.jpg";
        inputStream = new FileInputStream(secondLocalFile);

        Log.i("uploading", "Start uploading second file");
        OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
        byte[] bytesIn = new byte[4096];
        int read = 0;

        while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
        }
        inputStream.close();
        outputStream.close();

        boolean completed = ftpClient.completePendingCommand();
        if (completed) {
            Log.i("uploaded", "finished uploading second file");
        }

    } catch (IOException ex) {
        Log.i("Error", "Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

where is the error!!?

Thanks in advance..

Fadi Khalil
  • 291
  • 4
  • 8
  • 18
  • Where and how are you trying to display the image? Please give file length of original image and uploaded ones. – greenapps Jun 18 '14 at 08:43
  • After i uploaded the image i logged in to the web hosting using ftp username and password and double click on the image, so it should display in the browser but i see the error message instead of it,but if i try to see it using it URL address i see this lawyer-book.com/Images/profiles/faditest.jpg – Fadi Khalil Jun 18 '14 at 08:51
  • Please post a real link where we can click on. Can't you use a ftp client? Please find out the lengths. – greenapps Jun 18 '14 at 08:58
  • The image size is less than 100 KB, and this is a real link for image i uploaded http://lawyer-book.com/Images/profiles/faditest.jpg – Fadi Khalil Jun 18 '14 at 09:05
  • You have to provide two lengths. One of the original and one of the uploaded. In bytes! Not in KB. Every byte counts. – greenapps Jun 18 '14 at 09:09
  • Original image size is 28889 bytes, and the uploaded image is 29000... – Fadi Khalil Jun 18 '14 at 09:16
  • What do you mean with `29000...` Aren't you sure? Did you convert 29 KB yourself? – greenapps Jun 18 '14 at 09:25
  • No, i found the number 29000 bytes in the file details – Fadi Khalil Jun 18 '14 at 09:28
  • Hmmmm strange that it is more. You have two approaches. How do they differ? – greenapps Jun 18 '14 at 09:43
  • Unbelieveble remark. If you tried both approaches -separately- you have two results and you know. – greenapps Jun 18 '14 at 09:56

1 Answers1

0

This looks suspiciously like this bug: FTPClient corrupts the images while uploading to ftp server on android?

Try using FTP4J instead.

Community
  • 1
  • 1
HHK
  • 4,852
  • 1
  • 23
  • 40