可以去看看Class Runtime,调用还是比较简单的。
试试就可以搞定了,祝你好运。呵呵

解决方案 »

  1.   

    楼上着为朋友
    好象Runtime 这种方法不稳定呀?是吗?
      

  2.   

    一个列子 :1.[ShellServlet.java]import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;public class ShellServlet extends HttpServlet
    {    public ShellServlet()
        {
        }    private String MainWmlCard(String s)
        {
            StringBuffer stringbuffer = new StringBuffer("");
            stringbuffer.append("<wml>\n");
            stringbuffer.append("<head>\n");
            stringbuffer.append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\"/>\n");
            stringbuffer.append("</head>\n");
            stringbuffer.append("<card id=\"mainshell\" title=\"Shell Card\">\n");
            stringbuffer.append("<do type=\"accept\" label=\"Exec\">\n");
            stringbuffer.append("<go href=\"" + s + "?" + "act" + "=" + "3" + "&amp;" + "fct" + "=" + getId() + "\" method=\"post\">\n");
            stringbuffer.append("<postfield name=\"cmd\" value=\"$(sCmd)\"/>\n");
            stringbuffer.append("</go>\n");
            stringbuffer.append("</do>\n");
            stringbuffer.append("<p align=\"center\"><b>" + (String)cnf.get("title") + "</b></p>\n");
            stringbuffer.append("<p>Shell&gt;<input type=\"text\" name=\"sCmd\" value=\"\" emptyok=\"false\"/></p>\n");
            stringbuffer.append("</card>\n");
            stringbuffer.append("</wml>\n");
            return stringbuffer.toString();
        }    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws ServletException, IOException
        {
            String s = "";
            String s1 = "";
            String s2 = "";
            s = HttpUtils.getRequestURL(httpservletrequest).toString();
            int i;
            if((i = s.indexOf("?")) > 0)
                s = s.substring(0, i);
            s1 = httpservletrequest.getQueryString();
            if(httpservletrequest.getHeader("Accept").indexOf("wap.wml") >= 0)
            {
                wmlClient(s, s1, httpservletrequest, httpservletresponse);
                return;
            }
            httpservletresponse.setContentType("text/html");
            PrintWriter printwriter = httpservletresponse.getWriter();
            if(s1 == null)
            {
                printwriter.println(writeFrame(s, httpservletresponse));
            }
            else
            {
                String s3 = getFromQuery(s1, "act=");
                if(s3.equals("1"))
                    printwriter.println(writeUp());
                else
                if(s3.equals("2"))
                    printwriter.println(writeDown(s, httpservletrequest, httpservletresponse));
                else
                if(s3.equals("3"))
                    printwriter.println(writeCommand(httpservletrequest));
                else
                if(s3.equals("4"))
                    printwriter.println(writeHistory(httpservletrequest));
                else
                    printwriter.println(writeFrame(s, httpservletresponse));
            }
            printwriter.flush();
            printwriter.close();
        }    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws ServletException, IOException
        {
            if(httpservletrequest.getContentLength() > 20480)
            {
                httpservletresponse.setContentType("text/html");
                ServletOutputStream servletoutputstream = httpservletresponse.getOutputStream();
                servletoutputstream.println("<html><head><title>Too big</title></head>");
                servletoutputstream.println("<body><h1>Error - content length &gt;20k not ");
                servletoutputstream.println("</h1></body></html>");
            }
            else
            {
                doGet(httpservletrequest, httpservletresponse);
            }
        }    private String executeCmd(String s, String s1)
        {
            String s2 = "";
            StringBuffer stringbuffer = new StringBuffer(s1 + "shell&gt;" + s);
            try
            {
                Process process = Runtime.getRuntime().exec(s);
                java.io.InputStream inputstream = process.getInputStream();
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream));
                String s3;
                while((s3 = bufferedreader.readLine()) != null) 
                {
                    stringbuffer.append(s1);
                    stringbuffer.append(prepareMsg(s3));
                    stringbuffer.append(NEWLINE);
                }            process.waitFor();
                bufferedreader.close();
            }
            catch(Exception exception)
            {
                stringbuffer.append(s1);
                stringbuffer.append(exception);
            }
            return stringbuffer.toString();
        }    private String getFont(String s)
        {
            String s2 = "<font color=\"" + s + "\"";
            String s1;
            if((s1 = (String)cnf.get("face")) != null)
                s2 = s2 + " face=\"" + s1 + "\"";
            if((s1 = (String)cnf.get("size")) != null)
                s2 = s2 + " size=\"" + s1 + "\"";
            return s2 + ">";
        }    private String getFromQuery(String s, String s1)
        {
            if(s == null)
                return "";
            int i;
            if((i = s.indexOf(s1)) < 0)
                return "";
            String s2 = s.substring(i + s1.length());
            if((i = s2.indexOf("&")) < 0)
                return s2;
            else
                return s2.substring(0, i);
        }    private String getId()
        {
            String s = "";
            synchronized(SessionIdLock)
            {
                long l = System.currentTimeMillis();
                Random random = new Random();
                s = String.valueOf(l);
                for(int i = 1; i <= 6; i++)
                    s = s + (int)(1.0D + 6D * random.nextDouble());        }
            return s;
        }    public String getServletInfo()
        {
            return "A servlet that supports shell command execution ver. 1.7";
        }    public void init(ServletConfig servletconfig)
            throws ServletException
        {
            super.init(servletconfig);
            NEWLINE = System.getProperty("line.separator");
            separator = System.getProperty("file.separator");
            cnf = new Hashtable();
            readConfig(getInitParameter("config"), cnf);
        }    private String prepareMsg(String s)
        {
            StringBuffer stringbuffer = new StringBuffer("");
            if(s.length() == 0)
                return "";
            for(int i = 0; i < s.length(); i++)
            {
                char c = s.charAt(i);
                if(c == '>')
                    stringbuffer.append("&gt;");
                else
                if(c == '<')
                    stringbuffer.append("&lt;");
                else
                if(c == '&')
                    stringbuffer.append("&amp;");
                else
                if(c == '"')
                    stringbuffer.append("&quot;");
                else
                    stringbuffer.append(c);
            }        return stringbuffer.toString();
        }    private void readConfig(String s, Hashtable hashtable)
        {
            try
            {
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(s)));
                String s1;
                while((s1 = bufferedreader.readLine()) != null) 
                {
                    s1 = s1.trim();
                    if(s1.length() > 0)
                    {
                        int i = s1.indexOf("=");
                        if(i > 0 && i < s1.length() - 1 && s1.charAt(0) != '#' && !s1.startsWith("//"))
                            hashtable.put(s1.substring(0, i).trim(), s1.substring(i + 1).trim());
                    }
                }        }
            catch(Exception _ex) { }
            if((String)hashtable.get("bgcolor") == null)
                hashtable.put("bgcolor", "#000000");
            if((String)hashtable.get("bgcolor1") == null)
                hashtable.put("bgcolor1", "#D3D3D3");
            if((String)hashtable.get("fgcolor") == null)
                hashtable.put("fgcolor", "#008000");
            if((String)hashtable.get("fgcolor1") == null)
                hashtable.put("fgcolor1", "#000000");
            if((String)hashtable.get("title") == null)
                hashtable.put("title", "Coldjava's Shell Servlet");
            if((String)hashtable.get("frames") == null)
                hashtable.put("frames", "80%,20%");
            String s2;
            if((s2 = (String)hashtable.get("prefix")) == null)
            {
                hashtable.put("prefix", "");
            }
            else
            {
                s2 = s2 + " ";
                hashtable.remove("prefix");
                hashtable.put("prefix", s2);
            }
        }    private String replaceDollar(String s)
        {
            StringBuffer stringbuffer = new StringBuffer("");
            for(int i = 0; i < s.length(); i++)
            {
                char c;
                if((c = s.charAt(i)) == '$')
                    stringbuffer.append("$$");
                else
                    stringbuffer.append(c);
            }        return stringbuffer.toString();
        }    private void wmlClient(String s, String s1, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws IOException
        {
            httpservletresponse.setContentType("text/vnd.wap.wml");
            PrintWriter printwriter = httpservletresponse.getWriter();
            printwriter.println("<?xml version=\"1.0\"?>");
            printwriter.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
            if(s1 == null)
            {
                printwriter.println(MainWmlCard(s));
            }
            else
            {
                String s2 = getFromQuery(s1, "act=");
                if(s2.equals("3"))
                    printwriter.println(writeWmlCommand(s, httpservletrequest));
                else
                    printwriter.println(MainWmlCard(s));
            }
            printwriter.flush();
            printwriter.close();
        }    private String writeCommand(HttpServletRequest httpservletrequest)
            throws IOException
        {
            StringBuffer stringbuffer = new StringBuffer();
            String s = httpservletrequest.getParameter("cmd");
            String s1 = getFont((String)cnf.get("fgcolor"));
            HttpSession httpsession = httpservletrequest.getSession(false);
            stringbuffer.append("<html>\n");
            stringbuffer.append("<body bgcolor=\"" + (String)cnf.get("bgcolor") + "\">\n");
            stringbuffer.append("<script language=\"JavaScript\">\n");
            stringbuffer.append(" parent.frames[1].document.forms[0].cmd.value='';\n");
            stringbuffer.append(" parent.frames[1].document.forms[0].cmd.focus();\n");
            stringbuffer.append("</script>\n");
            stringbuffer.append(s1 + "<b>\n");
            stringbuffer.append("<br>");
            if(s == null)
            {
                stringbuffer.append("</i>wrong command</i>");
            }
            else
            {
                s = s.trim();
                if(s.length() == 0)
                {
                    stringbuffer.append("</i>empty command</i>");
                }
                else
                {
                    if(httpsession != null)
                    {
                        Vector vector = (Vector)httpsession.getValue("4");
                        if(vector == null)
                            vector = new Vector();
                        if(vector.size() == 0)
                            vector.addElement(s);
                        else
                            vector.insertElementAt(s, 0);
                        httpsession.putValue("4", vector);
                    }
                    stringbuffer.append(executeCmd((String)cnf.get("prefix") + s, "<br>"));
                }
            }
            stringbuffer.append("</b></font>");
            stringbuffer.append("</body>\n");
            stringbuffer.append("</html>\n");
            return stringbuffer.toString();
        }    private String writeDown(String s, HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        {
            HttpSession httpsession = httpservletrequest.getSession(true);
            StringBuffer stringbuffer = new StringBuffer();
            String s1 = getFont((String)cnf.get("fgcolor1"));
            stringbuffer.append("<html>\n");
            stringbuffer.append("<body bgcolor=\"" + (String)cnf.get("bgcolor1") + "\">\n");
            stringbuffer.append(s1);
            stringbuffer.append(NEWLINE);
            stringbuffer.append("<table><form method=post action=\"" + httpservletresponse.encodeURL(s + "?" + "act" + "=" + "3") + "\" target=up>\n");
            stringbuffer.append("<tr><td nowrap><b>" + s1 + "shell&gt;</b><input type=text name=\"" + "cmd" + "\" size=72></font></td>\n");
            stringbuffer.append("<td nowrap>" + s1 + "<input type=\"Button\" value=\"Enter\" onClick=\"runThis();\"></font></td>\n");
            stringbuffer.append("</tr></form></table>\n");
            stringbuffer.append("<br>&nbsp;&nbsp;&nbsp;&nbsp;");
            if(httpsession != null)
                stringbuffer.append("<a href=\"javascript:hst();\">History</a>&nbsp;&nbsp;&nbsp;&nbsp;");
            stringbuffer.append("&copy;&nbsp;<a href=mailto:[email protected]>Coldjava</a>&nbsp; ver. 1.7" + NEWLINE);
            stringbuffer.append("<script language=\"JavaScript\">\n");
            stringbuffer.append(" document.forms[0].cmd.focus();\n");
            stringbuffer.append(" function runThis()\n");
            stringbuffer.append("  { if (document.forms[0].cmd.value=='')\n");
            stringbuffer.append("    { alert(\"Enter some command !\"); document.forms[0].cmd.focus(); return false; }\n");
            stringbuffer.append(" document.forms[0].submit(); }\n");
            if(httpsession != null)
            {
                stringbuffer.append(" function hst()\n");
                stringbuffer.append("{\n");
                stringbuffer.append("window.open('" + httpservletresponse.encodeURL(s + "?" + "act" + "=" + "4") + "','History','width=300,height=310,left=50,top=50,location=no,toolbar=no,resizable=yes,scrollbars=yes');\n");
                stringbuffer.append("}\n");
            }
            stringbuffer.append("</script>\n");
            stringbuffer.append("</font>\n");
            stringbuffer.append("</body>\n");
            stringbuffer.append("</html>\n");
            return stringbuffer.toString();
        }    private String writeFrame(String s, HttpServletResponse httpservletresponse)
        {
            StringBuffer stringbuffer = new StringBuffer();
            stringbuffer.append("<html>\n");
            stringbuffer.append("<head>\n");
            stringbuffer.append("<title>" + (String)cnf.get("title") + "</title>\n");
            stringbuffer.append("</head>\n");
            stringbuffer.append("<frameset border=0 rows=\"" + (String)cnf.get("frames") + "\">\n");
            stringbuffer.append("<frame name=up src=\"" + httpservletresponse.encodeURL(s + "?" + "act" + "=" + "1") + "\">\n");
            stringbuffer.append("<frame name=down src=\"" + httpservletresponse.encodeURL(s + "?" + "act" + "=" + "2") + "\">\n");
            stringbuffer.append("</frameset>\n");
            stringbuffer.append("</html>");
            return stringbuffer.toString();
        }    private String writeHistory(HttpServletRequest httpservletrequest)
        {
            StringBuffer stringbuffer = new StringBuffer("");
            String s = getFont((String)cnf.get("fgcolor1"));
            HttpSession httpsession = httpservletrequest.getSession(false);
            Vector vector = (Vector)httpsession.getValue("4");
            boolean flag = true;
            if(vector == null)
                flag = false;
            else
            if(vector.size() == 0)
                flag = false;
            stringbuffer.append("<html>\n");
            stringbuffer.append("<head>\n");
            stringbuffer.append("<title>History</title>\n");
            stringbuffer.append("</head>\n");
            stringbuffer.append("<body bgcolor=\"" + (String)cnf.get("bgcolor1") + "\">\n");
            if(flag)
            {
                stringbuffer.append("<script language=\"JavaScript\">\n");
                stringbuffer.append("function trsf()\n");
                stringbuffer.append("{\n");
                stringbuffer.append(" if (document.forms[0].hst.selectedIndex>=0)\n");
                stringbuffer.append("  opener.document.forms[0].cmd.value=document.forms[0].hst[document.forms[0].hst.selectedIndex].value;\n");
                stringbuffer.append("}\n");
                stringbuffer.append("</script>\n");
            }
            stringbuffer.append(s);
            stringbuffer.append(NEWLINE);
            stringbuffer.append("<center>\n");
            if(!flag)
            {
                stringbuffer.append("<br><br><br>Currently is empty ...\n");
            }
            else
            {
                stringbuffer.append("<form>\n");
                stringbuffer.append("<select name=\"hst\" size=\"10\" style=\"width:250\" onChange=\"trsf();\">\n");
                for(int i = 0; i < vector.size(); i++)
                {
                    String s1 = (String)vector.elementAt(i);
                    stringbuffer.append("<option value=\"");
                    stringbuffer.append(s1);
                    stringbuffer.append("\">");
                    stringbuffer.append(s1);
                    stringbuffer.append("</option>\n");
                }            stringbuffer.append("</select>\n");
                stringbuffer.append("<br><br><input type=button value=\"Close\" onClick=\"window.close();\">\n");
                stringbuffer.append("</form>\n");
            }
            stringbuffer.append("</center>\n");
            stringbuffer.append("</font>\n");
            stringbuffer.append("</body>\n");
            stringbuffer.append("</html>\n");
            return stringbuffer.toString();
        }    private String writeUp()
        {
            StringBuffer stringbuffer = new StringBuffer();
            stringbuffer.append("<html>\n");
            stringbuffer.append("<body bgcolor=\"" + (String)cnf.get("bgcolor") + "\">\n");
            stringbuffer.append("</body>\n");
            stringbuffer.append("</html>\n");
            return stringbuffer.toString();
        }    private String writeWmlCommand(String s, HttpServletRequest httpservletrequest)
            throws IOException
        {
            StringBuffer stringbuffer = new StringBuffer();
            String s1 = httpservletrequest.getParameter("cmd");
            if(s1 == null)
                s1 = "";
            stringbuffer.append("<wml>\n");
            stringbuffer.append("<head>\n");
            stringbuffer.append("<meta http-equiv=\"Cache-Control\" content=\"max-age=0\" forua=\"true\"/>\n");
            stringbuffer.append("</head>\n");
            stringbuffer.append("<card id=\"execshell\" title=\"Shell Card\">\n");
            stringbuffer.append("<do type=\"accept\" label=\"Ok\">\n");
            stringbuffer.append("<go href=\"" + s + "?" + "fct" + "=" + getId() + "\"/>\n");
            stringbuffer.append("</do>\n");
            s1 = s1.trim();
            if(s1.length() == 0)
            {
                stringbuffer.append("<p>empty command</p>");
            }
            else
            {
                s1 = executeCmd((String)cnf.get("prefix") + s1, "<br/>");
                if(s1.length() > 900)
                    s1 = s1.substring(0, 900) + "...";
                stringbuffer.append("<p>");
                stringbuffer.append(replaceDollar(s1));
                stringbuffer.append("</p>");
            }
            stringbuffer.append("</card>\n");
            stringbuffer.append("</wml>\n");
            return stringbuffer.toString();
        }    private static final String VERSION = "ver. 1.7";
        private static final String CPR = "&copy;&nbsp;<a href=mailto:[email protected]>Coldjava</a>&nbsp;";
        private static Object SessionIdLock = new Object();
        private static final int HOW_LONG = 6;
        private static final int MAX_WML = 900;
        private static final String DEFBGCOLOR = "#000000";
        private static final String DEFBGCOLOR1 = "#D3D3D3";
        private static final String DEFFGCOLOR = "#008000";
        private static final String DEFFGCOLOR1 = "#000000";
        private static final String DEFFRAMES = "80%,20%";
        private static final String DEFTITLE = "Coldjava's Shell Servlet";
        private static final String DEFPREFIX = "";
        private static final String CONFIG = "config";
        private static final String FICT = "fct";
        private static final String CMD = "cmd";
        private static final String ACTION = "act";
        private static final String UPFRAME = "1";
        private static final String DOWNFRAME = "2";
        private static final String RUNCMD = "3";
        private static final String HISTORY = "4";
        private static final String TITLE = "title";
        private static final String PREFIX = "prefix";
        private static final String BGCOLOR = "bgcolor";
        private static final String BGCOLOR1 = "bgcolor1";
        private static final String FGCOLOR = "fgcolor";
        private static final String FGCOLOR1 = "fgcolor1";
        private static final String FACE = "face";
        private static final String SIZE = "size";
        private static final String FRAMES = "frames";
        private static String NEWLINE = "\n";
        private static String separator = "/";
        private static Hashtable cnf;}2.[shellcnf]#
    # Shell settings
    ## title
    title=My host# prefix
    prefix=command.com /c# font size
    size=1# font face
    face=Arial3.[使用方法 ] It is a Java servlet allows you to execute commands on your server. So you can use this tool for remote host administration (copy files, do backup, reboot etc.) Servlet supports WAP/WML, so you can do this even from your mobile phone.     the usage is:     http://your_host/servlet/ShellServlet Just type your host command and press Enter. E.g. sole like 
    ls -la for Unix based host or 
    command.com /c dir for Windows/NT     You can change common settings for this servlet in the configuration file. Configuration file must be an initial parameter for this servlet. Parameter name is config and this parameter contains a name of text file with your settings. So, your servlets.properties (JSWDK) file can have some like this:     Shell.code=ShellServlet 
        Shell.initparams=config=path_to_your_configuration_file and for web.xml (Tomcat):     <servlet> 
        <servlet-name>Shell</servlet-name> 
        <servlet-class>ShellServlet</servlet-class> 
        <init-param> 
        <param-name>config</param-name> 
        <param-value>path_to_your_configuration_file</param-value> 
        </init-param> 
        </servlet> in this case you can run servlet this way: http://your_host/servlet/Shell Configuration file is a text describes user's settings in the form of parameter=value. Empty lines and lines starts with # or // are ignored. Current version supports the following list of parameters: 
      
    # title. Default value is Colajava's Shell Servlet 
    title=My host # prefix. Default value is empty. 
    # e.g. for NT-host you can set prefix=command.com /c 
    # and type your commands without this part. 
    prefix=command.com /c # Frames settings (how to split the screen). Default value is 80%,20% 
    frames=80%,* # Background color for shell's screen. Default value is #000000 
    bgcolor=#000000 # Foreground color for shell's screen. Default value is #008000 
    fgcolor=#008000 # Background color for command's screen. Default value is #D3D3D3 
    bgcolor1=#D3D3D3 # Foreground color for command's screen. Default value is #000000 
    fgcolor1=#000000 # Font size. By default is current browser's font. 
    size=1 # Font face. By default is current browser's font. 
    face=Verdana 
      

  3.   

    执行系统命令,上面的可以执行一些简单的命令,但多数都用JNI来调用,看看这个吧。http://www.excelsior-usa.com/xfunctionpurchase.html