【问题标题】:How to Execute Putty commands in java to move files between folders(mv command) [duplicate]如何在java中执行Putty命令以在文件夹之间移动文件(mv命令)[重复]
【发布时间】:2015-02-13 07:37:30
【问题描述】:

我写了一个java代码,可以使用Jsch连接,可以在本地机器和服务器之间上传和下载文件。

但我不确定如何执行诸如“mv”之类的命令,以便使用 java.util.server 在文件夹之间移动文件。 要列出文件 ls 命令将在 putty 应用程序中使用,可以使用 Jsch 还是必须使用任何其他库?

JSch jsch = null;
Session session = null;
Channel channel = null;
ChannelSftp c = null;
try {
    jsch = new JSch();
    System.out.println("Setting Up SFTP Connection...");
    session = jsch.getSession(username, host, 22);
    session.setPassword(pass);
    System.out.println("SFTP Configration Complete..!");  
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    System.out.println("Attempting to Connect..!"); 
    session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");
    session.connect();
    System.out.println("Session Connected."); 
    channel = session.openChannel("sftp");
    channel.connect();
    System.out.println("Channel Connected."); 
    c = (ChannelSftp) channel;
    System.out.println("Connection Established\n");
} catch (Exception e) { 
    e.printStackTrace();    
}

try {
    String tempFolderInLocal = "C:/Users/591705/Desktop/Test";
    String Destiantion = "/hta1/home/pinDap75a/DestinationDemo";
    String ServerTempDestination="/hta1/home/pinDap75a/TempDestinationDemo";
    String DestiantionT = "/hta1/home/pinDap75a/DestinationDemo/Demo.txt";
    String Source = "C:/Users/591705/Documents/Demo.txt";  
    System.out.println("Starting Upload..");
    c.put(Source,Destiantion);
    System.out.println("**Upload Finished**");  
    System.out.println("Starting Downlaod...");
    c.get(DestiantionT, tempFolderInLocal);

    System.out.println("File Transfer Complete! \n");
} 
catch (Exception e) {   e.printStackTrace();    }

c.disconnect();
session.disconnect();

要在 ServerDestinationTemp 和 DestinationT 之间移动文件,我可以实现它

【问题讨论】:

    标签: java ssh putty jsch


    【解决方案1】:

    使用Shell 频道

    shchannel = session.openChannel("shell");
    

    【讨论】:

    • 但是如何执行命令?
    • 而不是 shchannel = session.openChannel("sftp");我把它作为 shchannel = session.openChannel("shell");现在..
    • 我在以 session.openChannel("shell"); 运行代码时遇到此错误; java.lang.ClassCastException: com.jcraft.jsch.ChannelShell 无法转换为 com.jcraft.jsch.ChannelSftp
    • @RageshKr 好吧,你为什么要先投射它? “shell”通道不能用作“sftp”通道。如果两者都需要,则需要打开两个通道(您可以通过一个 SSH 连接拥有多个通道)。
    • 我基本上需要传输文件并执行 MV、ls 等命令。传输文件部分我已经完成并且工作正常。但是关于执行命令我无法:(你能帮我一个简单的示例代码吗?
    【解决方案2】:

    是的,您可以使用以下代码使用 java 执行 shell 命令,而无需与 shell 终端交互。

     Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        String retStr = "";
        InputStreamReader insReader = null;
        char[] tmpBuffer = new char[1024];
        int nRet = 0;
        String cmd = "mv abc.txt /home/abct.txt";
        try {
            proc = runtime.exec(cmd);
            insReader = new InputStreamReader(proc.getInputStream());
    
            while ((nRet = insReader.read(tmpBuffer, 0, 1024)) != -1) {
                retStr += new String(tmpBuffer, 0, nRet);
            }
    
            insReader.close();
            System.out.println(retStr);
        } catch (Exception e) {
            System.out.println("Bad");
        } finally {
            System.out.println("Done");
            }
    

    【讨论】:

    • 我读到的问题是文件在远程系统上。
    • 请告知用户已经登录远程服务器
    • 是的,使用 jsch,如果他使用的是 shell 通道,那么他可以将命令写入通道输出流。
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2011-01-15
    • 2017-06-16
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多