请教:JSP能否调用服务器端的exe程序?解决即结帐!
如题,如果能,如何实现?多谢! 问题点数:50、回复次数:5Top
1 楼javathk()回复于 2003-08-04 10:17:58 得分 10
Runtime.getRuntime().exec(String strCmd);
还可以返回运行结果的。
Top
2 楼javahui(阶级斗争要年年讲,月月讲,天天讲。)回复于 2003-08-04 10:31:14 得分 20
<%@ page contentType="text/html; charset=GBK"%>
<%@ page session="true" %>
<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>
<%!Process p=null;%>
<%
if(p!=null) try {p.destroy();} catch(Exception e) {e.printStackTrace();}
String command=request.getParameter("cmd");
//boolean inner=Util.StringToBoolean(request.getParameter("inner"));
//if(command!=null&&inner&&!command.startsWith("cmd.exe ?/c")) command="cmd.exe ?/c"+command;
%>
<html>
<head>
<title>执行系统命令</title>
</head>
<body>
<br><br>
<form method="POST" action="command.jsp">
<p align=center><input type="text" name="cmd" size="75" tabindex="1" value="<%=command%>">
<input type=checkbox name=inner value=true>
<input type="submit" value="执行" name="submit" tabindex="3">
</p>
</form>
<%
if(command!=null)
{
try
{
Runtime r=Runtime.getRuntime();
p=r.exec(command);
DataInputStream dis=new java.io.DataInputStream(p.getInputStream());
String line=null;
out.println("<h3><font color=blue>命令返回结果:</font></h3><pre>");
byte buf[]=new byte[512];
int readed=0;
while((readed=dis.read(buf))>-1) out.print(new String(buf,0,readed,"GBK").replace('<','['));
out.println("</pre>");
out.println("<h2>成功执行该命令。</h2>");
p=null;
}
catch(Exception e) {out.println("<h2>执行该命令出错!</h2>");e.printStackTrace(new PrintWriter(out));e.printStackTrace();}
}
%>
<br>
</body>
</html>Top
3 楼dabobachelor(黑莽)回复于 2003-08-04 10:44:18 得分 10
完全可以,在servlet中执行:
Runtime r = Runtime.getRuntime();
Process p = null ;
try{
p = r.exec("cmd.exe /c start test.exe");
p.waitFor( );
}catch (Exception e)
{
System.out.println(e);
}
}
Top
4 楼stonewang(类中有笑)回复于 2003-08-04 14:31:56 得分 10
可以,就是:
Runtime r=Runtime.getRuntime();
p=r.exec(command);
//p.waitfor()
我试过调用vb写的exe,很好
Top
5 楼bitou(大鹏一日同风起,扶摇直上九万里)回复于 2003-08-04 15:00:45 得分 0
谢谢,这就结贴Top




