Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

j2ssh sftp put progress bar

807605Jul 21 2007
Hello,
I'm fresh beginner in Java. I have written an applet that uploads local file to other machine using ssh sftp protocol. I have based it on j2ssh package ver 0.2.8. The applet works perfect, but I don't know how to implement progress bar in it. I've been trying to use FileTransferProgress interface but somehow:) it's not working. I hope someone will help me to figure it out. I'm open for any ideas that will make this progress bar working.
Greets!

Here is my code...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.io.File;
import java.net.URL;

import org.apache.commons.logging.*;

import com.sshtools.j2ssh.*;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.FileTransferProgress;

import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.authentication.PublicKeyAuthenticationClient;

import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;

import com.sshtools.j2ssh.transport.publickey.SshPrivateKey;
import com.sshtools.j2ssh.transport.publickey.SshPrivateKeyFile;
import com.sshtools.j2ssh.transport.*;


import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.io.IOStreamConnectorState;


import com.sshtools.j2ssh.io.UnsignedInteger32;
import com.sshtools.j2ssh.session.SessionChannelClient;


import com.sshtools.j2ssh.sftp.*;
import com.sshtools.j2ssh.sftp.FileAttributes;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.SftpClient;


public class sftp extends JApplet implements ActionListener , FileTransferProgress{
	
	protected JFileChooser c;
	private JButton		b1 = new JButton("Dołącz plik wideo"),
				b2 = new JButton("Wyślij na serwer SFTP");
	
	private JTextField 	filename = new JTextField(50),
				dir = new JTextField(50);
	
	protected JProgressBar pb;
	protected long length, sent, bytesSoFar;
	protected sftp progress;
	protected String file_name;
	
	protected String file_read;			
	protected String file_write;
	protected String file_path;
	protected File file2;
	
	
	public void init() {
		//paramerty
		pb = new JProgressBar();
		pb.setMinimum(0);
		pb.setMaximum(100);
		length = sent = 0;
		pb.setStringPainted(true);
		pb.setString(0 + "/" + 0 + " kB");
		//layout
		b1.addActionListener(this);
		b1.setToolTipText("Dołącz plik i wyślij go na serwer");
		b2.addActionListener(this);
		Container cp = getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(b1);
		cp.add(b2);
		cp.add(filename);
		cp.add(dir);
		cp.add(pb); // progress bar

		file_read = getParameter("file_read");
		file_write = getParameter("file_write");
		file_path = getParameter("file_path");
		//File file = new File(file_read);
		
		progress = new FileTransferProgress();
	
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1) {
			
			File file = new File(file_read);
			File path = new File(file_path);
			JFileChooser c = new JFileChooser();
			c.setCurrentDirectory(path);
			c.setSelectedFile(file);
			int rVal = c.showOpenDialog(this);
			if(rVal == JFileChooser.APPROVE_OPTION) {
				filename.setText(c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName());
				dir.setText(c.getCurrentDirectory().toString());
				file_name = (c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName());
				File file2 = c.getSelectedFile();
				length = file2.length();
				repaint();
				c.setSelectedFile(null);
			}
			if(rVal == JFileChooser.CANCEL_OPTION) {
				filename.setText("Nacisnąłeś Anuluj");
				dir.setText("");
			}
		}
		else if(e.getSource()==b2) {
			repaint();
			SFTPConnect();
		}
	}
	
	public void SFTPConnect(){
		try {
			repaint();
			//filename.setText("Pr�ba połączenia 1...");
			String hostname = "sftp.host";
			String username = "user";
			String password = "pass";
			
						
			
			try {
			SshClient ssh = new SshClient();
			//ssh.setSocketTimeout(100);
			SshConnectionProperties properties = new SshConnectionProperties();
			properties.setHost(hostname);
			properties.setPrefPublicKey("ssh-dss");
			// Connect to the host
			ssh.connect(properties,new IgnoreHostKeyVerification());
			//set client
			PasswordAuthenticationClient pac = new PasswordAuthenticationClient();
			pac.setUsername(username);
			pac.setPassword(password);
			//Authenticate
			int result = ssh.authenticate(pac);
			if(result==AuthenticationProtocolState.FAILED || result==AuthenticationProtocolState.PARTIAL)
			filename.setText("failed or partial");
			else if(result==AuthenticationProtocolState.COMPLETE)
			{
				SendFile(ssh);
			}
			
			ssh.disconnect();
			} 
			catch (Exception e) {
			filename.setText("error");
			}
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void SendFile(SshClient ssh) {
		//dir.setText("Upload");
		try {
			
			SftpClient sftp2 = ssh.openSftpClient();
			
			sftp2.put(file_name, progress);
			
			sftp2.quit();
		}
		catch (Exception e) {
		
		filename.setText("error");
		}
	}
	
		
	public void updateProgressBar(long sent) {
		int percent = (int) ((float)sent / (float)length)* 100 ;
		pb.setValue(percent);
		pb.setString(sent/1024 + "/" + length/1024 + " kB");
		dir.setText("Upload zakończony"+percent);		
	}
	
 	public void started(long bytesTotal, String remoteFile) {
		
	}

	 public boolean isCancelled() {
		 return false;
	 }

	 
	 public void progressed(long bytesSoFar) {
		if (bytesSoFar>0)
		 updateProgressBar(bytesSoFar);
	 }
	 
	 public void completed(){
		 
		 
	 }
}
	
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2007
Added on Jul 21 2007
0 comments
598 views