java生成批处理文件_JSP/java 执行创建批处理文件,并执行批处理事务。

protected voiddoGet(HttpServletRequest req, HttpServletResponse resp) {

InputStreamin = null;

InputStreamReader ir= null;

List list= newArrayList();

String line= "";

String msg= "";

HttpSession session=req.getSession();

String username= req.getParameter("username");

String pwd= req.getParameter("pwd");//获取网站的绝对路径

String strDirPath = this.getServletConfig().getServletContext()

.getRealPath("/");//随机数

Random rd = newRandom();int rdNum = rd.nextInt(1000);//文件名

String fileName = System.currentTimeMillis() + "_" + rdNum + ".bat";//文件所在全路径

String filePath = strDirPath +fileName;//批处理文件的内容 username指用户登录名,pwd指密码

String content = "Dsquery user -samid" +username+ "| dsmod user -pwd" +pwd;

writeFile(filePath, content);try{

Process p=Runtime.getRuntime().exec(filePath);in =p.getInputStream();

ir= new InputStreamReader(in);

BufferedReader br= newBufferedReader(ir);while ((line = br.readLine()) != null) {

list.add(line);

}if (p.waitFor() != 0) {

msg= "0-批处理执行失败,请检查批处理文件是否正确!";

}else{

msg= "1-批处理执行成功!";

}

p.destroy();

Iterator it=list.iterator();

line= "";while(it.hasNext()) {

line= line +(String) it.next();

}

it= null;

br.close();

br= null;

ir.close();

ir= null;in.close();in = null;

p.destroy();

p= null;

}catch(IOException e) {

msg= "2-io异常,请检查指定的批处理文件是否存在!";

}catch(InterruptedException e) {

msg= "3-中断异常!";

}catch(IllegalArgumentException e) {

msg= "4-命令为空!";

}catch(Exception e) {

msg= "5-未知异常!";

}

String successUrl= "ok.jsp?msg=" + msg + "&line=" +line;try{

resp.sendRedirect(successUrl);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}/** 将内容写入文件*/

private voidwriteFile(String filePath, String content) {

File file= newFile(filePath);if (!file.exists()) {try{

file.createNewFile();

}catch(Exception e) {//TODO Auto-generated catch block//e.printStackTrace();

}

}try{

OutputStreamWriter write= newOutputStreamWriter(new FileOutputStream(file), "Utf-8");

BufferedWriter writer= newBufferedWriter(write);

writer.write(content);

writer.close();

write.close();

}catch (Exception e) {

J2ee中执行创建批处理文件,并执行批处理事务。

protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

InputStream in = null;

InputStreamReader ir = null;

List list = new ArrayList();

String line = "";

String msg = "";

HttpSession session = req.getSession();

String username = req.getParameter("username");

String pwd = req.getParameter("pwd");

// 获取网站的绝对路径

String strDirPath = this.getServletConfig().getServletContext()

.getRealPath("/");

// 随机数

Random rd = new Random();

int rdNum = rd.nextInt(1000);

// 文件名

String fileName = System.currentTimeMillis() + "_" + rdNum + ".bat";

// 文件所在全路径

String filePath = strDirPath + fileName;

// 批处理文件的内容 username指用户登录名,pwd指密码

String content = " Dsquery user -samid " + username

+ " | dsmod user -pwd " + pwd;

writeFile(filePath, content);

try {

Process p = Runtime.getRuntime().exec(filePath);

in = p.getInputStream();

ir = new InputStreamReader(in);

BufferedReader br = new BufferedReader(ir);

while ((line = br.readLine()) != null) {

list.add(line);

}

if (p.waitFor() != 0) {

msg = "0-批处理执行失败,请检查批处理文件是否正确!";

} else {

msg = "1-批处理执行成功!";

}

p.destroy();

Iterator it = list.iterator();

line = "";

while (it.hasNext()) {

line = line + (String) it.next();

}

it = null;

br.close();

br = null;

ir.close();

ir = null;

in.close();

in = null;

p.destroy();

p = null;

} catch (IOException e) {

msg = "2-io异常,请检查指定的批处理文件是否存在!";

} catch (InterruptedException e) {

msg = "3-中断异常!";

} catch (IllegalArgumentException e) {

msg = "4-命令为空!";

} catch (Exception e) {

msg = "5-未知异常!";

}

String successUrl = "ok.jsp?msg=" + msg + "&line=" + line;

try {

resp.sendRedirect(successUrl);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* 将内容写入文件

*/

private void writeFile(String filePath, String content) {

File file = new File(filePath);

if (!file.exists()) {

try {

file.createNewFile();

} catch (Exception e) {

// TODO Auto-generated catch block

// e.printStackTrace();

}

}

try {

OutputStreamWriter write = new OutputStreamWriter(

new FileOutputStream(file), "Utf-8");

BufferedWriter writer = new BufferedWriter(write);

writer.write(content);

writer.close();

write.close();

} catch (Exception e) {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值