java - 在 Android 中使用 CustomMultiPartEntity 取消上传文件

标签 java android upload

我正在使用 CustomMultiPartEntity 来上传具有取消功能的文件,文件上传成功,但是当我尝试取消此任务以停止上传时,它不会,有人已经实现或知道如何停止上传吗?

下面是我的取消代码

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;

import android.util.Log;

public class CustomMultiPartEntity extends MultipartEntity {
    private static final String TAG = CustomMultiPartEntity.class.getSimpleName();
    private final ProgressListener listener;
    private static boolean isUserInterrupt = false;
    public CustomMultiPartEntity(final ProgressListener listener) {
        super();
        this.listener = listener;
        isUserInterrupt = false;
    }

    public CustomMultiPartEntity(final HttpMultipartMode mode,
            final ProgressListener listener) {
        super(mode);
        this.listener = listener;
        isUserInterrupt = false;
    }

    public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary,
            final Charset charset, final ProgressListener listener) {
        super(mode, boundary, charset);
        this.listener = listener;
        isUserInterrupt = false;
    }

    public void cancelUploading(){
        isUserInterrupt = true;
        if(cos!=null){
            try{
                cos.flush();
                cos.close();
                cos = null;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    private CountingOutputStream cos;
    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        if(isUserInterrupt){
            if(cos!=null){
                try{
                    cos.flush();
                    cos.close();
                    cos = null;
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }

        cos = new CountingOutputStream(outstream, this.listener);
        super.writeTo(cos);
        Log.i(TAG, "write to calling");
    }

    public static interface ProgressListener {
        void transferred(long num);
    }

    public static class CountingOutputStream extends FilterOutputStream {

        private final ProgressListener listener;
        private long transferred;

        public CountingOutputStream(final OutputStream out,
                final ProgressListener listener) {
            super(out);
            this.listener = listener;
            this.transferred = 0;
        }

        public void write(byte[] b, int off, int len) throws IOException {
            if(isUserInterrupt)
            {
                Log.i(TAG, "write interrupt, off="+off+", len+"+len);
                return;
            }
            out.write(b, off, len);
            this.transferred += len;
            this.listener.transferred(this.transferred);
            Log.i(TAG, "write off="+off+", len+"+len);
        }

        public void write(int b) throws IOException {
            if(isUserInterrupt)
            {
                Log.i(TAG, "write interrupt, b+"+b);
                return;
            }
            out.write(b);
            this.transferred++;
            this.listener.transferred(this.transferred);
            Log.i(TAG, "write b+"+b);
        }
    }
}

在AsyncTask中使用这种方式取消上传

public class UploadImageTask extends AsyncTask<HttpResponse, Integer, String> {
@Override
    protected void onCancelled() {
        super.onCancelled();
        Log.e(TAG, "task cancelled");
    }

    public void taskCancel() {
        if(httpPost!=null){
            httpPost.abort();
            httpPost = null;
        }
        if(httpClient!=null){
            httpClient.getConnectionManager().shutdown();
        }
        httpClient = null;
    }
}

使用以下代码取消任务

UploadImageTask task = (UploadImageTask)iMap.get(id);
if(task!=null && !task.isCancelled()){
    task.taskCancel();
    task.cancel(true);
}

最佳答案

我找到了取消上传当前文件的方法。创建一种中止或关闭当前与服务器的连接的方法

public class MyTask extends AsyncTask<Void, Integer, String>{

    private HttpPost httpPost;
    private CustomMultiPartEntity multipartContent;
    private HttpClient httpClient;
    @Override
    protected String doInBackground(HttpResponse... arg0) {
        httpClient = new DefaultHttpClient();
        HttpContext httpContext = new BasicHttpContext();
        httpPost = new HttpPost(urlStr);

        ....
        // other upload statement here 
        return resp;
    }
    @Override
    protected void onCancelled() {
        super.onCancelled();
        Log.e(TAG, "task cancelled");
    }

    public void taskCancel() {
        if(httpPost!=null){
            httpPost.abort();
            httpPost = null;
        }
        if(httpClient!=null){
            httpClient.getConnectionManager().shutdown();
        }
        httpClient = null;
    }
}

现在先调用taskCancel(),然后调用cancel(true)方法来中止当前上传任务

希望这对您有帮助

关于java - 在 Android 中使用 CustomMultiPartEntity 取消上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25012887/

相关文章:

java - JPA 与 JDBC : Read only access on existing database

java - 显示 http ://localhost:9000 can not be reached 的 Sonar 转轮

java - Matlab/Java API 回调

java - 即使在重新排列列表后,如何在自定义 ListView 中获得正确的位置项?

java - Resteasy中的文件上传

c# - selenium webdriver .Net chrome 上传文件

ios - 如何在New iTunes Connect中删除上传的版本并添加新版本

java - for each 里面一个 for each - Java

android - 为什么 OnCreate 应该在 Activity 启动时只调用一次?

android - 如何创建不重复的组合并存储在android studio的数据库中?