如题:java如何调用服务器的shell脚本,并判断是否执行成功,或者由Java ftp上传至服务器,然后执行它

解决方案 »

  1.   

    必须在服务器端本地执行,远程是无法执行的,如果这样都行,那安全性何在。
    可以考虑在服务器上运行一个服务器端程序,接收客户端发来的指令,服务器端接到指令后根据指令运行shell脚本,并把运行结果反馈回客户端。
      

  2.   

    我写的,可以用,执行shell脚本的方法,返回的是执行结果。shell脚本格式:
    比如要执行ls。
    String[] command = {"/bin/bash" , "-c" , "ls"};    private String getDataByShellCMD(String[] command) throws Exception{
            Process process = null;
            BufferedReader in = null;
            try{
                process = Runtime.getRuntime().exec(command);
                int iWaitFor = process.waitFor();
                if (iWaitFor != 0) {
                    //出错写log
                }
                in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;
                line = in.readLine();
                return line;
            } catch (Exception e) {
                return null;
            } finally {
                if (process != null) {
                    process.destroy();
                    process = null;
                }
                if (in!=null){
                    in.close();
                    in = null;
                }
            }
        }这个方法只是封装的获取一行的返回值。可以自行修改返回多行的。
      

  3.   


    4楼的方法可以,通过Process 启动一个线程来调用设立了脚本。