6 Replies - 7789 Views - Last Post: 17 December 2009 - 08:01 AM Rate Topic: -----

#1 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Progress Bar with FTP uploader

Posted 16 December 2009 - 10:56 PM

Alright so I got a program here that will connect to a server (via FTP) and upload a file (soon to upload just photos) and when each photo is uploading there will be a progress bar showing the progress. Right now I have two progress bars...one for the overall progress and another that is the "" cause I have no clue how to implement that right yet. Here is the problem: When I press the start button on the uploading screen (card 4 - after the convert card) it just freezes and unfreezes when the upload(s) have completed. I have no clue what is going on here...anyone care to help me out? (try uploading a 1MB file...just freezes...fml...lol)

the ftp credentials in the code here work and files are uploaded to http://www.scottymedia.com/apptest

the library I am using is org.apache.commons.net.ftp (http://commons.apache.org/downloads/download_fileupload.cgi)

the uploading is done in uploadPhotos() in the view -- and I am using MVC

smapplication.java

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class smapplication extends JFrame{
	smview view;
smapplication( ){

super( "test app" );

//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated( true );

try {
	UIManager.setLookAndFeel(
			"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" );
			//"javax.swing.plaf.metal.MetalLookAndFeel" );
} catch ( Exception e ) {
	System.err.print( "Warning! could not set the Look and Feel. " );
	System.err.println( "Using the default." );
}

this.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );

//Create and set up the content pane.
view = new smview();
this.setJMenuBar(view.buildMenuBar());
view.setOpaque( true ); //content panes must be opaque
this.setContentPane( view );

//Display the window.
// By default, frame objects do not have an initial size, and
// are not visible.
// You, the programmer, must tell the frame its initial size.
this.pack();

// set the frame's size before centering
this.setSize( 600, 400 );
// center the frame on the screen
Dimension frameSize = this.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( Math.max(0,(screenSize.width - frameSize.width)/2),
				  Math.max(0,(screenSize.height - frameSize.height)/2));
}
public static void main( String[ ] args ){

//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
	public void run() {
		new smapplication().setVisible( true );
	}
 });
}

}



smmodel.java
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Observable;


public class smmodel extends Observable {

	// Version format is as follows
	// First number - Feature Added
	// Second number - Problem fixed
	// Third Number - Minor Tweak
	private static final String VERSION = "1.0.0";
	int position;
	int lock;
	private int currentphoto;
	private String procphoto;
	private String pgalleryname;
	private int totalphotos;
	private int uploaded;
	private int waiting;

	//public panels panel;

	smmodel(){
		position = 0; // on the welcome page
		lock = 0;
		currentphoto = 0;
		procphoto = "<< no file currently >>";
		totalphotos = 0;
		uploaded = 0;
		waiting = 0;
		pgalleryname = "";
		//panel = new panels();

	}

//checks for connection to the internet through dummy request
	public boolean isInternetReachable(){

		try {

			//make a URL to a known source
			URL url = new URL("http://www.google.com");

			//open a connection to that source
			HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection();

			//trying to retrieve data from the source. If there
			//is no connection, this line will fail
			Object objData = urlConnect.getContent();

		}catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return false;
		}

	return true; // internet is good

	}

	public void setUploaded(){
		this.uploaded++; // increase by one
		this.updateObservers();
	}
	public int getUploaded(){
		return this.uploaded;
	}
	public void setWaiting(int i){
		this.waiting = i;
	}
	public int getWaiting(){
		return this.waiting;
	}
	public void setGalleryName(String gallery){
		this.pgalleryname = gallery;
		this.updateObservers();
	}

	public String getGalleryName(){
		return this.pgalleryname;
	}

	public void setTotal(int total){
		this.totalphotos = total;
		this.updateObservers();
	}
	public int getTotal(){
		return this.totalphotos;
	}
	public void setLock(){
		lock = 1;
	}

	public int getLock(){
		return lock;
	}

	public void removeLock(){
		lock = 0;
	}

	public int getPosition(){
		return position;
	}

	public String getProgramVersion(){
		return VERSION;
	}

	public void back(){
		position--;
		this.updateObservers();
	}

	public void next(){
		position++;
		this.updateObservers();
	}
	public void setCurrentPhoto(int photo){
		this.currentphoto = photo;
		this.updateObservers();
	}

	public int getCurrentPhoto(){
		return this.currentphoto;
	}
	public void setProcPhoto(String photo){
		this.procphoto = photo;
		this.updateObservers();
	}

	public String getProcessedPhoto(){
		return this.procphoto;
	}

	private void updateObservers(){
		this.setChanged();
		this.notifyObservers();
	}
}



