Interrupt OutputStream - Android <p>I'm using a <a href="http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically" rel="nofollow">this</a> class to upload files in HTTP, my problem is that I noticed when I remove the lan cable from my router the upload stucks as the write call blocks, below shows where it exactly stuck, I tried interrupting the thread but that didn't help either.</p> <p>Now the question is how can I interrupt outputStream? </p> <pre><code>public void addFilePart(String fieldName, File uploadFile) throws IOException { ... //removed code from here for clarity. //Can be found in the link above FileInputStream inputStream = new FileInputStream(uploadFile); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { //The code stuck here. //using if (Thread.interrupted()) doesn't help //as the write blocks when the network cable //removed from the router outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); inputStream.close(); writer.append(LINE_FEED); writer.flush(); } </code></pre>