Search This Blog

Free, Simple and Light weight Download Manager Java Application - Application and Source Code

This is a free Internet Download Manager Application for PC developed in java and free to use. It is
very light weight with size only below 50KB. This download manager allows pause and resume for downloads and requires java jre 1.8 to run. The program source code is given below. This will help you to understand how to download files from internet using java. The program may help you to understand how to pause and resume downloads in a java program. The application also provides drag and drop support. You can drag a link to file to be downloaded from browser in to the drag and drop area (drop target) in the application and it will automatically start download or add to the download queue. This will also help you study how to use Drag and Drop and how to set a drop target.

Download free Download Manager Application for Computer


You may download the  lightweight java Download Manager Software from following link:

https://drive.google.com/file/d/0B5WYaNmq5rbueUl5bW03VllIc2M/view?usp=sharing

View Source code




import java.awt.Color; import java.awt.Desktop; import java.awt.Font; import java.awt.datatransfer.DataFlavor; import java.awt.dnd.DnDConstants; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableModel; public class DownloadManager extends JFrame{     File QueBackup;     boolean cancel=false;     DownloadManager d;     JLabel status;     ArrayList<task> Queue=new ArrayList<>();     ActionListener al;     JLabel DropBox;     long size;     long currentSize;     JButton addTask;     String droppedLink="";     JTable jtb;     DefaultTableModel tableModel;     boolean tableunlocked=true;     public DownloadManager()     {           setSize(288,277);         setLocationRelativeTo(null);         setAlwaysOnTop(true);         setResizable(false);         QueBackup=new File("C:\\OracleConfig\\QueBackup");         getContentPane().setLayout(null);         addTask=new JButton("Add new Download");         this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);         JButton cancelb=new JButton("Cancel a Download");         jtb=new JTable();         String col[] = {"No","Link","FileName","Size", "Completed"};         tableModel = new DefaultTableModel(col, 0);         jtb.setModel(tableModel);         jtb.setDragEnabled(false);         JScrollPane jsp=new JScrollPane();         JButton ShowList=new JButton("Show List");         ShowList.setFont(ShowList.getFont().deriveFont(Font.BOLD,14));         ShowList.setSize(273,30);         DropBox=new JLabel();         try {             if(QueBackup.exists()&&QueBackup.isFile())             {             ObjectInputStream ois=new ObjectInputStream(new FileInputStream(QueBackup));             Queue=(ArrayList<task>)ois.readObject();             ois.close();             }         for(int i=0;i<Queue.size();i++)             {             float flsa=(float)Queue.get(i).size/1000/1000;             float ffff=(float)Queue.get(i).downloaded/1000/1000;             Object[] rowob={i+1,Queue.get(i).address,Queue.get(i).filename,flsa,ffff};             tableModel.addRow(rowob);             }             } catch (Exception ex) {JOptionPane.showMessageDialog(d,ex.getMessage());}         al=new ActionListener() {             @Override             public void actionPerformed(ActionEvent e) {                 String btntext=((JButton)e.getSource()).getText();                 if(btntext.equals("Cancel a Download"))                     {                     int intex;                     String inputstring=JOptionPane.showInputDialog(d, "Enter the Serial number in Download queue");                     try{                     intex=Integer.parseInt(inputstring);                     if(intex==1)                         cancel=true;                     else                         {                         Queue.remove(intex-1);                         UpdateTable();                         }                     status.setText(Integer.toString(intex)+"th Download Cancelled!");                     }catch(Exception ex)                     {                     JOptionPane.showMessageDialog(d, "Enter a valid number between 1 and "+Integer.toString(Queue.size()));                     }                                     }                 else if(btntext.equals("Show List"))                     {                     for(int i=0;i<20;i+=1)                         {                         setSize(getWidth(),getHeight()+10);                         try{Thread.sleep(30);}catch(Exception ex){JOptionPane.showMessageDialog(d,ex.getMessage());}                         }                     ((JButton)e.getSource()).setText("Hide List");                     }                 else if(btntext.equals("Hide List"))                     {                     for(int i=0;i<20;i+=1)                         {                         setSize(getWidth(),getHeight()-10);                         try{Thread.sleep(30);}catch(Exception ex){JOptionPane.showMessageDialog(d,ex.getMessage());}                         }                     ((JButton)e.getSource()).setText("Show List");                     }                 else                     {                     String t=droppedLink;                     if(droppedLink.equals(""))                         t=JOptionPane.showInputDialog(d,"Enter address:");                     if(t!=null)                         {                         task temp=new task();                         temp.address=t;                         droppedLink="";                         int j=t.lastIndexOf(".");                         String fname;                         if(j==-1)                             {                             if(t.indexOf("googlevideo.com")!=-1)                                 fname="youtubevideo "+new Date().toString().replaceAll(":"," ")+".mp4";                             else fname=new Date().toString().replaceAll(":"," ");                             }                         else if(t.length()-j>8)                             fname=new Date().toString().replaceAll(":"," ");                         else                             fname=t.substring(t.lastIndexOf("/"));                         temp.filename="C:\\OracleConfig\\"+fname;                         URL url;                         try {                         url = new URL(t);                         HttpURLConnection ucn=(HttpURLConnection)url.openConnection();                         ucn.setRequestMethod("HEAD");                         ucn.getInputStream();                         temp.size=ucn.getContentLengthLong();                         ucn.disconnect();                         } catch (Exception ex) {                         JOptionPane.showMessageDialog(d, ex.getMessage());                         }                         Queue.add(temp);                     float ttt=(float)temp.size/1000/1000;                     String filenamee=temp.filename.substring(temp.filename.lastIndexOf("\\")+1);                     tableModel.addRow(new Object[]{Queue.size(),temp.address,filenamee,ttt,0});                     }                   }             }};         d=this;         cancelb.addActionListener(al);         addTask.addActionListener(al);         status=new JLabel("Starting.");         status.setVerticalAlignment(SwingConstants.CENTER);         status.setHorizontalAlignment(SwingConstants.CENTER);         status.setSize(273,30);         getContentPane().add(status);         status.setLocation(5,5);         addTask.setSize(273,30);         getContentPane().add(addTask);         addTask.setLocation(status.getX(),status.getY()+status.getHeight()+5);         DropBox.setSize(273,100);         DropBox.setBorder(BorderFactory.createLineBorder(Color.BLUE,3));         DropBox.setHorizontalAlignment(SwingConstants.CENTER);         DropBox.setVerticalAlignment(SwingConstants.CENTER);         getContentPane().add(DropBox);         DropBox.setLocation(addTask.getX(),addTask.getY()+addTask.getHeight()+5);         DropBox.setText("<html><font size=\"4\"><b>Drag n Drop Links Here</b></font></html>");         getContentPane().add(cancelb);         cancelb.setSize(273,30);         cancelb.setLocation(status.getX(),DropBox.getY()+DropBox.getHeight()+5);         getContentPane().add(ShowList);         ShowList.setLocation(cancelb.getX(),cancelb.getY()+cancelb.getHeight()+5);         getContentPane().add(jsp);         jsp.setSize(273, 190);         jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);         jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);         jsp.setLocation(ShowList.getX(),ShowList.getY()+ShowList.getHeight()+5);         jtb.setCellSelectionEnabled(false);         jtb.getColumnModel().getColumn(0).setPreferredWidth(30);         jtb.getColumnModel().getColumn(1).setPreferredWidth(90);         jtb.getColumnModel().getColumn(2).setPreferredWidth(80);         jtb.getColumnModel().getColumn(3).setPreferredWidth(35);         jtb.getColumnModel().getColumn(4).setPreferredWidth(35);         jsp.setViewportBorder(BorderFactory.createLineBorder(Color.yellow));         jsp.getViewport().add(jtb);         ShowList.addActionListener(al);         DropBox.setDropTarget(new DropTarget(addTask,new DropTargetListener() {             @Override             public void dragEnter(DropTargetDragEvent dtde) {}             @Override             public void dropActionChanged(DropTargetDragEvent dtde) {}             @Override             public void dragExit(DropTargetEvent dte) {}             @Override             public void drop(DropTargetDropEvent dtde) {                 try {                     dtde.acceptDrop(DnDConstants.ACTION_COPY);                     String link=(String)dtde.getTransferable().getTransferData(DataFlavor.fragmentHtmlFlavor);                     int i=link.indexOf("<a href=\"")+9;                                         if(i==-1)                     {                         JOptionPane.showMessageDialog(d,"Invalid link:"+link);                         dtde.dropComplete(false);                     }                     else                     {                         int j=link.indexOf("\">",i);                         link=link.substring(i,j);                         droppedLink=link;                         addTask.doClick();                         dtde.dropComplete(true);                     }                 } catch (Exception ex) {                     JOptionPane.showMessageDialog(d, ex.getMessage());                 }             }             @Override             public void dragOver(DropTargetDragEvent dtde) {}         }));         Runner.setPriority(8);         Runner.start();     }     public static void main(String[] args) {         try{               File MutexFile = new File(System.getProperty("java.io.tmpdir") + "\\JavaTongueDManagerRunning");     MutexFile.deleteOnExit();     if (MutexFile.exists()) {       return;     }     try     {       MutexFile.createNewFile();     }     catch (Exception ex) {}         DownloadManager t=new DownloadManager();         t.setVisible(true);                     Runtime.getRuntime().addShutdownHook(new Thread()             {                    public void run() {                 ObjectOutputStream ois=null;                 try {                                         if(t.QueBackup.exists())                         t.QueBackup.delete();                     t.QueBackup.createNewFile();                     ois = new ObjectOutputStream(new FileOutputStream(t.QueBackup));                     ois.writeObject((Object)t.Queue);                     ois.close();                         if (MutexFile.exists())                             MutexFile.delete();                     Desktop.getDesktop().browse(new URL("http://javatongue.blogspot.com/").toURI());                     }catch(Exception ee){JOptionPane.showMessageDialog(t,ee);}             }             }             );                         t.addComponentListener(new ComponentAdapter() {             @Override             public void componentHidden(ComponentEvent e) {                                 System.exit(0);                               }         });        }catch(Exception ex)                 {                 return;                 }     }     public void UpdateTable() {     tableunlocked=false;     tableModel.setRowCount(0);     for(int i=0;i<Queue.size();i++)         {         float flsa=(float)Queue.get(i).size/1000/1000;         float ffff=(float)Queue.get(i).downloaded/1000/1000;         Object[] rowob={i+1,Queue.get(i).address,Queue.get(i).filename,flsa,ffff};         tableModel.addRow(rowob);         }     tableunlocked=true; }     Thread Runner=new Thread() {   public void run()   {       while(true)       {        try{Thread.sleep(1000);}catch(Exception ex){JOptionPane.showMessageDialog(d,ex.getMessage());}        if(Queue.size()>0)        {        status.setText("Download starting..");        try {             File f;             int i=0;             URL url=new URL(Queue.get(i).address);             f=new File(Queue.get(i).filename);             HttpURLConnection ucn=(HttpURLConnection)url.openConnection();             ucn.setRequestProperty("Range", "bytes="+Queue.get(i).downloaded+"-");             ucn.connect();             size=ucn.getContentLengthLong();             Queue.get(i).size=size+Queue.get(i).downloaded;             size=Queue.get(i).size;             InputStream is=ucn.getInputStream();             long currsize=Queue.get(i).downloaded;             FileOutputStream fo=new FileOutputStream(f,true);             size=Queue.get(i).size;             tableModel.setValueAt((float)(size/1000/1000),i,3);             byte[] buf=new byte[1000000];             float perc,MBsize;             int len;             MBsize=(float)((float)size/1000/1000);             String MBsizestr=Float.toString(MBsize).substring(0,5);             status.setText("<HTML>Work in progress ("+Integer.toString(Queue.size()-1)+" tasks remaining)."+"<br/>Current: "+Queue.get(i).address+"<br/>ext= "+Queue.get(i).filename+"</HTML>");             while((len=is.read(buf))>0)             {                 currsize+=len;                 fo.write(buf,0, len);                 if(cancel)                     break;                 if(tableunlocked)                 {                 if(size!=-1)                     {                     perc=(float)currsize*100/size;                     status.setText("Progress: "+Double.toString(perc).substring(0,5)+"% of "+MBsizestr+" MB.");                     }                 Queue.get(i).downloaded=currsize;                 perc=(float)currsize/1000/1000;                 tableModel.setValueAt(perc,i,4);                 }             }             fo.close();             tableModel.removeRow(i);             if (cancel)                 {                 f.delete();                 status.setText("<HTML>"+Queue.get(i).address+" cancelled.</HTML>");                 cancel=false;                 }             else                 status.setText("<HTML>"+Queue.get(i).address+"  completed.</HTML>");             Queue.remove(i);             for(int m=0;i<Queue.size();i++)                 tableModel.setValueAt(m+1,m,0);             } catch (Exception ex) {                 JOptionPane.showMessageDialog(null, ex.getMessage());             }         }        else            status.setText("<HTML>Queue is Empty. </HTML>");       }   } }; } class task implements Serializable{ public String address; public long size; public long downloaded; public String filename; public task() {     downloaded=0; } }

No comments:

Post a Comment