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

时间:2014-07-29 10:11:29

标签: 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);
}

1 个答案:

答案 0 :(得分:3)

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

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)方法中止当前上传任务

希望这会对你有所帮助