package com.asiainfo.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Logger;
public class PerlUtil {
public PerlUtil() {
}
/**
* @Description: TODO
* @param String
* [] cmd = { "perl",
* "E:\\code\\cwBiBBM\\target\\classes\\perl_bin\\YsbgDataInit.pl"
* ,"192.168.1.254","3306","cw_bbm_test", "root","root",
* "8209929290",
* "19999","E:\\code\\cwBiBBM\\target\\classes\\perl_bin" };
* @return
* @return String
* @throws
* @author zhangshu
* @date 2015-8-13
*
*/
public static synchronized String runPerl(String[] cmd) throws Exception {
StringBuffer resultStringBuffer = new StringBuffer();
String lineToRead = "";
int exitValue = 0;
Process proc = Runtime.getRuntime().exec(cmd);
InputStream inputStream = proc.getInputStream();
BufferedReader bufferedRreader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
// save first line
if ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append(lineToRead);
}
// save next lines
while ((lineToRead = bufferedRreader.readLine()) != null) {
resultStringBuffer.append("\r\n");
resultStringBuffer.append(lineToRead);
}
// Always reading STDOUT first, then STDERR, exitValue last
proc.waitFor(); // wait for reading STDOUT and STDERR over
exitValue = proc.exitValue();
String error = InputStreamTOString(proc.getErrorStream());
System.out.println(error);
return String.valueOf(exitValue);
}
private static String InputStreamTOString(InputStream in) {
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int count = -1;
while ((count = in.read(data, 0, 1024)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 没有错误,exitValue=0
*
* 方法一 Process proc = Runtime.getRuntime().exec(
* "perl C:\\Users\\chenjw\\workspace\\ysbg\\YsbgAppContent_test.pl 8209929290 19999"
* );
*
* 方法二 带参数 String[] cmd = { "perl",
* "C:\\Users\\chenjw\\workspace\\ysbg\\tttt.pl", "8209929290","19999"};
* Process proc = Runtime.getRuntime().exec(cmd);
*
* 标准调用参数样式 0.perl 1.perl 文件路径 2.主机名/ip 3.mysql端口 4.数据库实例名 5.数据库用户名
* 6.数据库用户密码 7.方案ID 8.方法所属组织ID
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String[] cmd1 = { "perl", "D:\\CodeWorkSpace\\LunaWorkSpace\\cwMCA\\src\\main\\resources\\perl_bin\\dwcc_share_control.pl", "localhost", "3306", "cw_mca_deploy", "root", "root", "201508", "10900", "D:\\CodeWorkSpace\\LunaWorkSpace\\cwMCA\\src\\main\\resources\\perl_bin" };
String runPerl1 = PerlUtil.runPerl(cmd1);
System.out.println(runPerl1);
}
}
最近下载更多
pj_fhzy LV3
2024年10月13日
wangjinglin LV1
2019年12月9日
liuyishi LV6
2019年4月6日
blazing LV2
2019年2月19日
a1351328329 LV1
2018年6月25日
jic499 LV27
2018年4月13日
人工智能4708 LV11
2018年3月13日
sdxrh2005 LV1
2018年1月20日
shenkang10086 LV6
2017年5月11日
wyj11www LV1
2016年12月28日

最近浏览