E ai galéra, eu estáva pesquisando como fazer download e arquivos pelo java e achei um exemplo bem facil de entender,
public File gravaArquivoDeURL(String stringUrl, JanelaDownload janela) {
try {
URL url = new URL(stringUrl);
String nomeArquivoLocal = url.getPath();
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(this.pegaNomeArquivo(nomeArquivoLocal)));
chooser.showDialog(null, "Salvar");
InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream(chooser.getSelectedFile());
int umByte = 0;
while ((umByte = is.read()) != -1){
fos.write(umByte);
}
is.close();
fos.close();
return new File(chooser.getSelectedFile().getPath());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
mas eu queria fazer uma gracinha, então peguei e fiz uma telinha Swing com um inputTex para colocar a URL e um botão para fazer o download, mas eu ainda quero mais, alguem poderia me ajudar a colocar uma barra de progresso nessa telinha, e faze-la andar de acordo com a Download, segue o codigo da telinha
public class JanelaDownload extends JFrame {
private static final long serialVersionUID = -249368712218645295L;
private JTextField inpuTextUrl;
private JLabel labelUrl;
private JButton buttonDownload;
protected JLabel message;
protected JProgressBar barra;
public JanelaDownload() {
this.getContentPane().setLayout(null);
this.getContentPane().setBackground(new Color(238, 238, 238));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(new java.awt.Dimension(330, 286));
this.setLocation((screenSize.width - 330) / 2, (screenSize.height - 286) / 2);
this.setTitle("Download");
this.setResizable(false);
message = new JLabel("INICIO");
message.setBounds(new Rectangle(133, 150, 200, 40));
this.getContentPane().add(message);
barra = new JProgressBar();
barra.setSize(200, 20);
barra.setBounds(new Rectangle(65, 200, 200, 40));
barra.setMaximum(0);
barra.setMaximum(100);
this.getContentPane().add(barra);
inpuTextUrl = new JTextField();
inpuTextUrl.setBounds(new Rectangle(27, 56, 277, 30));
this.getContentPane().add(inpuTextUrl, null);
labelUrl = new JLabel("Insira a URL");
labelUrl.setBounds(new Rectangle(31, 31, 150, 20));
this.getContentPane().add(labelUrl, null);
buttonDownload = new JButton("Download");
buttonDownload.setBounds(new Rectangle(94, 110, 143, 30));
this.getContentPane().add(buttonDownload, null);
buttonDownload.addMouseListener (new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
download();
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
aoFechar();
}
});
this.setVisible(true);
}
private void aoFechar() {
System.exit(0);
}
public void download(){
Download d = new Download();
if(!inpuTextUrl.getText().equals("")){
message.setText("BAIXANDO...");
d.gravaArquivoDeURL(inpuTextUrl.getText(), this);
message.setText("DOWNLOAD COMPLETO!");
}
}
public static void main(String args[]) {
new JanelaDownload();
}
}
Alguem me ajude!!!