Overview
This article will demonstrate how to transfer files using SCP in Java using Secure iNet Factory.
Code Example
Download SCPEx.java
01 import java.io.*;
02 import com.jscape.inet.scp.*;
03 import com.jscape.inet.scp.events.*;
04 import com.jscape.inet.ssh.util.SshParameters;
05
06 public class SCPEx implements ScpEventListener
07 {
08 private Scp scp = null;
09
10 public SCPEx(String hostname, String username, String password)
11 {
12 // create new SshParamters instance
13 SshParameters params = new SshParameters(hostname,username,password);
14 // create new Scp instance
15 Scp scp = new Scp(params);
16 // register event listener
17 scp.addListener(this);
18 }
19
20 public void openConnection() throws ScpException
21 {
22 scp.connect();
23 }
24
25 public void closeConnection() throws ScpException
26 {
27 scp.disconnect();
28 }
29
30 public void uploadFile(String filename, String destination) throws ScpException, IOException
31 {
32 scp.upload(new File(filename), destination);
33
34 }
35
36 public void downloadFile(String remoteDir, String remoteFile) throws ScpException, IOException
37 {
38 scp.download(remoteDir, remoteFile);
39 }
40
41 public static void main(String[] args)
42 {
43 try
44 {
45 // collect connection details and launch example
46 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
47 System.out.print("Enter SCP hostname: ");
48 String hostname = reader.readLine();
49 System.out.print("Enter SCP username: ");
50 String username = reader.readLine();
51 System.out.print("Enter SCP password: ");
52 String password = reader.readLine();
53 System.out.print("Enter file to upload e.g. c:/tmp/test.txt : ");
54 String filename = reader.readLine();
55 System.out.print("Enter destination directory with ending path separator e.g. /home/user/ : ");
56 String destination = reader.readLine();
57
58 // Create SCPEx instance
59 SCPEx example = new SCPEx(hostname,username,password);
60 // open connection to the server
61 example.openConnection();
62 // upload file
63 example.uploadFile(filename, destination);
64 // download file
65 example.downloadFile("/remoteDir/", "remoteFile");
66 // close connection
67 example.closeConnection();
68 }
69 catch (Exception e)
70 {
71 e.printStackTrace();
72 }
73 }
74
75 public void connected(ScpConnectedEvent arg)
76 {
77 // handle connected event
78 }
79
80 public void disconnected(ScpDisconnectedEvent arg)
81 {
82 // handle disconnected event
83 }
84
85 public void download(ScpFileDownloadedEvent arg)
86 {
87 // download event
88 }
89
90 public void progress(ScpTransferProgressEvent arg)
91 {
92 // progress event
93 }
94
95 public void upload(ScpFileUploadedEvent arg)
96 {
97 // upload event
98 }
99 }
Lines 1-4 : Necessary import statements. Line 13 : Create SshParameters instance. Line 15 : Create Scp instance. Lines 20-23 : openConnection() / closeConnection(). Lines 30-39 : methods for uploading/downloading files. Lines 46-67 : Read username, hostname, password, upload file, destination directory from console. Lines 75-98 : Events from ScpEventListener.
|
Comments