Java下载任务定时超时退出
来源:互联网 发布:编程中if else怎么用 编辑:程序博客网 时间:2024/06/04 18:02
1. 主程序中代码
初始化LoadSomething开始准备
初始化DownloadTask对象
调用LoadSomething的对象调用DownloadTask对象进行计时运行
LoadSomething load =new LoadSomething();
DownloadTask downloadTask = new DownloadTask(URL, getDir());
// 开始下载,并设定超时限额为3毫秒
load.beginToLoad(downloadTask, 60000, TimeUnit.MILLISECONDS);
2. LoadSomething对象代码
importjava.io.FileNotFoundException;
import java.io.IOException;
importjava.util.concurrent.ExecutionException;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.Future;
importjava.util.concurrent.TimeUnit;
importjava.util.concurrent.TimeoutException;
public class LoadSomething {
// 线程池服务接口
private ExecutorServiceexecutor;
public LoadSomething() {
executor = Executors.newSingleThreadExecutor();
}
public voidbeginToLoad(DownloadTask task,long timeout,
TimeUnit timeType) {
Future<?> future = executor.submit(task);
try {
future.get(timeout,timeType);
task.throwException();
} catch (InterruptedExceptione) {
System.out.println("下载任务已经取消");
} catch (ExecutionExceptione) {
System.out.println("下载中发生错误,请重新下载");
} catch (TimeoutExceptione) {
System.out.println("下载超时,请更换下载点");
} catch (FileNotFoundExceptione) {
System.out.println("请求资源找不到");
} catch (IOExceptione) {
System.out.println("数据流出错");
} finally {
task.setStop(true);
// 因为这里的下载测试不用得到返回结果,取消任务不会影响结果
future.cancel(true);
}
}
}
DownloadTask代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import org.apache.commons.io.FileUtils;
class DownloadTask implements Runnable {
private Stringurl;
private Stringdir;
// 接收在run方法中捕获的异常,然后自定义方法抛出异常
private Throwableexception;
//是否关闭此下载任务
private boolean isStop = false;
public void setStop(booleanisStop) {
this.isStop =isStop;
}
public DownloadTask(Stringurl, String dir) {
this.url =url;
this.dir =dir;
}
/**
* 下载大数据
*
* @param filename
* @throws FileNotFoundException
* , IOException
*/
private void download() throws FileNotFoundException,IOException {
try {
URL httpurl = new URL(url);
String fileName = getFileNameFromUrl(url);
System.out.println(fileName);
File f = new File(dir +"/" + fileName);
FileUtils.copyURLToFile(httpurl,f);
} catch (Exceptione) {
e.printStackTrace();
}
}
public static StringgetFileNameFromUrl(String url) {
String name = new Long(System.currentTimeMillis()).toString()+".X";
int index = url.lastIndexOf("/");
if (index > 0) {
name = url.substring(index + 1);
if (name.trim().length() > 0){
returnname;
}
}
returnname;
}
public void throwException() throws FileNotFoundException,IOException {
if (exceptioninstanceof FileNotFoundException)
throw (FileNotFoundException)exception;
if (exceptioninstanceof IOException)
throw (IOException)exception;
}
@Override
public void run() {
try {
download();
} catch (FileNotFoundExceptione) {
exception = e;
} catch (IOExceptione) {
exception = e;
}
}
}
3.
- Java下载任务定时超时退出
- Java定时启动任务以及Heritrix超时处理
- scheduleAtFixedRate定时任务超时问题
- Java任务超时处理
- 定时任务,定时下载FTP指定文件
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- Java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- javaweb 定时任务 java定时任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务 每天定时执行任务
- java定时任务,每天定时执行任务
- 各种算法
- c#读取txt文件并生成一张表
- d
- ie浏览器兼容笔记
- 【机器学习】动手写一个全连接神经网络(三):分类
- Java下载任务定时超时退出
- 微信小程序
- java中判断一个字符串是否为整型数字
- tesseract_ocr 合并字库 字符识别基础及训练字库、
- Nosql学习第一天
- unicode utf8 与 ascii的关系
- ArcGIS网络分析之Silverlight客户端服务区分析(五)
- Linux常用性能调优工具索引
- HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析