Multithreaded Progress Bar file download program

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 20825 Views - Last Post: 29 May 2008 - 09:10 PM Rate Topic: -----

#1 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Multithreaded Progress Bar file download program

Posted 28 May 2008 - 03:32 PM

I want to create a program that can download multiple files (from anywhere internet or from the computer itself or something) on multiple threads (one per file) simultaneously, each with their own progress bar. I have viewed the progress bar tutorial (progress bar tutorial), and that has helped alot, but I still have some issues.

A few questions:

1) How could I download files? Easiest and simplest method, I have viewed alot of ways, but I'm relatively new to java so I'm rather unsure.

2) Would I be correct to assume that in order to create a progress bar per file, I would need to declare multiple progress bars initially?

3) For creating multiple threads, would I basically just replicate
new Thread(new thread1()).start();
per thread necessary, each including the repaint and setvalue methods?

4) I am kind of confused on how to measure the size of the file, and how would I measure the value of how much has been downloaded in order to pass it into the progress bar?

5) I have only worked with java through the NetBeans IDE, and through that haven't used applets yet, which is why i wanted to do this, is there anything aside from the implementation (through html code) that is vital to my program?

ty;

Is This A Good Question/Topic? 1

Replies To: Multithreaded Progress Bar file download program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 03:47 PM

Don't use an Applet, by default Applet can only access files from the server they are downloaded

Browse the API for URLConnection and HttpURLConnection

Yes you can build an Application with multiple JProgressBar and pass the JProgressBar to use to the constructor of your thread
Was This Post Helpful? 0
  • +
  • -

#3 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 04:02 PM

View Postpbl, on 28 May, 2008 - 03:47 PM, said:

Don't use an Applet, by default Applet can only access files from the server they are downloaded

Browse the API for URLConnection and HttpURLConnection

Yes you can build an Application with multiple JProgressBar and pass the JProgressBar to use to the constructor of your thread

Yes, but I intend to do this with an applet because I plan to incorporate all my mini-projects into applets and put them online.

