-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSFTP.java
More file actions
188 lines (169 loc) · 4.54 KB
/
Copy pathSFTP.java
File metadata and controls
188 lines (169 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package javaforce;
import java.io.*;
import java.util.*;
import javaforce.*;
import javaforce.awt.*;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.future.ConnectFuture;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.sftp.client.SftpClient;
import org.apache.sshd.sftp.client.impl.DefaultSftpClientFactory;
/** FTP over SSH.
*
* Using Apache SSHD.
*
* @author pquiring
*/
public class SFTP {
private SshClient client; //ssh
private ClientSession session;
private SftpClient channel;
private String path;
private boolean aborted;
public boolean connect(String host, int port, String username, String password, String key) {
try {
client = SshClient.setUpDefaultClient();
client.start();
ConnectFuture cf = client.connect(username, host, port);
session = cf.verify().getSession();
//System.out.println("session = " + jschsession);
if (key == null || key.length() == 0) {
session.addPasswordIdentity(password);
} else {
JFLog.log("using key:" + key);
JFLog.log("TODO : set ssh key");
//session.addPublicKeyIdentity(sshKey);
}
session.auth().verify(30000);
channel = DefaultSftpClientFactory.INSTANCE.createSftpClient(session);
} catch (Exception e) {
JFAWT.showMessage("Error", "Error:" + e);
JFLog.log(e);
return false;
}
return true;
}
public void disconnect() {
try {
channel.close();
session.close();
} catch (Exception e) {}
}
public String remote_pwd() {
String wd;
try {
wd = channel.canonicalPath(path);
int i1 = wd.indexOf("\"");
if (i1 != -1) {
//the reply is ["path" is your current location]
int i2 = wd.lastIndexOf("\"");
if (i1 == i2) return "/";
wd = wd.substring(i1+1, i2);
}
} catch (Exception e) {
return "/";
}
return wd;
}
@SuppressWarnings("unchecked")
public String[] remote_ls() {
try {
Iterable<SftpClient.DirEntry> ls;
ls = channel.readDir(path);
ArrayList<String> files = new ArrayList<>();
for(SftpClient.DirEntry e : ls) {
files.add(e.getFilename());
}
return files.toArray(JF.StringArrayType);
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public void remote_chdir(String newpath) {
try {
if (newpath.startsWith("/")) {
path = channel.canonicalPath(newpath); //absolute
} else {
path = channel.canonicalPath(path + "/" + newpath); //relative
}
remote_ls();
} catch (Exception e) {
JFLog.log(e);
}
}
private static final int bufsiz = (1024 * 64);
private void copy(InputStream is, OutputStream os) throws Exception {
byte[] buf = new byte[bufsiz];
while (is.available() > 0) {
int toread = is.available();
if (toread > bufsiz) toread = bufsiz;
int read = is.read(buf, 0, toread);
if (read == -1) throw new Exception("read error");
if (read > 0) {
os.write(buf, 0, read);
}
}
}
public void download_file(File remote, File local) {
try {
FileOutputStream fos = new FileOutputStream(local);
InputStream is = channel.read(path + "/" +remote.getName());
copy(is, fos);
is.close();
} catch (Exception e) {
JFLog.log(e);
}
}
public void upload_file(File local, File remote) {
try {
FileInputStream fis = new FileInputStream(local);
OutputStream os = channel.write(path + "/" +remote.getName());
copy(fis, os);
} catch (Exception e) {
JFLog.log(e);
}
}
public void abort() {
aborted = true;
//TODO : channel.abort() ?
}
public void remote_mkdir(String file) {
try {
channel.mkdir(file);
} catch (Exception e) {
JFLog.log(e);
}
}
public void remote_delete_file(String file) {
try {
channel.remove(file);
} catch (Exception e) {
JFLog.log(e);
}
}
public void remote_rename(String from, String to) {
try {
channel.rename(from, to);
} catch (Exception e) {
JFLog.log(e);
}
}
public void setPerms(int value, String remoteFile) {
try {
SftpClient.Attributes attr = new SftpClient.Attributes();
attr.setPermissions(value);
channel.setStat(remoteFile, attr);
remote_chdir("."); //refresh
} catch (Exception e) {
JFLog.log(e);
}
}
public void remote_delete_folder(String file) {
try {
channel.rmdir(file);
} catch (Exception e) {
JFLog.log(e);
}
}
}