//---------
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;public class DownLoader extends JPanel {
  protected URL downLoadURL;
  protected InputStream inputStream;
  protected OutputStream outputStream;
  protected byte[] buffer;  protected int fileSize;
  protected int bytesRead;  protected JLabel urlLabel;
  protected JLabel sizeLabel;
  protected JLabel completeLabel;
  protected JProgressBar progressBar;  public final static int BUFFER_SIZE=1000;
  protected boolean stopped;  public DownLoader() {
  }
  public DownLoader(URL url,OutputStream os) throws IOException
  {
    downLoadURL = url ;
    outputStream = os;
    bytesRead = 0;
    URLConnection urlConnnection = downLoadURL.openConnection();
    fileSize=urlConnnection.getContentLength();
    if(fileSize == -1)
       throw new FileNotFoundException(url.toString());     inputStream = new BufferedInputStream(urlConnnection.getInputStream());
     buffer=new byte[BUFFER_SIZE];
     buildLayout();     stopped = false;  }  protected void buildLayout()
  {
    JLabel label;    setLayout(new GridBagLayout());
    GridBagConstraints gbc=new GridBagConstraints();
    gbc.fill=GridBagConstraints.HORIZONTAL;
    gbc.insets=new Insets(5,10,5,10);    gbc.gridx=0;
    label=new JLabel("URL:",JLabel.LEFT);
    add(label,gbc);    label=new JLabel("Complete :",JLabel.LEFT);
    add(label,gbc);    label=new JLabel("Downloaded :",JLabel.LEFT);
    add(label,gbc);    gbc.gridx=1;
    gbc.gridwidth = GridBagConstraints.REMAINDER;    gbc.weightx=1;
    urlLabel=new JLabel(downLoadURL.toString());
    add(urlLabel,gbc);    progressBar = new JProgressBar(0,fileSize);
    progressBar.setStringPainted(true);
    add(progressBar,gbc);    gbc.gridwidth=1;
    completeLabel=new JLabel(Integer.toString(bytesRead));
    add(completeLabel,gbc);    gbc.gridx = 2;
    gbc.weightx = 0;
    gbc.anchor=GridBagConstraints.EAST;
    label=new JLabel("Size:",JLabel.LEFT);
    add(label,gbc);    gbc.gridx=3;
    gbc.weightx = 1;
    sizeLabel = new JLabel(Integer.toString(fileSize));
    add(sizeLabel,gbc);
   }  public static void main(String[] args) throws Exception
  {
      DownLoader dl=null;
      if(args.length < 2){
            System.out.println("You must specify the URL of the file to download end " +
                               "the name of the local file to which its contents will be written.");
            System.exit(0);
      }
      URL url=new URL(args[0]);
      FileOutputStream fos=new FileOutputStream(args[1]);
      try
      {
        dl=new DownLoader(url,fos);
       }catch(Exception e)
       {
        System.out.println("file '"+args[0] + "'does not exist");
        System.exit(0);
       }      JFrame f=new JFrame();
      f.getContentPane().add(dl);
      f.setSize(600,400);
      //f.pack();
      f.setVisible(true);
//----------------------------------------------------------------------------
      f.addWindowListener(new WindowAdapter()
                          {
                            public void WindowClosing(WindowEvent e)
                            { System.out.println("asdfadf");
                              System.exit(0);
                            }
                            });
//----------------------------------------------------------------------------
      dl.performDownload();
      }  public void performDownload()
  {
    int byteCount;
    while (( bytesRead < fileSize ) && (!stopped))
    {
      try{
      byteCount = inputStream.read(buffer);
      if (byteCount == -1)
      {
        stopped = true;
        break;
      }
      else
      {
        outputStream.write(buffer,0,byteCount);
        bytesRead +=byteCount;
        progressBar.setValue(bytesRead);
        completeLabel.setText(Integer.toString(bytesRead));
      }
      }
      catch(IOException ioe)
      {
        stopped = true;
        JOptionPane.showMessageDialog(this,ioe.getMessage(),"I/O Error",JOptionPane.ERROR_MESSAGE);
        break;
      }      }
          try
          {
            outputStream.close();
            inputStream.close();
          }
          catch(IOException ioe){};
    }
    }