3

您好我正在尝试使用 ftp4j 将图像上传到服务器。我的图像已上传到服务器但已损坏。我知道我的代码中缺少某些内容。我的代码是

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();

    }
}

我在服务器上的图像是: 在此处输入图像描述

我的日志跟踪是:

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

上传后服务器上的文件大小始终为 4 KB。为什么?

我搜索了很多但没有得到答案。请告诉我我的代码有什么问题。提前致谢。

4

1 回答 1

1

尝试像这样实现监听器:

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
    }

}

并上传文件client.upload(new File(picturepath), new MyTransferListener());因此您可以跟踪文件上传时发生的情况。

编辑:

  1. 检查方法中更新的图像大小和参数transferred(int length)是否相等。(如果图像小于 64k)
  2. 检查 FTP 服务器上收到的相同字节数(检查 FTP 服务器上的日志)
  3. 如果上述条件之一不成立,则 ftp4j 有问题,如果两者都成立,则说明您的 FTP 服务器有问题。
于 2012-10-26T13:13:04.417 回答