来自远程主机的SCP使用常规

时间:2013-06-07 12:35:46

标签: groovy scp

我正在使用JSch从远程主机尝试SCP。我已成功在java STS IDE中复制文件格式远程主机。但是,当我尝试在使用SOAP UI执行的groovy脚本中运行它时,我收到以下错误。

Fri Jun 07 16:53:02 IST 2013:INFO:Exception : groovy.lang.MissingMethodException: No signature of method: java.lang.Byte.minus() is applicable for argument types: (java.lang.String) values: [0]
Possible solutions: minus(java.lang.Number), minus(java.lang.Character), plus(java.lang.String), plus(java.lang.Character), plus(java.lang.Number), times(groovy.lang.Closure)

我的代码是:

import com.jcraft.jsch.*;
import java.io.*;
import java.lang.*;


    FileOutputStream fos=null;
    try{

      String user="soapui";
      String host="192.168.1.1";
      String rfile="//bin//output.txt";
      String lfile="E://temp1.txt";

      String prefix=null;
      if(new File(lfile).isDirectory()){
        prefix=lfile+File.separator;
      }

      JSch jsch=new JSch();
      Session session=jsch.getSession(user, host, 22);

      // username and password will be given via UserInfo interface.
      UserInfo ui=new MyUserInfo();
      session.setUserInfo(ui);
      session.connect();

      // exec 'scp -f rfile' remotely
      String command="scp -f "+rfile;
      Channel channel=session.openChannel("exec");
      ((ChannelExec)channel).setCommand(command);

      // get I/O streams for remote scp
      OutputStream out=channel.getOutputStream();
      InputStream ins=channel.getInputStream();

      channel.connect();

      byte[] buf=new byte[10240];

      // send '\0'
      buf[0]=0; out.write(buf, 0, 1); out.flush();

      while(true){
    int c=checkAck(ins);
        if(c!='C'){
      break;
    }

        // read '0644 '
        ins.read(buf, 0, 5);

        long filesize=0L;
        while(true){
          if(ins.read(buf, 0, 1)<0){
            // error
            break; 
          }
          if(buf[0]==' ')break;
          //char c1='0';
          //long tmp=Long.valueOf(buf[0]-'0');
          **filesize=filesize*10L+(long)(buf[0]-'0');**
        }

        String file=null;
        for(int i=0;;i++){
          ins.read(buf, i, 1);
          if(buf[i]==(byte)0x0a){
            file=new String(buf, 0, i);
            break;
      }
        }

    log.info("filesize="+filesize+", file="+file);

        // send '\0'
        buf[0]=0; out.write(buf, 0, 1); out.flush();

        // read a content of lfile
        fos=new FileOutputStream(prefix==null ? lfile : prefix+file);
        int foo;
        while(true){
          if(buf.length<filesize) foo=buf.length;
      else foo=(int)filesize;
          foo=ins.read(buf, 0, foo);
          if(foo<0){
            // error 
            break;
          }
          fos.write(buf, 0, foo);
          filesize-=foo;
          if(filesize==0L) break;
        }
        fos.close();
        fos=null;
        log.info("Closing Stream");

    if(checkAck(ins)!=0){
      System.exit(0);
    }

        // send '\0'
        buf[0]=0; out.write(buf, 0, 1); out.flush();
      }

      session.disconnect();

      System.exit(0);
    }
    catch(Exception e){
      log.info("Exception : " + e);
      try{if(fos!=null)fos.close();}catch(Exception ee){}
    }


  static int checkAck(InputStream ins) throws IOException{
    int b=ins.read();
    // b may be 0 for success,
    //          1 for error,
    //          2 for fatal error,
    //          -1
    if(b==0) return b;
    if(b==-1) return b;

    if(b==1 || b==2){
      StringBuffer sb=new StringBuffer();
      int c;
     while(c!='\n') {
    c=ins.read();
    sb.append((char)c);
      }

      if(b==1){ // error
    System.out.print(sb.toString());
      }
      if(b==2){ // fatal error
    System.out.print(sb.toString());
      }
    }
    return b;
  }

  public  class MyUserInfo implements UserInfo{
    public String getPassword(){ return passwd;}
    public boolean promptYesNo(String str){return true;} 
    String passwd="123";
    public String getPassphrase(){return null;}
    public boolean promptPassphrase(String message){return true;}
    public boolean promptPassword(String message){return true;}
    public void showMessage(String s){};
    }

我调试的是代码的第filesize=filesize*10L+(long)(buf[0]-'0');行,groovy失败并生成异常groovy.lang.MissingMethodException:没有方法签名:java.lang.Byte.minus()。请为此问题提出一些解决方案。

1 个答案:

答案 0 :(得分:1)

在groovy中,'0'是一个String常量,而不是char或byte。您不能从字节中减去字符串。将其转换为char或使用第一个字节:

byte b = 51 // ASCII '3'
assert b - ('0' as char) == 3
assert b - '0'.bytes[0] == 3

从字节获取数字的更好方法是使用Character.digit()。例如:

filesize = filesize * 10L + Character.digit(buf[0], 10)
相关问题