zoukankan      html  css  js  c++  java
  • JSP/java 执行创建批处理文件,并执行批处理事务。

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

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

  • 相关阅读:
    mysql drop table & myisam
    python 发送 html email
    python mysqldb 查询返回字典结构
    shell 脚本 连接数据库
    python 中使用map 构建sql查询语句
    C#启动一个外部程序(1)WinExec
    知道在那里划这一条线吗[zt]
    C#启动一个外部程序(2)ShellExecute
    把FlashCom 帮助安装到Flash 8 中文版
    C#读写ini文件
  • 原文地址:https://www.cnblogs.com/nidongde/p/5364041.html
Copyright © 2011-2022 走看看