轉自 https://blog.csdn.net/qq_27948659/article/details/80895860
public R downLoadFiles(){ logger.info("執行開始下載命令"); String cmd = //"hadoop fs -ls /files/ftp/zhihui003/oc_data"; "hadoop fs -get /files/ftp/zhihui003/* "+fileDir+""; //String cmd = "ls "+fileDir; logger.info("downLoadFiles的cmd命令爲 :"+cmd); // 定時掃描新文件 掃描後處理新文件 分包 String[] cmds = {"/bin/sh", "-c", cmd }; Process process=null; try { process=Runtime.getRuntime().exec(cmds); //獲取進程的標準輸入流 final InputStream is1 = process.getInputStream(); //獲取進城的錯誤流 final InputStream is2 = process.getErrorStream(); //啓動兩個線程,一個線程負責讀標準輸出流,另一個負責讀標準錯誤流 new Thread() { public void run() { BufferedReader br1 = new BufferedReader(new InputStreamReader(is1)); try { String line1 = null; while ((line1 = br1.readLine()) != null) { if (line1 != null){ logger.info(line1); } } } catch (IOException e) { e.printStackTrace(); } finally{ try { is1.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { BufferedReader br2 = new BufferedReader(new InputStreamReader(is2)); try { String line2 = null ; while ((line2 = br2.readLine()) != null ) { if (line2 != null){ logger.info(line2); } } } catch (IOException e) { e.printStackTrace(); } finally{ try { is2.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); //可能導致進程阻塞,甚至死鎖 int ret = process.waitFor(); System.out.println("return value:"+ret); System.out.println(process.exitValue()); logger.info("event:{}", "RunExeForWindows",process.exitValue()); byte[] bytes = new byte[process.getInputStream().available()]; process.getInputStream().read(bytes); System.out.println(new String(bytes)); logger.info("event:{}", "RunExeForWindows",new String(bytes)); }catch (Exception ex){ ex.printStackTrace(); try{ process.getErrorStream().close(); process.getInputStream().close(); process.getOutputStream().close(); } catch(Exception ee){ ee.printStackTrace(); } } return R.ok("下載成功"); }