Android:在被动模式下FTP客户端文件传输需要一段时间才能在100%上传后关闭连接

时间:2014-06-26 08:24:23

标签: android asynchronous ftp

Android:在被动模式下FTP客户端文件传输花费时间在100%上传后关闭连接

在通过FTP客户端传输文件时,在被动模式下,我们使用异步任务。 即使在指定了100%的文件的进度更新后,仍然ftp连接在执行后仍然保持异步任务。

所花费的时间与上网速度和上传文件大小成正比。

尝试使用独立应用程序上传zip文件, 在主动和被动模式下都尝试了ftp。

问题仍然存在。

public class UploadZipFiles extends AsyncTask<Object, Integer, Object> {
        ArrayList<String> zipFiles;
        String userName, password;
        WeakReference<ServiceStatusListener> listenerReference;
        private Context mContext;
        private long totalFileSize = 0;
        protected long totalTransferedBytes = 0;
        final NumberFormat nf = NumberFormat.getInstance();
        private CustomFtpClient ftpClient = null;

        public UploadZipFiles(Context mContext, ServiceStatusListener listener,
                ArrayList<String> zipFiles, String userName, String password) {
            Log.d("u and p", "" + userName + "=" + password);
            this.mContext = mContext;
            this.zipFiles = zipFiles;
            this.userName = userName;
            this.password = password;
            this.listenerReference = new WeakReference<ServiceStatusListener>(
                    listener);
            nf.setMinimumFractionDigits(2);
            nf.setMaximumFractionDigits(2);

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // getting total size of the file
            for (String file : zipFiles) {

                totalFileSize = totalFileSize + new File(file).length();
            }
        }

        @Override
        protected Object doInBackground(Object... arg0) {

            ftpClient = new CustomFtpClient();

            try {
                ftpClient.connect(ftpUrl, 21);

                ftpClient.login(userName, password);

                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

                for (String file : zipFiles) {

                    InputStream in;

                    in = new FileInputStream(new File(file));

                    ftpClient.storeFile(new File(file).getName(), in);

                    in.close();

                }


            } catch (IOException e1) {
                e1.printStackTrace();
            }

            return "Success";
        }

        @Override
        protected void onPostExecute(Object result) {

            if (result instanceof Exception) {
                listenerReference.get().onFailure(
                        new Exception(result.toString()));
            } else {
                listenerReference.get().onSuccess("Success");
            }


        }

        @Override
        protected void onProgressUpdate(Integer... values) {

            int uploadProgress = ((float) values[0] / totalFileSize) * 100);
            //Some code to show loader
            .......


        }

        /** Custom client to publish progress **/

        public class CustomFtpClient extends FTPClient {

            public boolean storeFile(String remote, InputStream local)
                    throws IOException {
                final OutputStream output;
                final Socket socket;

                if ((socket = _openDataConnection_(FTPCommand.STOR, remote)) == null)
                    return false;

                output = new BufferedOutputStream(socket.getOutputStream(),
                        getBufferSize());

                try {
                    Util.copyStream(local, output, getBufferSize(),
                            CopyStreamEvent.UNKNOWN_STREAM_SIZE,
                            new CopyStreamListener() {
                                @Override
                                public void bytesTransferred(
                                        long totalBytesTransferred,
                                        int bytesTransferred, long streamSize) {
                                    totalTransferedBytes = totalTransferedBytes
                                            + bytesTransferred;
                                    publishProgress((int) totalTransferedBytes);

                                    if (totalTransferedBytes == totalFileSize) {

                                        Log.d(TAG, "upload completed");

                                    }

                                }

                                @Override
                                public void bytesTransferred(
                                        CopyStreamEvent arg0) {
                                    // TODO Auto-generated method stub

                                }
                            });


                } catch (IOException e) {
                    try {
                        socket.close();
                    } catch (IOException f) {
                    }
                    throw e;                    }
                output.close();
                socket.close();
                return completePendingCommand();
            }
        }
    }

0 个答案:

没有答案