package com.auto.db.excel.export.tools;
import java.io.*;
import org.apache.log4j.Logger;
class StreamGobbler extends Thread {
InputStream is;
String type;
boolean _run = true;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void stopThread(boolean run) {
this._run = !run;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null){
System.out.println(type + "> " + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public class GoodWindowsExec {
private Logger logger = Logger.getLogger(GoodWindowsExec.class);
private String imp_bat;
public GoodWindowsExec(){
this.imp_bat = GoodWindowsExec.class.getResource("export.bat").getPath().substring(1);
}
public void exec() throws Exception{
new BatUtils().creatBat();
String[] cmd = new String[]{"cmd.exe ","/C ",imp_bat};
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1] + " "+ cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(
proc.getErrorStream(), "ERROR ");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(
proc.getInputStream(), "OUTPUT ");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
//
int exitVal = proc.waitFor();
if(exitVal==1){
proc.destroy();
logger.info("-->>成功导出");
}
System.out.println("ExitValue: " + exitVal);
}
public static void main(String[] args) throws Exception{
new GoodWindowsExec().exec();
}
}
==================
BatUtils.java创建一个临时bat文件供上面的方法调用
new BatUtils().creatBat(); ===============
package com.auto.db.excel.export.tools; import java.io.FileWriter; import java.io.IOException; import java.util.Map; import org.apache.log4j.Logger; import com.auto.db.util.ClassesUtil; import com.auto.db.util.DateUtils; import com.auto.db.util.FileUtil; public class BatUtils { private Logger logger = Logger.getLogger(BatUtils.class); /** * 该方法根据params参数负责生成bat文件 * @param params [0]=监听到的SQL脚本 */ public void creatBat() { FileWriter fw = null; try { Map map = ClassesUtil.readFile("db.properties"); FileUtil.createDirs(map.get("export_path"), true); String command = "@echo off\r\n"+ "echo 正在启动司法部“社区矫正”表导出任务\r\n"+ "exp "+map.get("datacenter_uname")+"/"+map.get("datacenter_pwd")+"@"+map.get("datacenter_host") +" tables=("+ map.get("export_tables")+") file="+map.get("export_path")+"\\sqjz_"+DateUtils.date2String("yyyyMMdd")+".dmp\r\n"+ "echo 数据表导出完毕\r\n"; String bat = BatUtils.class.getResource("").getPath().substring(1)+"export.bat"; logger.info("---->>export_cmd\r\n"+command); logger.info("---->>export_bat\r\n"+bat); fw = new FileWriter(bat); fw.write(command); } catch (IOException e) { e.printStackTrace(); System.exit(0); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new BatUtils().creatBat(); } }