菜鸟日记之java运行bat文件

java 代码
package startbat;   
import java.io.IOException;   
import java.io.InputStream;   
  
public class RunBat {   
    public void RunBatFile(){   
        String command = "D:\\test.bat";   
        //String command = "D:\\test.bat";   
        try {   
            Process child = Runtime.getRuntime().exec(command);   
            InputStream in = child.getInputStream();   
            int c;   
            while ((c = in.read()) != -1) {   
                System.out.print(c);   
            }   
            in.close();   
            try {   
                child.waitFor();   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
            System.out.println("done");   
        } catch (IOException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
    }   
}   
java 代码
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)   
  2.             throws ServletException, IOException {   
  3.         try {   
  4.             String ot = request.getParameter("operType") == null ? "" : request.getParameter("operType");   
  5.             PrintWriter out = response.getWriter();   
  6.             //if (ot == "start"){   
  7.                 RunBat rb = new RunBat();   
  8.                 rb.RunBatFile();   
  9.                 request.getRequestDispatcher("/success.jsp").forward(request,   
  10.                         response);   
  11.             //}   
  12.             out.flush();   
  13.             out.close();   
  14.         } catch (Exception e) {   
  15.             e.printStackTrace();   
  16.         }   
  17.     }  

你可能感兴趣的:(java,C++,c,jsp,C#)