doesn't all fix in one....so....smview.java is in the file attached called smview.txt

I would be SOO greatful if somebody could help me out

Attached File(s)


This post has been edited by scottyadam: 16 December 2009 - 11:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Progress Bar with FTP uploader

#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: Progress Bar with FTP uploader

Posted 16 December 2009 - 11:10 PM

Where is your JProgressBar and where do you update it ?
Was This Post Helpful? 0
  • +
  • -

#3 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Progress Bar with FTP uploader

Posted 16 December 2009 - 11:13 PM

Didn't even realize that I didn't edit the value of it...that doesn't worry me too much....(i don't think)

what worries me is the fact that this:

progressPhoto (which is a JProgressBar - the gif one)

that I define like this: progressPhoto.setIndeterminate(false);

just freezes when i initiate the upload and unfreezes when i finish the upload...

This post has been edited by scottyadam: 16 December 2009 - 11:14 PM

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: Progress Bar with FTP uploader

Posted 16 December 2009 - 11:22 PM

View Postscottyadam, on 16 Dec, 2009 - 10:13 PM, said:

that I define like this: progressPhoto.setIndeterminate(false);

Where ?

Quote

just freezes when i initiate the upload and unfreezes when i finish the upload...

Not seeing your code not easy but if the FTP process monopolize the thread no repaint of the GUI will be done until the FTP finishes
Was This Post Helpful? 0
  • +
  • -

#5 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Progress Bar with FTP uploader

Posted 16 December 2009 - 11:27 PM

View Postpbl, on 16 Dec, 2009 - 10:22 PM, said:

View Postscottyadam, on 16 Dec, 2009 - 10:13 PM, said:

that I define like this: progressPhoto.setIndeterminate(false);

Where ?

I do this in the public void uploadPhotos() and it actually the parameter true...so that the bar starts moving...the line is progressPhoto.setIndeterminate(true);

View Postpbl, on 16 Dec, 2009 - 10:22 PM, said:

View Postscottyadam, on 16 Dec, 2009 - 10:13 PM, said:

just freezes when i initiate the upload and unfreezes when i finish the upload...

Not seeing your code not easy but if the FTP process monopolize the thread no repaint of the GUI will be done until the FTP finishes


so how do I make it (FTP) not momopolize and let the progress bar do it's thing. I have seen apps that can do that...did I just throw an epic failure here? :P

This post has been edited by scottyadam: 16 December 2009 - 11:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: Progress Bar with FTP uploader

Posted 17 December 2009 - 02:40 AM

Quote

so how do I make it (FTP) not momopolize and let the progress bar do it's thing


I would use a SwingWorker. See

http://www.java2s.co...ProgressBar.htm
Was This Post Helpful? 0
  • +
  • -

#7 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Progress Bar with FTP uploader

Posted 17 December 2009 - 08:01 AM

