JavaAPI无法访问它-< 命令+=(“”+(_lfile.lastModified()/1000)+“0\n”); out.write(command.getBytes());out.flush(); 如果(检查确认(in)!=0){ 系统出口(0);

JavaAPI无法访问它-< 命令+=(“”+(_lfile.lastModified()/1000)+“0\n”); out.write(command.getBytes());out.flush(); 如果(检查确认(in)!=0){ 系统出口(0);,java,unix,Java,Unix,JavaAPI无法访问它-< 命令+=(“”+(_lfile.lastModified()/1000)+“0\n”); out.write(command.getBytes());out.flush(); 如果(检查确认(in)!=0){ 系统出口(0); } } //发送“C0644文件大小文件名”,其中文件名不应包括“/” 长文件大小=_lfile.length(); command=“C0644”+文件大小+”; 如果(lfile.lastIndexOf('/')>0){ command+

JavaAPI无法访问它-< 命令+=(“”+(_lfile.lastModified()/1000)+“0\n”); out.write(command.getBytes());out.flush(); 如果(检查确认(in)!=0){ 系统出口(0); } } //发送“C0644文件大小文件名”,其中文件名不应包括“/” 长文件大小=_lfile.length(); command=“C0644”+文件大小+”; 如果(lfile.lastIndexOf('/')>0){ command+=lfile.substring(lfile.lastIndexOf('/')+1); } 否则{ command+=lfile; } 命令+=“\n”; out.write(command.getBytes());out.flush(); 如果(检查确认(in)!=0){ 系统出口(0); } //发送lfile的内容 fis=新文件输入流(lfile); 字节[]buf=新字节[1024]; while(true){ int len=fis.read(buf,0,buf.length);
如果(Len您的设置的物理体系结构是什么?Windows机器和Unix服务器?Unix服务器是否公开任何类型的接口来执行这种复制操作?例如任何形式的通信通道,例如
tcp
http
ssh
,等等。我能够将JSch库连接到Unix服务器。这给了我很大的帮助n在服务器上执行命令的可选性。但是我通过代码使用vi,那么它的响应格式不适合我的使用。这就是为什么我在本地计算机上创建文件并希望传输到Unix服务器的原因。@Azeem我正在使用ssh连接。然后,您可以简单地使用
scp
命令来复制您的文件。您可以按照answe操作r由@Christopher Maggiulli在下面通过使用
pscp
来实现,或者您可以找到您选择的任何其他工具来
secure copy
到Unix服务器。您的设置的物理体系结构是什么?Windows机器和Unix服务器?Unix服务器是否公开任何类型的接口来实现这种复制?就像任何形式的通信一样离子通道,例如,
tcp
http
ssh
,等等。我可以通过JSch库连接到Unix服务器。这给了我在服务器上执行命令的功能。但我通过代码使用vi,然后它的响应格式不适合我们使用。这就是为什么我在本地计算机和想要传输到Unix服务器。@Azem我正在使用ssh连接。然后,您只需使用
scp
命令复制您的文件。您可以按照下面@Christopher Maggiulli的回答,使用
pscp
执行此操作,或者您可以找到您选择的任何其他工具来
安全复制到Unix服务器。您能解释一下吗解释够了吗?你能再解释一点吗?解释够了吗?您好,感谢您分享代码它工作正常,如果我们想包含RSA密钥以连接到外部系统,您能告诉我们该怎么做:)您好,感谢您分享代码。它工作正常。如果我们要包含用于连接外部系统的RSA密钥,您能告诉我们该怎么做吗:)
Runtime.getRuntime().exec("pscp.exe C:\Users\usr\Downloads>pscp -r -P 22 "directory123" root@192.168.2.1:/tmp");
set PATH=C:\path\to\putty\directory;%PATH%
pscp.exe C:\Users\usr\Downloads>pscp -r -P 22 "directory123" root@192.168.2.1:/tmp
Runtime.getRuntime().exec("....");
  import com.jcraft.jsch.*;
  import java.io.*;

    public class ScpTo{
    public static void main(String[] arg){
      FileInputStream fis=null;
       try{
       String lfile="file.txt";
        String user="username";
        String host="host";
     String rfile="file1.txt";

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

     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no");
     session.setPassword("******");
      session.setConfig(config);
       session.connect();

  boolean ptimestamp = false;
  // exec 'scp -t rfile' remotely
  String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
  Channel channel=session.openChannel("exec");
  ((ChannelExec)channel).setCommand(command);

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

  channel.connect();

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

  File _lfile = new File(lfile);

  if(ptimestamp){
    command="T "+(_lfile.lastModified()/1000)+" 0";
    // The access time should be sent here,
    // but it is not accessible with JavaAPI ;-<
    command+=(" "+(_lfile.lastModified()/1000)+" 0\n"); 
    out.write(command.getBytes()); out.flush();
    if(checkAck(in)!=0){
  System.exit(0);
    }
  }

  // send "C0644 filesize filename", where filename should not include '/'
  long filesize=_lfile.length();
  command="C0644 "+filesize+" ";
  if(lfile.lastIndexOf('/')>0){
    command+=lfile.substring(lfile.lastIndexOf('/')+1);
  }
  else{
    command+=lfile;
  }
  command+="\n";
  out.write(command.getBytes()); out.flush();
  if(checkAck(in)!=0){
System.exit(0);
  }

  // send a content of lfile
  fis=new FileInputStream(lfile);
  byte[] buf=new byte[1024];
  while(true){
    int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
    out.write(buf, 0, len); //out.flush();
  }
  fis.close();
  fis=null;
  // send '\0'
  buf[0]=0; out.write(buf, 0, 1); out.flush();
  if(checkAck(in)!=0){
System.exit(0);
  }
  out.close();
  System.out.print("Transfer done.");

  channel.disconnect();
  session.disconnect();

  System.exit(0);
}
catch(Exception e){
  System.out.println(e);
  try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}

static int checkAck(InputStream in) throws IOException{
int b=in.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;
  do {
c=in.read();
sb.append((char)c);
  }
  while(c!='\n');
  if(b==1){ // error
System.out.print(sb.toString());
  }
  if(b==2){ // fatal error
System.out.print(sb.toString());
  }
}
return b;
 }
}