Newer
Older
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
package org.josast.databaseSync;
import java.util.logging.Logger;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SendSftp {
private static Logger appLogger = Logger.getLogger("AmsatLogger");
private String remoteHost = null;
private String username = null;
private String password = null;
private String KnownHosts = "LATMOS_SSH";
private ChannelSftp channelSftp = null;
public SendSftp(String remoteHost, String username, String password) {
super();
this.remoteHost = remoteHost;
this.username = username;
this.password = password;
channelSftp = setupJsch();
}
public ChannelSftp setupJsch() {
Session jschSession = null;
ChannelSftp channel = null;
try {
JSch jsch = new JSch();
jsch.setKnownHosts(KnownHosts);
jschSession = jsch.getSession(username, remoteHost);
jschSession.setPassword(password);
jschSession.connect();
} catch (Exception e) {
appLogger.severe("error connection to " + remoteHost + " " + e.getMessage());
}
try {
channel = (ChannelSftp) jschSession.openChannel("sftp");
} catch (JSchException e) {
appLogger.severe("error to open channel sftp " + e.getMessage());
}
return channel;
}
public void upload(String filein, String fileout) {
try {
channelSftp.connect();
} catch (JSchException e) {
appLogger.severe("error to connect channel " + e.getMessage());
e.printStackTrace();
}
try {
channelSftp.put(filein, fileout);
} catch (SftpException e) {
appLogger.severe("error to send file " + e.getMessage());
e.printStackTrace();
}
System.out.println("sentn");
channelSftp.exit();
}
}