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) {