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

  • 相关阅读:
    jquery easy ui 学习 (8)basic treegrid
    jquery easy ui 学习 (7) TreeGrid Actions
    jquery easy ui 学习 (6) basic validatebox
    jquery easy ui 学习 (5) windowlayout
    jquery easy ui 学习 (4) window 打开之后 限制操纵后面元素属性
    提示“应用程序无法启动,因为应用程序的并行配置不正确”不能加载 System.Data.SQLite.dll
    visual studio 添加虚线的快捷键
    VS2010打开项目时,出现“已经在解决方案中打开了具有该名称的项目”问题的解决方案
    visual studio 编译时 出现 Files 的值 乱码
    微信 连接被意外关闭
  • 原文地址:https://www.cnblogs.com/nidongde/p/5364041.html
Copyright © 2011-2022 走看看