Results 1 to 4 of 4

Thread: Progressbar FTP

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    Progressbar FTP

    I'm scripting an FTP Applet in Java and I want to have a Progressbar but it doesn't work.

    My code:

    ftpUpload.java
    Code:
    package mediaplayer;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class ftpUpload {
        protected ftpConnectie cconn;
        public final String localfile;
        public final String targetfile;
    
        public ftpUpload(String _host, String _user, String _password, String _localfile, String _targetfile) {
            cconn= new ftpConnectie(_host, _user, _password);
            localfile= _localfile;
            targetfile= _targetfile;
            doIt();
        }
    
        public ftpUpload(String _host, String _user, String _password, String _file) {
            cconn= new ftpConnectie(_host, _user, _password);
            localfile= _file;
            targetfile= _file;
            doIt();
        }
    
        protected void doIt() {
            try {
                progress p = new progress();
                
                int percVal = 0;
                int i = 0;
                double bytesUploaded = 0;
                int currentFileUpPercent = 0;
    
                File fileInfo = new File(localfile);
                OutputStream os= cconn.openUploadStream(targetfile);
                FileInputStream is=  new FileInputStream(localfile);
                byte[] buf= new byte[1024];
                int c;
                while ((i = is.read(buf)) >= 0) {
                    bytesUploaded += i;
                    percVal = (int) ((bytesUploaded / fileInfo.length()) * 100);
                    currentFileUpPercent = percVal;
                    p.vulProgress(currentFileUpPercent);
    
                    c= is.read(buf);
                    if (c<= 0)  break;
                    os.write(buf, 0, c);
                }
                os.close();
                is.close();
                cconn.close();
            } catch (Exception E) {
            }
        }
    }
    progress.java
    Code:
    package mediaplayer;
    import java.awt.*;
    import javax.swing.*;
    
    public class progress {
        JFrame test = new JFrame();
        JProgressBar pbProgress = new JProgressBar();
        JLabel lblNaam = new JLabel();
    
        public progress() {
            test.setLayout(new BorderLayout());
            lblNaam.setText("Progress: ");
            test.getContentPane().add(lblNaam, BorderLayout.NORTH);
            test.getContentPane().add(pbProgress, BorderLayout.SOUTH);
            test.setSize(300,150);
            test.setEnabled(true);
            test.setVisible(true);
        }
    
        public void vulProgress(int proc){
            pbProgress.setValue(proc);
        }
    
    }
    And in my main-project I have the following code under a button.

    Code:
        private void btnBestandActionPerformed(java.awt.event.ActionEvent evt) {                                           
            JFileChooser fc = new JFileChooser("C:/");
            MijnFilter filter = new MijnFilter();
            filter.addExtension("mp3");
            filter.addExtension("wav");
            filter.setDescription("Muziekbestanden");
            fc.setFileFilter(filter);
            if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                File bestand = fc.getSelectedFile();
                ftpUpload fu = new ftpUpload("*ftp-folder*","*username*,"*password*",
                        bestand.getAbsolutePath(),bestand.getName());
                txtBestandUploaden.setText(bestand.getAbsolutePath());
            }
        }

    If I run the code I see an empty JFrame if I upload a file and if the file is uploaded I can see all the items in my JFrame and a half filled Progressbar.
    The file uploads perfectly.
    Can anyone help me with this?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Progressbar FTP

    Well. First of all, you're getting a half filled progress bar, because you're reading 2 bytes in your while loop and only incrementing the progress bar by 1.

    Second of all, you're not seeing anything until the whole process is complete because the EventDispatcher (ED) is waiting for your operation to finish to set the value.. In other words, you're setting the value of the progress bar, but the swing is waiting for you to finish, then it'll invoke the vulProgress.

    If you want to fix this, you'll need to read about SwingUtilities
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    2

    Re: Progressbar FTP

    Now I'm working with a Task and that works perfectly
    Thanks for replying btw!

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Progressbar FTP

    I was going to recommend threading, but Timers are just as good
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width