国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java調(diào)用Shell命令的方法

2019-11-26 15:04:26
字體:
供稿:網(wǎng)友

本文實例講述了Java調(diào)用Shell命令的方法。分享給大家供大家參考。具體如下:

近日項目中有這樣一個需求:系統(tǒng)中的外幣資金調(diào)度完成以后,要將調(diào)度信息生成一個Txt文件,然后將這個Txt文件發(fā)送到另外一個系統(tǒng)(Kondor)中。生成文件自然使用OutputStreamWirter了,發(fā)送文件有兩種方式,一種是用寫個一個類似于FTP功能的程序,另外一種就是使用Java來調(diào)用Shell,在Shell中完成文件的發(fā)送操作。我們選擇后一種,即當完成外幣資金的調(diào)度工作后,用Java的OutputStreamWriter來生成一個Txt文件,然后用Java來調(diào)用Shell腳本,在Shell腳本中完成FTP文件到Kondor系統(tǒng)的工作。

以下為Java程序JavaShellUtil.java:

import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;public class JavaShellUtil {//基本路徑private static final String basePath = "/tmp/";//記錄Shell執(zhí)行狀況的日志文件的位置(絕對路徑)private static final String executeShellLogFile = basePath + "executeShell.log";//發(fā)送文件到Kondor系統(tǒng)的Shell的文件名(絕對路徑)private static final String sendKondorShellName = basePath + "sendKondorFile.sh";public int executeShell(String shellCommand) throws IOException {int success = 0;StringBuffer stringBuffer = new StringBuffer();BufferedReader bufferedReader = null;//格式化日期時間,記錄日志時使用DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS ");try {stringBuffer.append(dateFormat.format(new Date())).append("準備執(zhí)行Shell命令 ").append(shellCommand).append(" /r/n");Process pid = null;String[] cmd = {"/bin/sh", "-c", shellCommand};//執(zhí)行Shell命令pid = Runtime.getRuntime().exec(cmd);if (pid != null) {stringBuffer.append("進程號:").append(pid.toString()).append("/r/n");//bufferedReader用于讀取Shell的輸出內(nèi)容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);pid.waitFor();} else {stringBuffer.append("沒有pid/r/n");}stringBuffer.append(dateFormat.format(new Date())).append("Shell命令執(zhí)行完畢/r/n執(zhí)行結(jié)果為:/r/n");String line = null;//讀取Shell的輸出內(nèi)容,并添加到stringBuffer中while (bufferedReader != null &&(line = bufferedReader.readLine()) != null) {stringBuffer.append(line).append("/r/n");}} catch (Exception ioe) {stringBuffer.append("執(zhí)行Shell命令時發(fā)生異常:/r/n").append(ioe.getMessage()).append("/r/n");} finally {if (bufferedReader != null) {OutputStreamWriter outputStreamWriter = null;try {bufferedReader.close();//將Shell的執(zhí)行情況輸出到日志文件中OutputStream outputStream = new FileOutputStream(executeShellLogFile);outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");outputStreamWriter.write(stringBuffer.toString());} catch (Exception e) {e.printStackTrace();} finally {outputStreamWriter.close();}}success = 1;}return success;}}

以下是Shell腳本sendKondorFile.sh,該Shell腳本的作用是FTP文件到指定的位置:

#!/bin/sh#日志文件的位置logFile="/opt/fms2_kondor/sendKondorFile.log"#Kondor系統(tǒng)的IP地址,會將生成的文件發(fā)送到這個地址kondor_ip=192.168.1.200#FTP用戶名ftp_username=kondor#FTP密碼ftp_password=kondor#要發(fā)送的文件的絕對路徑filePath=""#要發(fā)送的文件的文件名fileName=""#如果Shell命令帶有參數(shù),則將第一個參數(shù)賦給filePath,將第二個參數(shù)賦給fileNameif [ $# -ge "1" ]thenfilePath=$1elseecho "沒有文件路徑"echo "沒有文件路徑/n" >>$logFilereturnfiif [ $# -ge "2" ]thenfileName=$2elseecho "沒有文件名"echo "沒有文件名/n" >>$logFilereturnfiecho "要發(fā)送的文件是 ${filePath}/${fileName}"cd ${filePath}ls $fileNameif (test $? -eq 0)thenecho "準備發(fā)送文件:${filePath}/${fileName}"elseecho "文件 ${filePath}/${fileName} 不存在"echo "文件 ${filePath}/${fileName} 不存在/n" >>$logFilereturnfiftp -n ${kondor_ip} <<_enduser ${ftp_username} ${ftp_password}ascpromptput $fileNamebye_endecho "`date +%Y-%m-%d' '%H:%M:%S` 發(fā)送了文件 ${filePath}/${fileName}"echo "`date +%Y-%m-%d' '%H:%M:%S` 發(fā)送了文件 ${filePath}/${fileName}/n" >>$logFile

調(diào)用方法為:

JavaShellUtil javaShellUtil = new JavaShellUtil();//參數(shù)為要執(zhí)行的Shell命令,即通過調(diào)用Shell腳本sendKondorFile.sh將/temp目錄下的tmp.pdf文件發(fā)送到192.168.1.200上int success = javaShellUtil.executeShell("sh /tmp/sendKondorFile.sh /temp tmp.pdf");

希望本文所述對大家的java程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐城市| 宜城市| 旺苍县| 五河县| 新郑市| 凤冈县| 孙吴县| 邯郸市| 临沭县| 嵊泗县| 南靖县| 安阳县| 吕梁市| 嘉定区| 蒙阴县| 含山县| 方城县| 光泽县| 卢龙县| 呼图壁县| 扶风县| 张家港市| 太湖县| 轮台县| 寿光市| 商都县| 大田县| 梁河县| 连山| 平塘县| 海丰县| 鞍山市| 达拉特旗| 丰原市| 金乡县| 屏东市| 垦利县| 吉安市| 尼勒克县| 昆山市| 顺平县|