java在linux本地執(zhí)行shell命令的實現(xiàn)方法
更新時間:2022年02月25日 11:26:49 作者:純潔的小魔鬼
本文主要介紹了java在linux本地執(zhí)行shell命令的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
一.以springboot為例,建立代碼
1.IExecCommandServer:
public interface IExecCommandServer { void execCommand(String cmd); }
2.ExecCommandServerImp:
@Service public class ExecCommandServerImp implements IExecCommandServer { @Override public void execCommand(String cmd){ try{ Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd,null,null); InputStream stderr = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr,"GBK"); BufferedReader br = new BufferedReader(isr); String line=""; while ((line = br.readLine()) != null) { System.out.println(line); } }catch (Exception e){ e.printStackTrace(); } } }
3.ExecCommandController:
@CrossOrigin @RestController @RequestMapping("/linux") public class ExecCommandController { @Autowired private IExecCommandServer execCommandServer; @GetMapping("/exec") public ResultMap execCommand(String cmd) throws Exception { execCommandServer.execCommand(cmd); return Result.success("ok"); } }
二,執(zhí)行示例
http://192.168.142.222:8086/linux/exec?cmd=ls /mnt
日志中輸出:
到此這篇關于java在linux本地執(zhí)行shell命令的實現(xiàn)方法的文章就介紹到這了,更多相關java在linux執(zhí)行shell命令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringCloud turbine監(jiān)控實現(xiàn)過程解析
這篇文章主要介紹了SpringCloud turbine監(jiān)控實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解
這篇文章主要為大家介紹了Seata?AT獲取數(shù)據(jù)表元數(shù)據(jù)源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11