]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java
1 package org
.argeo
.slc
.jsch
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
7 import org
.apache
.commons
.io
.IOUtils
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.SlcException
;
11 import org
.springframework
.core
.io
.Resource
;
13 import com
.jcraft
.jsch
.Channel
;
14 import com
.jcraft
.jsch
.ChannelExec
;
15 import com
.jcraft
.jsch
.Session
;
17 public class ScpTo
extends AbstractJschTask
{
18 private final static Log log
= LogFactory
.getLog(ScpTo
.class);
20 private Resource localFile
;
21 private String remoteFile
;
24 InputStream in
= null;
25 OutputStream channelOut
;
26 InputStream channelIn
;
28 Session session
= openSession();
31 // exec 'scp -t rfile' remotely
32 String command
= "scp -p -t " + remoteFile
;
33 Channel channel
= session
.openChannel("exec");
34 ((ChannelExec
) channel
).setCommand(command
);
36 // get I/O streams for remote scp
37 channelOut
= channel
.getOutputStream();
38 channelIn
= channel
.getInputStream();
43 // send "C0644 filesize filename", where filename should not include
45 File lFile
= localFile
.getFile();
46 long filesize
= lFile
.length();
47 command
= "C0644 " + filesize
+ " ";
48 int index
= lFile
.getPath().lastIndexOf('/');
50 command
+= lFile
.getPath().substring(index
+ 1);
52 command
+= lFile
.getPath();
56 channelOut
.write(command
.getBytes());
60 if (log
.isDebugEnabled())
61 log
.debug("Start copy of " + localFile
+ " to " + remoteFile
62 + " on " + getSshTarget() + "...");
64 // send a content of lfile
65 in
= localFile
.getInputStream();
66 byte[] buf
= new byte[1024];
69 int len
= in
.read(buf
, 0, buf
.length
);
72 channelOut
.write(buf
, 0, len
); // out.flush();
73 if ((cycleCount
% 1024) == 0)// each 1 MB
74 System
.out
.print('#');
79 channelOut
.write(buf
, 0, 1);
83 if (log
.isDebugEnabled())
84 log
.debug("Finished copy of " + localFile
+ " to " + remoteFile
85 + " on " + getSshTarget() + "...");
87 IOUtils
.closeQuietly(channelOut
);
92 } catch (Exception e
) {
93 throw new SlcException("Cannot copy " + localFile
+ " to "
96 IOUtils
.closeQuietly(in
);
100 public void setLocalFile(Resource localFile
) {
101 this.localFile
= localFile
;
104 public void setRemoteFile(String remoteFile
) {
105 this.remoteFile
= remoteFile
;