程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java挪用Shell敕令的辦法

Java挪用Shell敕令的辦法

編輯:關於JAVA

Java挪用Shell敕令的辦法。本站提示廣大學習愛好者:(Java挪用Shell敕令的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java挪用Shell敕令的辦法正文


本文實例講述了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");

願望本文所述對年夜家的java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved