PDA

View Full Version : Progressbar FTP


Badeendje
02-15-2009, 06:26 PM
I'm scripting an FTP Applet in Java and I want to have a Progressbar but it doesn't work.

My code:

ftpUpload.java
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
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.

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?