2016-05-13 51 views
0

有人可以帮助我如何使用spring sftp适配器在java代码中执行unix命令。我已经与该jar进行了连接,但不知道如何继续执行unix命令。使用sftp弹簧适配器在java中执行unix命令

package automation; 

import java.io.InputStream; 

import org.springframework.integration.channel.ChannelPurger; 
import org.springframework.integration.file.remote.session.Session; 
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory; 
import org.springframework.integration.sftp.session.SftpSession; 

import com.jcraft.jsch.*; 


public class unixConnect { 

    DefaultSftpSessionFactory dsftp; 
    String input1 = "/****/****/file.dat"; 

    public DefaultSftpSessionFactory getDsftp() { 
     return dsftp; 
    } 

    public void setDsftp(DefaultSftpSessionFactory dsftp) { 
     this.dsftp = dsftp; 
    } 

    private Session sess; 

    public void connect() { 
     try {   
      sess = dsftp.getSession(); 
      String command = "ksh /home/***/exec " + input1; 
      ---- This is the place how to execute the above command. 
      channel.setInputStream(null);   
      channel.setOutputStream(System.out);                 
      ((ChannelExec)channel).setCommand(command);    
      ((ChannelExec)channel).setErrStream(System.out); 
      channel.connect();             
      InputStream input = channel.getInputStream();  

     } catch (Exception e) { 
      System.out.println("Error" + e); 
     } 
    } 
} 

我一直在寻找它找到一个答案,但没有得到一个。 :(

+0

SFTP与在linux中执行命令有什么关系Java已经提供了一种通过运行时库执行linux命令的方法,请检查一下。 –

回答

0

SFTPSSH文件传输协议。你看,部分“文件传输”,没有,执行命令,你可以传输文件,但不能运行任何东西,这就是定义。