I am using the following code to download a file using Java but i want to detect when connection is lost. I ran the following code and while in the middle of downloading i disconnected my internet purposefully but no exception was thrown and it hanged. Even after turning on the connection back nothing happened. So, it hanged forever without any exceptions. Is there a way to make it throw an exception when the connection is lost? Thanks for the help!
package toplower.top;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.*;
import javax.mail.*;
public class testing {
public static void main(String[] args) {
try{
URL website = new URL("http://b128.ve.vc/b/data/128/1735/asd.mp3");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("song.mp3");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
catch(Exception e){
System.out.println("got here");
e.printStackTrace();
}
System.out.println("Done downloading...");
}
}