[RESOLVIDO] Download com JProgressBar

0 respostas
java
rodriguesabner

Fala pessoal, eu custei pra achar isso mas consegui fazer, pra quem precisar vou deixar o método aqui:

public class Notifica extends javax.swing.JFrame {

public Notifica() {
    initComponents();
    barraProgresso.setVisible(false);

    //// Coloca no canto inferior direito da tela.
    pack();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
    int x = (int) rect.getMaxX() - this.getWidth();
    int y = (int) rect.getMaxY() - this.getHeight();
    setLocation(x, y);
    //// Coloca no canto inferior direito da tela.
}

private void baixar(){                                               
    SwingWorker worker = new SwingWorker() {
        @Override
        protected Object doInBackground() throws Exception {
            barraProgresso.setVisible(true);
            botaoAtualizar.setVisible(false);
            String pastaGeral = System.getProperty("user.home");
            try {
                String link = "suaUrl";
                URL url = new URL(link);
                URLConnection conn = url.openConnection();
                InputStream input = conn.getInputStream();
                int size = conn.getContentLength();

                barraProgresso.setMaximum(size);
                barraProgresso.setValue(0);

                int bufferSize = 512;
                byte[] buffer = new byte[bufferSize];

                String[] str = link.split("/");

                BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(pastaGeral + "\\" + str[str.length - 1]));

                int len = 0;
                while ((len = input.read(buffer, 0, bufferSize)) > 0) {
                    outStream.write(buffer, 0, len);
                    barraProgresso.setValue(barraProgresso.getValue() + len);
                }

                outStream.flush();
                outStream.close();

                barraProgresso.setStringPainted(true);
                barraProgresso.setValue(0);
                barraProgresso.setString("Abrindo Arquivo...");

                Process processo = Runtime.getRuntime().exec(pastaGeral + "setup.exe");
                System.out.println("Abrindo...");
                System.exit(0);
                int status = processo.waitFor();
                System.out.println("Instalador executado! Status: " + status);

            } catch (MalformedURLException ex) {
                Logger.getLogger(JFNotificaShow.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(JFNotificaShow.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
    };
    worker.execute();
}

Criado 3 de agosto de 2018
Respostas 0
Participantes 1