Opa… fiz um código rápido aqui para demonstrar um exemplo de download utilizando JProgressBar… ele também controla a taxa de transferência do download.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Time;
import java.text.DateFormat;
import java.text.NumberFormat;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
/**
*
* @author Lucas Bellin
*/
public class Download extends JFrame
{
// classe que extende um Thread para executar o download
// é necessário para que a Progress seja atualizado enquanto o download estiver sendo feito
public class Runner extends Thread
{
public void run()
{
try
{
startDownload();
}
catch ( Exception e )
{
System.out.println( "ERRO: " + e.toString() );
}
}
}
// método main que abre a janela com a Progress e um botão para iniciar ela
public static void main( String args[] )
{
new Download().setVisible( true );
}
// link do arquivo de download
public static final String link = "http://www.seulink.com/download/arquivo.zip";
/**
* Download
*
*/
public Download()
{
initComponents();
}
/**
* startDownload
*
*/
private void startDownload()
{
try
{
progress.setValue( 0 );
// arquivo local onde o arquivo do download será salvo
File tmp = new File( "/home/lb/Desktop/setup.zip" );
downloadFile( link, tmp );
System.out.println("DONE!");
}
catch ( Exception e )
{
e.printStackTrace();
}
}
/**
* downloadFile
*
* @param source String
* @param target File
* @throws Exception
*/
private void downloadFile( String source, File target ) throws Exception
{
jlMessage.setText( "Conectando ..." );
URLConnection conn = new URL( source ).openConnection();
int contentLength = conn.getContentLength();
progress.setMinimum( 0 );
progress.setMaximum( contentLength );
progress.setValue( 0 );
int bytesRead = 0;
BufferedInputStream in = new BufferedInputStream( conn.getInputStream() );
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( target ) );
byte[] buffer = new byte[4096]; // 4kB
jlMessage.setText( "Baixando ..." );
DateFormat tf = DateFormat.getTimeInstance();
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits( 1 );
nf.setMaximumFractionDigits( 1 );
nf.setGroupingUsed( true );
long timeStarted = System.currentTimeMillis();
while ( true )
{
int n = in.read( buffer );
if ( n == -1 )
{
break;
}
out.write( buffer, 0, n );
bytesRead += n;
progress.setValue( bytesRead );
long timeElapsed = System.currentTimeMillis() - timeStarted;
double downloadRate = ( bytesRead / 1000.0 ) / ( timeElapsed / 1000.0 );
long timeRemaining = (long) ( 1000 * ( ( contentLength - bytesRead ) / ( downloadRate * 1000 ) ) );
long timeArrival = timeStarted + timeElapsed + timeRemaining;
jlMessage.setText( "Inciado: " + tf.format( new Time( timeStarted ) ) +
", Chegada: " + tf.format( new Time( timeArrival ) ) +
", Transferência: " + nf.format( downloadRate ) + " kB/s" );
}
in.close();
out.close();
System.out.println( target.exists() );
jlMessage.setText( "Download concluído!" );
}
private void initComponents()
{
setLayout( new GridBagLayout() );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 400, 150 );
setLocationRelativeTo( null );
buttonsPane.setLayout( new FlowLayout() );
buttonsPane.add( btStart );
jlMessage.setFont( new Font( "SansSerif" , Font.PLAIN, 10 ) );
add( jlMessage, new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets( 5, 5, 5, 5 ), 0, 0 ) );
add( progress, new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets( 5, 5, 5, 5 ), 0, 0 ) );
add( buttonsPane, new GridBagConstraints( 0, 2, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets( 5, 5, 5, 5 ), 0, 0 ) );
btStart.addActionListener( new AbstractAction()
{
public void actionPerformed( ActionEvent e )
{
new Runner().start();
}
});
}
private JLabel jlMessage = new JLabel( "Message:" );
private JProgressBar progress = new JProgressBar();
private JPanel buttonsPane = new JPanel();
private JButton btStart = new JButton( "Start download" );
}
qualquer dúvida poste aí!! 