/Users/lyon/j4p/src/net/scp/ScpTo.java
|
1 package net.scp;
2
3
4 import com.jcraft.jsch.*;
5 import futils.Futil;
6 import gui.In;
7
8 import javax.swing.JOptionPane;
9 import javax.swing.JPasswordField;
10 import javax.swing.JTextField;
11 import java.io.*;
12
13 public class ScpTo {
14 public static void main(String[] arg) {
15 scpTo();
16 }
17
18 public static void scpTo() {
19 String user = In.getString("user id");
20 String host = In.getString("host");
21
22 File lfile = Futil.getReadFile(
23 "local file");
24 String rfile = "/var/www/html/book/cgij/code/jnlp/"+lfile.getName();
25 UserInfo ui = new MyUserInfo();
26 scpTo(lfile.toString(),
27 user,
28 host,
29 rfile,
30 ui);
31 }
32
33
34 public static void scpTo(String lfile,
35 String user,
36 String host,
37 String rfile,
38 UserInfo ui) {
39 try {
40
41 JSch jsch = new JSch();
42 Session session = jsch.getSession(
43 user, host, 22);
44
45 session.setUserInfo(ui);
46 session.connect();
47
48
49 // exec 'scp -t rfile' remotely
50 String command = "scp -t " + rfile;
51 Channel channel = session.openChannel(
52 "exec");
53 ((ChannelExec) channel).setCommand(
54 command);
55
56 // get I/O streams for remote scp
57 OutputStream out = channel.getOutputStream();
58 InputStream in = channel.getInputStream();
59
60 channel.connect();
61
62 if (checkAck(in) != 0) {
63 System.exit(0);
64 }
65
66 // send "C0644 filesize filename", where filename should not include '/'
67 int filesize = (int) (new File(lfile)).length();
68 command = "C0644 " + filesize + " ";
69 if (lfile.lastIndexOf('/') > 0) {
70 command +=
71 lfile.substring(lfile.lastIndexOf(
72 '/') +
73 1);
74 } else {
75 command += lfile;
76 }
77 command += "\n";
78 out.write(command.getBytes());
79 out.flush();
80
81 if (checkAck(in) != 0) {
82 return;
83 }
84
85 // send a content of lfile
86 FileInputStream fis = new FileInputStream(
87 lfile);
88 byte[] buf = new byte[1024];
89 while (true) {
90 int len = fis.read(buf,
91 0,
92 buf.length);
93 if (len <= 0) break;
94 out.write(buf, 0, len);
95 out.flush();
96 }
97
98 // send '\0'
99 buf[0] = 0;
100 out.write(buf, 0, 1);
101 out.flush();
102
103 if (checkAck(in) != 0) {
104 System.exit(0);
105 }
106
107 return;
108 } catch (Exception e) {
109 System.out.println(e);
110 }
111 }
112
113 static int checkAck(InputStream in)
114 throws IOException {
115 int b = in.read();
116 // b may be 0 for success,
117 // 1 for error,
118 // 2 for fatal error,
119 // -1
120 if (b == 0) return b;
121 if (b == -1) return b;
122
123 if (b == 1 || b == 2) {
124 StringBuffer sb = new StringBuffer();
125 int c;
126 do {
127 c = in.read();
128 sb.append((char) c);
129 } while (c != '\n');
130 if (b == 1) { // error
131 System.out.print(sb.toString());
132 }
133 if (b == 2) { // fatal error
134 System.out.print(sb.toString());
135 }
136 }
137 return b;
138 }
139
140 public static class MyUserInfo
141 implements UserInfo {
142
143
144 public String getPassword() {
145 return passwd;
146 }
147
148 public boolean promptYesNo(String str) {
149 return true;
150 }
151
152 String passwd;
153 JTextField passwordField = (JTextField) new JPasswordField(
154 20);
155
156 public String getPassphrase() {
157 return null;
158 }
159
160 public boolean promptPassphrase(
161 String message) {
162 return true;
163 }
164
165 public boolean promptPassword(
166 String message) {
167 Object[] ob = {passwordField};
168 int result =
169 JOptionPane.showConfirmDialog(
170 null,
171 ob,
172 message,
173 JOptionPane.OK_CANCEL_OPTION);
174 if (result == JOptionPane.OK_OPTION) {
175 passwd = passwordField.getText();
176 return true;
177 } else {
178 return false;
179 }
180 }
181
182 public void showMessage(String message) {
183 JOptionPane.showMessageDialog(null,
184 message);
185 }
186 }
187
188 }
189