Overview
The article will demonstrate how to use the sftp component in Secure FTP Factory
to upload files and directories. This component can
be used to transfer files between a client and SSH (Secure Shell).
Code Example
Download SFTPExample.java
001 // required import statements
002 import java.io.*;
003 import java.util.*;
004
005 import com.jscape.inet.sftp.Sftp;
006 import com.jscape.inet.sftp.SftpException;
007 import com.jscape.inet.sftp.SftpFile;
008 import com.jscape.inet.sftp.events.SftpAdapter;
009 import com.jscape.inet.sftp.events.SftpConnectedEvent;
010 import com.jscape.inet.sftp.events.SftpDisconnectedEvent;
011 import com.jscape.inet.ssh.util.SshParameters;
012
013 public class SFTPExample extends SftpAdapter
014 {
015 private String hostname;
016 private String username;
017 private String password;
018 private Sftp sftp;
019
020 public SFTPExample(String hostname, String username, String password)
021 {
022 // initialize local SFTP instance with hostname, username and password.
023 sftp = new Sftp(new SshParameters(hostname,
024 username,
025 password));
026
027 // register the listener so this instance can recieve events.
028 sftp.addSftpListener(this);
029 }
030
031 // open connection to the remote server.
032 public void openConnection() throws SftpException
033 {
034 sftp.connect();
035 }
036
037 // disconnect from the remote server.
038 public void closeConnection()
039 {
040 sftp.disconnect();
041 }
042
043 // set binary transfer mode
044 public void setBinaryMode()
045 {
046 this.setAutoDetectFileMode(false);
047 sftp.setBinary();
048 }
049
050 // set ASCII transfer mode
051 public void setAsciiMode()
052 {
053 this.setAutoDetectFileMode(false);
054 sftp.setAscii();
055 }
056
057 // automatically detect the transfer mode
058 public void setAutoDetectFileMode(boolean value)
059 {
060 sftp.setAuto(value);
061 }
062
063 // provide a filename expression to upload
064 // e.g. :
065 // *.* upload all files from in the current local directory.
066 // .*\\.gif upload all files with the GIF image extension.
067 public void uploadFile(String expression) throws SftpException
068 {
069 sftp.mupload(expression);
070 }
071
072 // call this method if you want to append data to an existing file
073 // e.g.
074 // File src = new File("c:/tmp/logs.txt");
075 // String dest = "all-logs.txt";
076 // sftp.upload(src,dest,true);
077 public void uploadFile(String source, String dest, boolean append) throws SftpException
078 {
079 sftp.upload(new File(source), dest, true);
080 }
081
082 // upload a directory
083 // e.g. :
084 // uploadDir(new File("e:/somedir"));
085 // This call will upload "somedir" to the remote server
086 public void uploadDir(File file) throws SftpException
087 {
088 sftp.uploadDir(file);
089 }
090
091 // upload from memory
092 // e.g.
093 // String someData = "data";
094 // upload(someData.getBytes(), "data.txt");
095 public void upload(byte[] b, String file) throws SftpException
096 {
097 sftp.upload(b, file);
098 }
099
100 // set local directory to upload from
101 public void setLocalDir(String dir)
102 {
103 sftp.setLocalDir(new File(dir));
104 }
105
106 // set remote directory to upload to
107 public void setRemoteDir(String dir) throws SftpException
108 {
109 sftp.setDir(dir);
110 }
111
112 // this method is invoked when when client connects to the remote server.
113 public void connected(SftpConnectedEvent evt)
114 {
115 System.out.println("Connected to host: " + evt.getHostname());
116 }
117
118 // this method is invoked when client disconnects from the remote server.
119 public void disconnected(SftpDisconnectedEvent evt)
120 {
121 System.out.println("Disconnected from host: " + evt.getHostname());
122 }
123
124 public static void main(String[] args)
125 {
126 String hostname = null;
127 String username = null;
128 String password = null;
129 try
130 {
131 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
132 System.out.print("Enter hostname : ");
133 hostname = reader.readLine();
134 System.out.print("Enter username : ");
135 username = reader.readLine();
136 System.out.print("Enter password : ");
137 password = reader.readLine();
138 reader.close();
139
140 SFTPExample example = new SFTPExample(hostname, username, password);
141 example.openConnection();
142 example.setRemoteDir("/remoteDir");
143 example.setLocalDir("/localDir");
144 example.uploadFile("*.*");
145 example.closeConnection();
146 }
147 catch(Exception e)
148 {
149 e.printStackTrace();
150 }
151
152 }
153 }
Lines 131 - 138 : Get hostname, username and password from console. Line 140 : Create an instance of SFTPExample. Line 142 : Set the remote directory to upload to. Line 143 : Set the local directory to upload from. Lines 144 - 145 : Upload the file(s) and close the connection to remote server. Line 28 : To recieve events in SFTPExample from SftpAdapter we need to add the listener to the SFTPExample instance.
|
Comments