我正在尝试使用 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: No signature of method: java.lang.Byte.minus()
。请为此问题提出一些解决方案。