在我的代码中添加进度条以下载文件

时间:2015-04-21 19:40:07

标签: java progress-bar downloading

我有一个从网页下载文件的简单代码,我需要打开另一个显示下载进度的窗口。

下面。用代码查看我的文件:

Gui.java

package com.app;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Gui extends JFrame {
    public static void main(String[] args){
        }
        JButton button;
        public Gui(){
            super("PAYDAY 2 Mod Installer");
            setLayout(new FlowLayout());

            button = new JButton("Install");
            add(button);

            GuiHandler handler = new GuiHandler();
            button.addActionListener(handler);
        }

        private class GuiHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                //System.out.println("test");
                GetModFiles files = new GetModFiles();
                try {
                    files.main(null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
}

GuiHandler.java

package com.app;

import javax.swing.JFrame;

public class GuiHandler {
    public static void main(String[] args) {
        Gui go = new Gui();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(350, 200);
        go.setAlwaysOnTop(true);
        go.setVisible(true);
    }
}

GetModFile.java

package com.app;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class GetModFiles {

    public static void main(String[] args) throws IOException {

        String fileName = "PAYDAY2.Pre-Alpha.0.06.zip";

        URL link = new URL("http://download1337.mediafire.com/wie8ek7gv53g/dy5vtumtbxyvt6h/PAYDAY2.Pre-Alpha.0.06.zip");

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

        FileOutputStream fos = new FileOutputStream(fileName);
        fos.write(response);
        fos.close();
    }
}

现在如何让它打开一个新窗口(按下"安装"按钮后),显示进度条?

告诉我您是否需要更多详细信息。

有什么想法吗?

0 个答案:

没有答案