文件的背景下载

时间:2017-02-25 02:18:35

标签: java download background

我是新人。我有一个程序,我点击一个按钮,电影的下载开始,但当它下载它,我什么都不做,所以该程序将关闭或仍然冻结(我要下载的文件是2GB。我的下载代码如下:

所以我希望我可以在我的程序中做一些事情,如果有人知道如何添加进度条会很棒,所以我可以看到下载的进展情况。

if (!"Anschauen".equals(down.getText())) {
    try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gedown.txt", true)))) {
        bw.write(titles.getText());
        bw.newLine();

        String fileName = (titles.getText()+".mp4"); //The file that will be saved on your computer
        URL link = new URL("http://s180.zerocdn.to/dl/63974fae7073944b11a7cf3e72e36cec/58b0d089/mm59cc5df499f1c21806495fea50072438.mp4"); //The file that you want to download

        switch (titles.getText()) {
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
            case "doesnotmatter":
                link = new URL("doesnotmatter");
                break;
        }

        ByteArrayOutputStream out;
        try (InputStream in = new BufferedInputStream(link.openStream())) {
            out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1!=(n=in.read(buf)))
            {
                out.write(buf, 0, n);
            }
            out.close();
        }
        byte[] response = out.toByteArray();

        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(response);
        fos.close();
    }
}
else {
    System.out.println("Noch nicht fertig");
}

1 个答案:

答案 0 :(得分:0)

您应该使用主题(Official Info

public void run() {
    //This will be called when the thread starts.
    //Put the download code right here.
}

public static void main(String args[]) {
    //The class must implement Runnable. 
    Thread thread = new Thread(this, "Thread's name eg. Download  Thread");
    thread.start();
}