Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 后台下载文件_Java_Download_Background - Fatal编程技术网

Java 后台下载文件

Java 后台下载文件,java,download,background,Java,Download,Background,我是新来的。我有一个程序,我点击一个按钮,电影开始下载,但当它下载时,我什么都做不了,所以程序将关闭或仍然冻结(我要下载的文件是2GB。我的下载代码如下所示: 所以我希望我能在我的程序中做点什么,如果有人知道我如何添加进度条,那就太好了,这样我就能看到下载的进展了 if (!"Anschauen".equals(down.getText())) { try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new

我是新来的。我有一个程序,我点击一个按钮,电影开始下载,但当它下载时,我什么都做不了,所以程序将关闭或仍然冻结(我要下载的文件是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");
}
您应该使用线程()


您可能应该学习线程的概念(并且)将所有下载代码放在一个单独的类中,并将其作为线程运行。
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();
}