简体   繁体   English

获取下载文件的文件名和分段下载

[英]Get file name of download file and segment downloading

Here is my code for downloading a file from a specified URL using a thread. 这是我的代码,用于使用线程从指定的URL下载文件。 I want to make it to a multi-threading download, but i cant work it out. 我想将其下载到多线程,但无法解决。 And i want to get the downloaded filename is the same as name of the file on URL + part.1,2,3.. etc. Can u guys help me out ? 我想下载的文件名与URL + part.1,2,3 ..等上的文件名相同。你们可以帮我吗? and sorry for my bad english grammar ! 对不起,我的英语语法不好! My favorite link for test this is : https://docs.oracle.com/javaee/7/JEETT.pdf Thannks all in advance ! 我最喜欢的测试链接是: https: //docs.oracle.com/javaee/7/JEETT.pdf提前致谢!

public class Threadtest extends Thread {
    String s ;

    public void InputUrl(){

        Scanner sc = new Scanner(System.in);
        System.out.println("Input URL :");
        s = sc.nextLine();


    }   
    public void run(){

        BufferedInputStream bis = null ;
        RandomAccessFile raf = null ; 
        HttpURLConnection conn = null;
        URL mUrl = null ;
        String a = new File(mUrl.getPath().toString()).getName();           
         try{

             mUrl = new URL(s);
              conn = (HttpURLConnection)mUrl.openConnection();
              String byteRange = 0+ "-" + 1024;
                conn.setRequestProperty("Range", "bytes=" + byteRange);

             conn.connect();    
             bis = new BufferedInputStream(conn.getInputStream());
                raf = new RandomAccessFile(a, "rw");
                raf.seek(0);

             byte[] buffer = new byte[4092];
             int count = 0 ;
             while ((count = bis.read(buffer,0,4092))!= -1)
             {

                raf.write(buffer, 0 ,count);
             }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

} 

Extending Thread is not so good idea. 扩展Thread不是一个好主意。 Better, by philosophical reason, is to implements Runnable interface. 从哲学的角度来看,更好的方法是实现Runnable接口。

class DownloadTask implements Runnable {

    private final URL url;

    DownloadTask(URL url) {
        this.url = url;
    }

    @Override
    public void run() {
        // download code
    }
}

now we have code to download only. 现在我们只有代码可供下载。 Next step is to use Executor Framework to execute our download tasks. 下一步是使用Executor Framework执行我们的下载任务。

class Downloader{

    private static boolean exitFlag;

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        while(exitFlag) {
            URL fileUrl = readUrlFromUserOrSetExit();
            pool.execute(new DownloadTask(fileUrl));
        }
        pool.shutdown();
    }

    private static URL readUrlFromUserOrSetExit() {
        // read from user of set exit flag
    }
}

How does it works? 如何运作?

  • You create executor named pool that is responsible for all "thread magic". 您创建名为pool执行程序,该执行程序负责所有“线程魔术”。
  • while user not put exit command you read file URL 当用户不输入退出命令时,您将读取文件URL
  • create DownloadTask instance 创建DownloadTask实例
  • send it to pool to execute . 将其发送到pool execute
  • if user put exit command then you shutdown executor. 如果用户放置了exit命令,那么您将shutdown执行程序。

It is probably the easiest way if you don't want to use external libs. 如果您不想使用外部库,这可能是最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM