title: cancel uploading file using CustomMultiPartEntity in Android
body: <p>I am using CustomMultiPartEntity for Uploading file with Cancel feature, file upload successfully but when I try to cancel this task to stop uploading it won't, does anybody have implemented or any idea how to stop uploading?</p>
  
  <p>below is my code with cancel</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>Cancelling Upload using this way in AsyncTask</p>
  
  <pre><code>public class UploadImageTask extends AsyncTask&lt;HttpResponse, Integer, String&gt; {
  @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;
      }
  }
  </code></pre>
  
  <p>Cancel Task using below code</p>
  
  <pre><code>UploadImageTask task = (UploadImageTask)iMap.get(id);
  if(task!=null &amp;&amp; !task.isCancelled()){
      task.taskCancel();
      task.cancel(true);
  }
  </code></pre>