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.       

0 0
原创粉丝点击