Rash
Rash

Reputation: 896

Uploade image is corrupted on server

Hi I am trying to upload image to server using ftp4j. My image is uploaded on server but getting corrupted. I know something is missing in my code.My code is

private class UploadBackgroundTask extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
        dialog = new ProgressDialog(xxx.this);
        dialog.setMessage("Loading");
        dialog.setCancelable(false);
        dialog.show();

    }

    protected String doInBackground(final String... args) {



        try{
            TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };

            SSLContext sslContext = null;
            try {
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, trustManager, new SecureRandom());
            } catch (Exception e) {
                e.printStackTrace();
            }
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            FTPClient client = new FTPClient();

            client.setSSLSocketFactory(sslSocketFactory);
            client.setSecurity(FTPClient.SECURITY_FTPES);

            client.connect("xxx.xxx.com");
            client.login("xxx", "xxx");

            client.changeDirectory("product_images/import");
            client.setType(FTPClient.TYPE_BINARY);
            //@TODO read file from android system
            System.out.println("temPath -->"+picturepath);
            client.upload(new File(picturepath));


            FTPFile[] list = client.list();
            for(int i=0;i<list.length;i++){
                System.out.println("-->"+list[i]);
            }


            Log.v("SUCCESFULLY UPLOADED", "SUCCESFULLY UPLOADED");
            }catch(Exception ex){
                ex.printStackTrace();
            }


        return null;

    }

    protected void onPostExecute(String list) {
        dialog.dismiss();

    }
}

My image on server is: enter image description here

My logTrace is:

it.sauronsoftware.ftp4j.FTPException [code=450, message= Transfer aborted. Link to file server lost]
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2799)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2586)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2495)
at com.Activity.xxx.AddProducts$UploadBackgroundTask.doInBackground(AddProducts.java:902)
at com.Activity.xxx.AddProducts$UploadBackgroundTask.doInBackground(AddProducts.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
 Window already focused, ignoring focus gain of:
com.android.internal.view.IInputMethodClient$Stub$Proxy@41174c80

File size is on server is always 4 KB after uploading.why?

I searched a lot but not getting answer. Please tell me what is wrong in my code. Thanks in advance.

Upvotes: 3

Views: 1163

Answers (1)

user1516873
user1516873

Reputation: 5193

try to implement listener like this:

import it.sauronsoftware.ftp4j.FTPDataTransferListener;

public class MyTransferListener implements FTPDataTransferListener {

    public void started() {
        // Transfer started
    }

    public void transferred(int length) {
        // Yet other length bytes has been transferred since the last time this
        // method was called
    }

    public void completed() {
        // Transfer completed
    }

    public void aborted() {
        // Transfer aborted
    }

    public void failed() {
        // Transfer failed
    }

}

and upload file with client.upload(new File(picturepath), new MyTransferListener()); So you can track out what happens on file upload.

EDIT:

  1. Check upladed image size and params in method transferred(int length) is equals. (in case image less than 64k)
  2. Check the same byte amount was received on FTP server ( check logs on FTP server)
  3. If one of conditions above is not true, problem in ftp4j, if both of them true, problem in your FTP server.

Upvotes: 1

Related Questions