~ubuntu-branches/ubuntu/trusty/jsch/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/ScpTo.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-06 10:50:04 UTC
  • mfrom: (4.2.1 experimental) (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140106105004-9al5p968hmaaj78t
Tags: 0.1.50-1ubuntu1
* Resync with Debian unstable.
* Drop dependency on libjzlib-java for continued main inclusion. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
 
2
/**
 
3
 * This program will demonstrate the file transfer from local to remote.
 
4
 *   $ CLASSPATH=.:../build javac ScpTo.java
 
5
 *   $ CLASSPATH=.:../build java ScpTo file1 user@remotehost:file2
 
6
 * You will be asked passwd. 
 
7
 * If everything works fine, a local file 'file1' will copied to
 
8
 * 'file2' on 'remotehost'.
 
9
 *
 
10
 */
2
11
import com.jcraft.jsch.*;
3
12
import java.awt.*;
4
13
import javax.swing.*;
28
37
      session.setUserInfo(ui);
29
38
      session.connect();
30
39
 
 
40
      boolean ptimestamp = true;
31
41
 
32
42
      // exec 'scp -t rfile' remotely
33
 
      String command="scp -p -t "+rfile;
 
43
      String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
34
44
      Channel channel=session.openChannel("exec");
35
45
      ((ChannelExec)channel).setCommand(command);
36
46
 
44
54
        System.exit(0);
45
55
      }
46
56
 
 
57
      File _lfile = new File(lfile);
 
58
 
 
59
      if(ptimestamp){
 
60
        command="T "+(_lfile.lastModified()/1000)+" 0";
 
61
        // The access time should be sent here,
 
62
        // but it is not accessible with JavaAPI ;-<
 
63
        command+=(" "+(_lfile.lastModified()/1000)+" 0\n"); 
 
64
        out.write(command.getBytes()); out.flush();
 
65
        if(checkAck(in)!=0){
 
66
          System.exit(0);
 
67
        }
 
68
      }
 
69
 
47
70
      // send "C0644 filesize filename", where filename should not include '/'
48
 
      long filesize=(new File(lfile)).length();
 
71
      long filesize=_lfile.length();
49
72
      command="C0644 "+filesize+" ";
50
73
      if(lfile.lastIndexOf('/')>0){
51
74
        command+=lfile.substring(lfile.lastIndexOf('/')+1);