Assuming I got the connection itself working, could I just make the URL the link of a file on the internet for download
i.e. mmorpg map download link:
URL url = new URL (http://mirror.silliam.com/dota/DotA%20Allstars%20v6.52c.w3x


If So, how would I read the download's progress?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 04:13 PM

View PostHypnotiqBIG, on 28 May, 2008 - 04:02 PM, said:

Yes, but I intend to do this with an applet because I plan to incorporate all my mini-projects into applets and put them online.

So your users will have to "registered" your Applet

View PostHypnotiqBIG, on 28 May, 2008 - 04:02 PM, said:

Assuming I got the connection itself working, could I just make the URL the link of a file on the internet for download

Guess so

View PostHypnotiqBIG, on 28 May, 2008 - 04:02 PM, said:

If So, how would I read the download's progress?


BufferedInputStream.available() might be your friend...
Was This Post Helpful? 1
  • +
  • -

#5 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 06:00 PM

So after using your help, and my previous code and some other internet sources and toying around a bit I ended up with this:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 * 
 *
 * @author Corey
 */
public class keke {
	static JFrame frmMain;
	static Container pane;
	static JButton btnDo;
	static JProgressBar barDo;
		static JFrame frmMain2;
	static Container pane2;
	static JButton btnDo2;
	static JProgressBar barDo2;
	static int aval, aval2;
	
	  public static void main (String[] args) throws Exception{
		  
				  try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
		catch (Exception e) {}
				  
		URL url2=new URL("http://www.dota.gamingtagz.com/maps/DotA%20Allstars%20v6.52c.w3x");
		URLConnection conn = url2.openConnection();
		conn.setDoInput(true);
		conn.connect();
		
		BufferedInputStream buff= new BufferedInputStream(conn.getInputStream());
		aval=buff.available();
		
				URL url=new URL("http://mirror.silliam.com/dota/DotA%20Allstars%20v6.52c.w3x");
		URLConnection conn2 = url2.openConnection();
		conn2.setDoInput(true);
		conn2.connect();
		
		BufferedInputStream buff2= new BufferedInputStream(conn2.getInputStream());
		aval2=buff.available();
		
		//Create all components
		frmMain = new JFrame("Sample progress bar application");
		frmMain.setSize(300, 100); //Window size 300x100 pixels
		pane = frmMain.getContentPane();
		pane.setLayout(null); //Use the null layout
		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit when X is clicked
		btnDo = new JButton("Go!");
		barDo = new JProgressBar(0, 100); //Min value: 0 Max value: 100	

		//Add components to pane
		pane.add(btnDo);
		pane.add(barDo);

		//Position controls (X, Y, width, height)
		barDo.setBounds(10, 10, 280, 20);
		btnDo.setBounds(100, 35, 100, 25);

		//Make frame visible
		frmMain.setResizable(false); //No resize
		frmMain.setVisible(true);

		//Add action listeners
		btnDo.addActionListener(new btnDoAction()); //Add the button's action
		///////////divide up the sections
		
		frmMain2 = new JFrame("Sample progress bar application");
		frmMain2.setSize(300, 100); //Window size 300x100 pixels
		pane2 = frmMain2.getContentPane();
		pane2.setLayout(null); //Use the null layout
		frmMain2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exit when X is clicked
		btnDo2 = new JButton("Go!");
		barDo2 = new JProgressBar(0, 100); //Min value: 0 Max value: 100	

		//Add components to pane
		pane2.add(btnDo2);
		pane2.add(barDo2);

		//Position controls (X, Y, width, height)
		barDo2.setBounds(10, 10, 280, 20);
		btnDo2.setBounds(100, 35, 100, 25);

		//Make frame visible
		frmMain2.setResizable(false); //No resize
		frmMain2.setVisible(true);

		//Add action listeners
		btnDo2.addActionListener(new btnDoAction()); //Add the button's action

	  }

	  //The action
	public static class btnDoAction implements ActionListener{
		public void actionPerformed (ActionEvent e){
			new Thread(new thread1()).start(); //Start the thread
			 new Thread(new thread2()).start();
		}
	}
	
	   //The thread
	public static class thread1 implements Runnable{
		public void run(){
				barDo.setValue(aval); //Set value
				barDo.repaint(); //Refresh graphics
//				try{Thread.sleep(50);} //Sleep 50 milliseconds
//				catch (InterruptedException err){}
//			}
		}
	}
	
	public static class thread2 implements Runnable{
		public void run(){
				barDo2.setValue(aval2); //Set value
				barDo2.repaint(); //Refresh graphics
//				try{Thread.sleep(50);} //Sleep 50 milliseconds
//				catch (InterruptedException err){}
//			}
		}
	}
}



This is major progress from what I had less than 7 hours ago, which goes along with my plan to work on this about 10 hours a week. ty alot so far btw, for your help.

Obviously I can run this more efficiently (obviously just passing values into the class instead of having the same code twice with different variables :rolleyes: ), but I usually need some visual aid to understand what I just coded.

Questions:

-What is the need for the sleep thread part leftover from when I copied the progress bar code? Any need to sleep the thread for X mills?

-Is there any problems with this code aside from the above-stated? Inefficiencies? Anything?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 08:00 PM

Do not see what you have a class thread1 and a class thread2
what will you do when you will want to support 100 downloads ? Classes thread1 to thread100 ?

Just have one class Download that receives in its constructor the URL to dowload and the JProgressBar to update in your main Frame
Was This Post Helpful? 0
  • +
  • -

#7 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 08:46 PM

Ok I can work on that, but I'm having some issues, I changed some variable names, but now their more self-explanatory;
	public static class thread1 implements Runnable{
		public void run(){
			for (int i=0; i<=aval1; i++){
				progbar1.setValue(i); //Set value
				progbar1.repaint(); //Refresh graphics
				//Sleep 50 milliseconds
				try{Thread.sleep(50);
				} 
				catch (InterruptedException err){
				}
			}
		}
	}

Is this code correct in doing what I want: updating the progress bar as java recieves the url's data (the input stream)?

Or do I need to incorporate something like this:
size1=Integer.valueOf(buff1.toString());
into the area with the url connections, and change the progress bar's max paramater to these variables and rework the for loop?

ty

EDIT

Here is my upgraded code:
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author Corey
 */
public class DownloadProgressBar {
	static JFrame frame1, frame2, frame3;
	static Container pane1, pane2, pane3;
	static JButton button1, button2, button3;
	static JProgressBar progbar1, progbar2, progbar3;
	static int aval1, aval2, aval3;
//	static int size1, size2, size3;
	
 public static void main (String[] args) throws Exception{	

	//Set look & feel
	try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
	catch (Exception e) {}
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////	Downloads	///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

	//Link 1
	URL url1=new URL("http://mirror.silliam.com/dota/DotA%20Allstars%20v6.52c.w3x");
	URLConnection conn1 = url1.openConnection();
		//Allow input from url, then connect
	conn1.setDoInput(true);
	conn1.connect();	
		//Get input stream, and all available bytes
	BufferedInputStream buff1= new BufferedInputStream(conn1.getInputStream());
  //  size1=Integer.valueOf(buff1.toString());
	aval1=buff1.available(); 
	
	//Link 2
	URL url2=new URL("http://www.dota.gamingtagz.com/maps/DotA%20Allstars%20v6.52c.w3x");
	URLConnection conn2 = url2.openConnection();
		//Allow input from url, then connect
	conn2.setDoInput(true);
	conn2.connect();   
		//Get input stream, and all available bytes
	BufferedInputStream buff2= new BufferedInputStream(conn2.getInputStream());
 //   size2=Integer.valueOf(buff2.toString());
	aval2=buff2.available();
	
	//Link 3
	URL url3=new URL("http://mirror.geesu.net/dota/download.php?f=6.48b.zip");
	URLConnection conn3 = url3.openConnection();
		//Allow input from url, then connect
	conn3.setDoInput(true);
	conn3.connect();
		//Get input stream, and all available bytes
	BufferedInputStream buff3= new BufferedInputStream(conn3.getInputStream());
  //  size3=Integer.valueOf(buff3.toString());
	aval3=buff3.available();
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////	Create Components	///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

	 //Window 1   
	frame1 = new JFrame("1Download in progress...");
		//Window size 300x100 pixels
	frame1.setSize(300, 100); 
	pane1 = frame1.getContentPane();
		//Use the 'null' layout
	pane1.setLayout(null); 
		//Exit when X (or close) is clicked
	frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

	button1 = new JButton("Begin Download");
		//Min value: 0 Max value: 100   
	progbar1 = new JProgressBar(0, 100);	  //size1
		//Add components to jpane
	pane1.add(button1);
	pane1.add(progbar1);
	   //Position/Size controls (X, Y, width, height))
	button1.setBounds(100, 35, 100, 25);
	progbar1.setBounds(10, 10, 280, 20);
		//Make frame visible & No resize
	frame1.setResizable(false); 
	frame1.setVisible(true);
		//Add action listeners (Add the button's action)
	button1.addActionListener(new begindl());
	
		 //Window 2
	frame2 = new JFrame("2Download in progress...");
		//Window size 300x100 pixels
	frame2.setSize(300, 100); 
	pane2 = frame2.getContentPane();
		//Use the 'null' layout
	pane2.setLayout(null); 
		//Exit when X (or close) is clicked
	frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

	button2 = new JButton("Begin Download");
		//Min value: 0 Max value: 100   
	progbar2 = new JProgressBar(0, 100); //size2	 
		//Add components to jpane
	pane2.add(button2);
	pane2.add(progbar2);
	   //Position/Size controls (X, Y, width, height)
	button2.setBounds(100, 35, 100, 25);
	progbar2.setBounds(10, 10, 280, 20);
		//Make frame visible & No resize
	frame2.setResizable(false); 
	frame2.setVisible(true);
		//Add action listeners (Add the button's action)
	button2.addActionListener(new begindl());
	
		 //Window 3  
	frame3 = new JFrame("3Download in progress...");
		//Window size 300x100 pixels
	frame3.setSize(300, 100); 
	pane3 = frame3.getContentPane();
		//Use the 'null' layout
	pane3.setLayout(null); 
		//Exit when X (or close) is clicked
	frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		//Make button
	button3 = new JButton("Begin Download");
		//Min value: 0 Max value: 100   
	progbar3 = new JProgressBar(0, 100);   //size3   
		//Add components to jpane
	pane3.add(button3);
	pane3.add(progbar3);
		//Position/Size controls (X, Y, width, height)
	button3.setBounds(100, 35, 100, 25);
	progbar3.setBounds(10, 10, 280, 20);
		//Make frame visible & No resize
	frame3.setResizable(false); 
	frame3.setVisible(true);
		//Add action listeners (Add the button's action)
	button3.addActionListener(new begindl());	
 }
	
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////	Execute	////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

	public static class begindl implements ActionListener{
		public void actionPerformed (ActionEvent e){
			new Thread(new thread1()).start();
			new Thread(new thread2()).start();
			new Thread(new thread3()).start();
			System.out.println(aval1+""+aval2+""+aval3);
		}
	}
	 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////	Threads	////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
	
	public static class thread1 implements Runnable{
		public void run(){
			for (int i=0; i<=aval1; i++){
				progbar1.setValue(i); //Set value
				progbar1.repaint(); //Refresh graphics
				//Sleep 50 milliseconds
				try{Thread.sleep(50);
				} 
				catch (InterruptedException err){
				}
			}
		}
	}
	
	public static class thread2 implements Runnable{
		public void run(){
			for (int i=0; i<=aval2; i++){ 
				progbar2.setValue(i); //Set value
				progbar2.repaint(); //Refresh graphics
				 //Sleep 50 milliseconds
				try{Thread.sleep(50);
				}
				catch (InterruptedException err){
				}
			}
		}
	}
	
	public static class thread3 implements Runnable{
		public void run(){
			for (int i=0; i<=aval3; i++){
				progbar3.setValue(i); //Set value of progress
				progbar3.repaint(); //Refresh graphics
				//Sleep 50 milliseconds
				try{Thread.sleep(50);
				} 
				catch (InterruptedException err){
				}
			}
		}
	}
}

This post has been edited by HypnotiqBIG: 28 May 2008 - 09:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 08:52 PM

	public static class thread1 implements Runnable{
		public void run(){
			for (int i=0; i<=aval1; i++){
				progbar1.setValue(i); //Set value
				progbar1.repaint(); //Refresh graphics
				//Sleep 50 milliseconds
				try{Thread.sleep(50);
				} 
				catch (InterruptedException err){
				}
			}
		}
	}


Don't know what aval1 is... is it the value returned by .available() ?
I don't see why you sleep... you artificially want to slow down the file/bytes transfert ?
No need to call repaint() the Java GUI will do you for you as soon as you change the value of he progressBar
Was This Post Helpful? 0
  • +
  • -

#9 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:06 PM

yes aval1 is the value returned by the BufferedInputStream.available().


I beleive that was there to add some delay to the progress bar's completion.

Is it that it's not downloading the file? Or is it that it's downloading it so fast that we can't see it's progress? Without the sleeps, it just finishes once clicked.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:20 PM

View PostHypnotiqBIG, on 28 May, 2008 - 09:06 PM, said:

yes aval1 is the value returned by the BufferedInputStream.available().


I beleive that was there to add some delay to the progress bar's completion.

Is it that it's not downloading the file? Or is it that it's downloading it so fast that we can't see it's progress? Without the sleeps, it just finishes once clicked.

Your choice....
I personnaly think it would be stupid to delay file downlaod to just show a nice GUI.
If the file downloads it 2 seconds good for it... the progress bar will fly....
If it has multiple megabytes no need to slow down the process
Don't need the "delay" that is done in another thread

This post has been edited by pbl: 28 May 2008 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:23 PM

So the for loop I am currently executing does do what I want it to? It downloads the file located at the link, and updates the progress bar accordingly?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:33 PM

View PostHypnotiqBIG, on 28 May, 2008 - 09:23 PM, said:

So the for loop I am currently executing does do what I want it to? It downloads the file located at the link, and updates the progress bar accordingly?

The loop just update the progressBar it does not download bytes from the file
What I am saying is that the sleep is useless
There are thousand of providers in the Internet that offer programs that are supposed to go faster than regular code... why would you put a sleep() in your download process ?
Was This Post Helpful? 0
  • +
  • -

#13 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:43 PM

View Postpbl, on 28 May, 2008 - 11:33 PM, said:

View PostHypnotiqBIG, on 28 May, 2008 - 09:23 PM, said:

So the for loop I am currently executing does do what I want it to? It downloads the file located at the link, and updates the progress bar accordingly?

The loop just update the progressBar it does not download bytes from the file
What I am saying is that the sleep is useless
There are thousand of providers in the Internet that offer programs that are supposed to go faster than regular code... why would you put a sleep() in your download process ?

ohh ok i will remove that, also any pointers on how (and what) i would need to change in this code in order to make it download the files? OBviously I would have to turn this into one class just called upon, instead of having the 'thread1 etc.' all numbered, what else? Does it involve getcontent ?

This post has been edited by HypnotiqBIG: 28 May 2008 - 09:45 PM

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:48 PM

View PostHypnotiqBIG, on 28 May, 2008 - 09:43 PM, said:

View Postpbl, on 28 May, 2008 - 11:33 PM, said:

View PostHypnotiqBIG, on 28 May, 2008 - 09:23 PM, said:

So the for loop I am currently executing does do what I want it to? It downloads the file located at the link, and updates the progress bar accordingly?

The loop just update the progressBar it does not download bytes from the file
What I am saying is that the sleep is useless
There are thousand of providers in the Internet that offer programs that are supposed to go faster than regular code... why would you put a sleep() in your download process ?

ohh ok i will remove that, also any pointers on how (and what) i would need to change in this code in order to make it download the files? OBviously I would have to turn this into one class just called upon, instead of having the 'thread1 etc.' all numbered, what else? Does it involve getcontent ?

It involes a BufferedInpuStrean that read bytes from your URL and stores them somewhere.
Was This Post Helpful? 0
  • +
  • -

#15 HypnotiqBIG  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 28-May 08

Re: Multithreaded Progress Bar file download program

Posted 28 May 2008 - 09:52 PM

Can you elaborate?

My ultimate intention for this program is to have the user input link/path, then it goes to the download link/path and downloads the file located there.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3