I am here to solve an issue i am facing. I hope to get some help from the experts.
Below is a code of a simple file downloader in java. I want to detect an exception when the connection is broken in the middle of downloading a file. Now, I run the code and in the middle of downloading the file, i turn off my wifi with keyboard wifi off button. Doing this the program hangs forever without throwing any exceptions and without terminating. It seems it blocks forever. Firstly my confusion is why is exception not thrown? Secondly, in the following code you can see this line //con.setReadTimeout(2000); which is currently commented out. Removing the comment and running the program, now if connection breaks in the middle by turning off wifi then it waits for 2 seconds and then if it cannot read then it terminates without throwing exception. So, again in this case why is it just terminating and not throwing any exception? I am very puzzled by this behavior. I hope i can get some help. Thank you very much!
I recommend using con.getContentLength() to know how many bytes to expect, and then put the transferFrom() call in a loop, counting how many bytes have been transferred each time (see return value of transferFrom()), and stopping only when the expected number of bytes is reached.
subject: Why is exception not thrown when the connection is terminated?