Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統計

          留言簿(23)

          積分與排名

          優秀學習網站

          友情連接

          閱讀排行榜

          評論排行榜

          Java 調用 Shell 命令

           近日項目中有這樣一個需求:系統中的外幣資金調度完成以后,要將調度信息生成一個Txt文件,然后將這個Txt文件發送到另外一個系統(Kondor)中。生成文件自然使用OutputStreamWirter了,發送文件有兩種方式,一種是用寫個一個類似于FTP功能的程序,另外一種就是使用Java來調用Shell,在Shell中完成文件的發送操作。我們選擇后一種,即當完成外幣資金的調度工作后,用Java的OutputStreamWriter來生成一個Txt文件,然后用Java來調用Shell腳本,在Shell腳本中完成FTP文件到Kondor系統的工作。
              以下為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執行狀況的日志文件的位置(絕對路徑)
          private static final String executeShellLogFile = basePath + "executeShell.log";

          //發送文件到Kondor系統的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("準備執行Shell命令 ").append(shellCommand).append(" \r\n");

          Process pid = null;
          String[] cmd = {"/bin/sh", "-c", shellCommand};
          //執行Shell命令
          pid = Runtime.getRuntime().exec(cmd);
          if (pid != null) {
          stringBuffer.append("進程號:").append(pid.toString()).append("\r\n");
          //bufferedReader用于讀取Shell的輸出內容 bufferedReader = new BufferedReader(new InputStreamReader(pid.getInputStream()), 1024);
          pid.waitFor();
          } else {
          stringBuffer.append("沒有pid\r\n");
          }
          stringBuffer.append(dateFormat.format(new Date())).append("Shell命令執行完畢\r\n執行結果為:\r\n");
          String line = null;
          //讀取Shell的輸出內容,并添加到stringBuffer中
          while (bufferedReader != null &
          &
          (line = bufferedReader.readLine()) != null) {
          stringBuffer.append(line).append("\r\n");
          }
          } catch (Exception ioe) {
          stringBuffer.append("執行Shell命令時發生異常:\r\n").append(ioe.getMessage()).append("\r\n");
          } finally {
          if (bufferedReader != null) {
          OutputStreamWriter outputStreamWriter = null;
          try {
          bufferedReader.close();
          //將Shell的執行情況輸出到日志文件中
          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系統的IP地址,會將生成的文件發送到這個地址
          kondor_ip=192.168.1.200

          #FTP用戶名
          ftp_username=kondor

          #FTP密碼
          ftp_password=kondor

          #要發送的文件的絕對路徑
          filePath=""

          #要發送的文件的文件名
          fileName=""

          #如果Shell命令帶有參數,則將第一個參數賦給filePath,將第二個參數賦給fileName
          if [ $# -ge "1" ]
          then
          filePath=$1
          else
          echo "沒有文件路徑"
          echo "沒有文件路徑\n" >
          >
          $logFile
          return
          fi

          if [ $# -ge "2" ]
          then
          fileName=$2
          else
          echo "沒有文件名"
          echo "沒有文件名\n" >
          >
          $logFile
          return
          fi

          echo "要發送的文件是 ${filePath}/${fileName}"

          cd ${filePath}
          ls $fileName
          if (test $? -eq 0)
          then
          echo "準備發送文件:${filePath}/${fileName}"
          else
          echo "文件 ${filePath}/${fileName} 不存在"
          echo "文件 ${filePath}/${fileName} 不存在\n" >
          >
          $logFile
          return
          fi

          ftp -n ${kondor_ip} <
          <
          _end
          user ${ftp_username} ${ftp_password}
          asc
          prompt
          put $fileName
          bye
          _end

          echo "`date +%Y-%m-%d' '%H:%M:%S` 發送了文件 ${filePath}/${fileName}"
          echo "`date +%Y-%m-%d' '%H:%M:%S` 發送了文件 ${filePath}/${fileName}\n" >
          >
          $logFile

           
           
           
           
          調用方法為:

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

          posted on 2009-09-23 10:16 都市淘沙者 閱讀(9148) 評論(0)  編輯  收藏 所屬分類: Linux/OpenVPN/Python

          主站蜘蛛池模板: 屏东市| 禹州市| 光泽县| 阳信县| 平利县| 宁阳县| 桐庐县| 贵溪市| 云林县| 清流县| 遵义县| 蒙自县| 调兵山市| 资阳市| 开阳县| 贵德县| 襄垣县| 永康市| 涞水县| 新干县| 瑞昌市| 灌南县| 北京市| 当雄县| 确山县| 南京市| 秀山| 孟津县| 交口县| 望谟县| 通州区| 禹州市| 资阳市| 榆中县| 凌海市| 河南省| 望城县| 新丰县| 比如县| 大竹县| 儋州市|