如何解决使用java nio没有互联网连接超过10分钟时下载文件过程没有结束
让我总结一下我的问题我正在尝试使用 java nio 下载文件,因为我还编写了代码,用于在您再次运行程序时恢复文件下载,但我的问题是当没有互联网连接时下载过程没有停止(我的意思是当没有互联网时,代码不会转到下一行,无一例外,它只是等待互联网恢复。)
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,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();
}
}
在上面的代码中,如果你能看到下面的代码行
fOutStream.getChannel().transferFrom(rbcObj,Long.MAX_VALUE);
这是用于下载文件,当我禁用互联网连接(无互联网)时,控件不会进入下一行
System.out.println("! File Successfully Downloaded From The Url !");
也不在catch块中
System.out.println("Problem Occured While Closing The Object= " + ioExObj.getMessage());
我想要完成的是,当没有互联网时,进程(下载)或频道应该关闭,其余代码正常执行,但实际发生的是,直到我重新连接互联网,它才会停止(连接互联网后仍然需要时间)。
所以简单来说,我的应用程序甚至会在没有互联网并且没有错误的情况下等待几个小时来停止下载。
请有人帮助我克服这种情况,我只想在没有互联网时停止。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。