Alright how does code look (with modified to upload file)...it works...just takes FOREVER to upload a 200KB file....it takes like 3 minutes in this java application (for the file to upload) and about 15 seconds in Filezilla FTP client...why does it take so long??? the progress is displayed and updated....

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileInputStream;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import org.apache.commons.net.ftp.FTPClient;
public class ProgressBarDemo extends JPanel implements ActionListener,
	PropertyChangeListener {

  private JProgressBar progressBar;
  private JButton startButton;
  private JTextArea taskOutput;
  private Task task;
  FTPClient client;
	  final static String SERVER = "ftp.scottymedia.com";
	final static String USERNAME = "username@scottymedia.com";
	final static String PASSWORD = "password";

  class Task extends SwingWorker<Void, Void> {
	/*
	 * Main task. Executed in background thread.
	 */
	@Override
	public Void doInBackground() {
	  Random random = new Random();
	  int progress = 0;
	  // Initialize progress property.
	  setProgress(0);
	  while (progress < 100) {
		// Sleep for up to one second.
		try {

			FileInputStream fis = new FileInputStream("G:\\Adam Pictures\\Atown Pictures\\blah.jpg");

			if( !client.storeFile("./adam.jpg", fis) ){

				taskOutput.append("can't upload!\n");

			}else{

			   //JOptionPane.showMessageDialog(this,"upload successfull :: " + path + " :: " + filename);

			}

		}
		//catch(Exception e) { e.printStackTrace(); }
		catch(Exception e){
			//JOptionPane.showMessageDialog(this,e);
			taskOutput.append("blah"+e);
		}
		// Make random progress.
		progress += random.nextInt(10);
		setProgress(Math.min(progress, 100));
	  }
	  return null;

	}

	/*
	 * Executed in event dispatching thread
	 */
	@Override
	public void done() {
	  Toolkit.getDefaultToolkit().beep();
	  startButton.setEnabled(true);
	  setCursor(null); // turn off the wait cursor
	  taskOutput.append("Done!\n");
	}
  }

  public ProgressBarDemo() {
	super(new BorderLayout());

	// Create the demo's UI.
	startButton = new JButton("Start");
	startButton.setActionCommand("start");
	startButton.addActionListener(this);

	client = new FTPClient();
	progressBar = new JProgressBar(0, 100);
	progressBar.setValue(0);
	progressBar.setStringPainted(true);
	progressBar.setIndeterminate(true);

	taskOutput = new JTextArea(5, 20);
	taskOutput.setMargin(new Insets(5, 5, 5, 5));
	taskOutput.setEditable(false);
			// Connect to server and login
			try{
				client.connect(SERVER); // connect to the server
				if( !client.login(USERNAME, PASSWORD) ){ // login
					//JOptionPane.showMessageDialog(this,"can't login");
					//errormsg = "Error code: 2";
					//errorflag = 1;
				}
			}catch(Exception e){
				JOptionPane.showMessageDialog(this,e);
			}
	JPanel panel = new JPanel();
	panel.add(startButton);
	panel.add(progressBar);

	add(panel, BorderLayout.PAGE_START);
	add(new JScrollPane(taskOutput), BorderLayout.CENTER);
	setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

  }

  /**
   * Invoked when the user presses the start button.
   */
  public void actionPerformed(ActionEvent evt) {
	startButton.setEnabled(false);
	setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
	// Instances of javax.swing.SwingWorker are not reusuable, so
	// we create new instances as needed.
	task = new Task();
	task.addPropertyChangeListener(this);
	task.execute();
  }

  /**
   * Invoked when task's progress property changes.
   */
  public void propertyChange(PropertyChangeEvent evt) {
	if ("progress" == evt.getPropertyName()) {
	  int progress = (Integer) evt.getNewValue();
	  progressBar.setValue(progress);
	  taskOutput.append(String.format("Completed %d%% of task.\n", task
		  .getProgress()));
	}
  }

  /**
   * Create the GUI and show it. As with all GUI code, this must run on the
   * event-dispatching thread.
   */
  private static void createAndShowGUI() {
	// Create and set up the window.
	JFrame frame = new JFrame("ProgressBarDemo");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	// Create and set up the content pane.
	JComponent newContentPane = new ProgressBarDemo();
	newContentPane.setOpaque(true); // content panes must be opaque
	frame.setContentPane(newContentPane);

	// Display the window.
	frame.pack();
	frame.setVisible(true);
  }

  public static void main(String[] args) {
	// Schedule a job for the event-dispatching thread:
	// creating and showing this application's GUI.
	javax.swing.SwingUtilities.invokeLater(new Runnable() {
	  public void run() {
		createAndShowGUI();
	  }
	});
  }
}


This post has been edited by scottyadam: 17 December 2009 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1