Let me summarize my problem I am trying to download a file using java nio in that I have also written code for resuming the file download when you run the program again but my problem is this when there is no internet connection the download process is not stoping( what I meant is when there is no internet the code is not going to the next line no exception nothing it simply waits for the internet to resume .)
package com.jcg.java.nio;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DownloadFileFromUrl {
// File Location
private static String filePath ="D:\\path\\app.zip";
// Sample Url Location
private static String sampleUrl = "server_url";
// private static int downloaded;
// This Method Is Used To Download A Sample File From The Url
private static void downloadFileFromUrlUsingNio() {
URL urlObj = null;
ReadableByteChannel rbcObj = null;
FileOutputStream fOutStream = null;
long downloaded=0l;
try {
long startTime = System.currentTimeMillis();
urlObj = new URL(sampleUrl);
HttpURLConnection httpUrlConnection = (HttpURLConnection) urlObj.openConnection();
File file=new File("D:\\path\\app.zip");
if(file.exists()){
System.out.println("if condition");
downloaded = file.length();
System.out.println(downloaded);
httpUrlConnection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
else{
httpUrlConnection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
rbcObj = Channels.newChannel(urlObj.openStream());
fOutStream = new FileOutputStream(filePath,true);
fOutStream.getChannel().transferFrom(rbcObj, 0, Long.MAX_VALUE);
System.out.println("! File Successfully Downloaded From The Url !");
long endTime = System.currentTimeMillis();
System.out.println(endTime);
System.out.println(endTime-startTime);
// System.out.println(System);
} catch (IOException ioExObj) {
System.out.println("Problem Occured While Downloading The File= " + ioExObj.getMessage());
} finally {
try {
if(fOutStream != null){
fOutStream.close();
}
if(rbcObj != null) {
rbcObj.close();
}
} catch (IOException ioExObj) {
System.out.println("Problem Occured While Closing The Object= " + ioExObj.getMessage());
}
}
// } else {
// System.out.println("File Not Present! Please Check!");
// }
}
public static void main(String[] args) {
downloadFileFromUrlUsingNio();
// usingJavaNIO();
}
}
In the above code if you can see the below code of line
fOutStream.getChannel().transferFrom(rbcObj, 0, Long.MAX_VALUE);
this is for downloading the file , when I disable the internet connection(no internet) than the control is not comming to the next line
System.out.println("! File Successfully Downloaded From The Url !");
and not in catch block either
System.out.println("Problem Occured While Closing The Object= " + ioExObj.getMessage());
And what I am trying to accomplish is that when there is no internet the process(download) or the channel should close and the rest of the code executes normally but what actually happens is until I reconnect my internet it will not stop(and after connecting internet still it takes time).
So in simple terms my application will even wait for hours to stop the download when there is no internet and there will be no errors.
Please someone help me to overcome this scenario I just want it to stop when there is no internet.