我用如下的servlet来调用一个exe程序,exe程序见下。
当用tomcat运行这个servlet时发现任务管理器里闪了一下这个exe,但是exe没有打开文件,请问这是怎么回事?
package myHelloWorld;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Scanner;
public class testopenfile extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out=response.getWriter();
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("</head>");
out.write("<body>\r\n");
out.print("going to capture data on sunxudong's pc");
String string_port_num;
String exetestpath="g:/sunAppPacket/openfile.exe";
String fileName="g:/sunAppPacket/netport_config.txt";
//
BufferedReader in=new BufferedReader(new FileReader(fileName));
string_port_num=in.readLine();
in.close();
run_exetest(string_port_num,exetestpath);
out.write("</body>\r\n");
out.write("</html>");
}//fun: doget
public static void run_exetest(String string_port_num,String exepath)
{ String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null; // launch EXE and grab stdin/stdout and stderr
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(exepath);
stdin = p.getOutputStream ();
stderr = p.getErrorStream ();
stdout = p.getInputStream ();
// "write" the parms into stdin
line = string_port_num + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
}catch (Exception e) {
}//catch}//void run
}//class
exe程序如下,要求用户输入一段内容写到hello。txt里面#include <fstream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
int main(){
ofstream outfile;
char cCharContent[100];
printf("please input the content to write in\n");
scanf("%s",cCharContent);
outfile.open("hello.txt");
outfile<<cCharContent;
outfile.close();return 1;
}
当用tomcat运行这个servlet时发现任务管理器里闪了一下这个exe,但是exe没有打开文件,请问这是怎么回事?
package myHelloWorld;
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Scanner;
public class testopenfile extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
{
PrintWriter out=response.getWriter();
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("</head>");
out.write("<body>\r\n");
out.print("going to capture data on sunxudong's pc");
String string_port_num;
String exetestpath="g:/sunAppPacket/openfile.exe";
String fileName="g:/sunAppPacket/netport_config.txt";
//
BufferedReader in=new BufferedReader(new FileReader(fileName));
string_port_num=in.readLine();
in.close();
run_exetest(string_port_num,exetestpath);
out.write("</body>\r\n");
out.write("</html>");
}//fun: doget
public static void run_exetest(String string_port_num,String exepath)
{ String line;
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null; // launch EXE and grab stdin/stdout and stderr
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
p = rn.exec(exepath);
stdin = p.getOutputStream ();
stderr = p.getErrorStream ();
stdout = p.getInputStream ();
// "write" the parms into stdin
line = string_port_num + "\n";
stdin.write(line.getBytes() );
stdin.flush();
stdin.close();
}catch (Exception e) {
}//catch}//void run
}//class
exe程序如下,要求用户输入一段内容写到hello。txt里面#include <fstream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
int main(){
ofstream outfile;
char cCharContent[100];
printf("please input the content to write in\n");
scanf("%s",cCharContent);
outfile.open("hello.txt");
outfile<<cCharContent;
outfile.close();return 1;
}
解决方案 »
- 如何在jsp页面解释html标签(这些标签是从数据库读取来的字符)
- 计算机等级考试练习系统(送100分)
- Webservice能够传递包含公有成员方法的复杂对象吗?
- 那位朋友比较熟悉webservice?帮忙看一个wsdl 谢谢!
- 各位大侠!请问关于webshere6“不推荐使用资源 jdbc/wfmsds 的直接 JNDI 查找”的错误怎么解决,很急!还请各位高手百忙之中帮忙解决!先
- hibernate异常could not deserialize,初学帮分析一下
- 运行EJB的客户测试文件出错问题
- hibernate如何中配置jndi?
- 请问如何在java应用程序中加入图片?
- 请教 javamail 在 linux 上发送邮件
- <bean:define 标签使用问题,请问如果对应的bean是null,如何返回一个空的bean?
- 问大家一个用apache poi读取excel文件的问题
21 public class testopenfile extends HttpServlet
22 {
23 public void doGet(HttpServletRequest request, HttpServletResponse respo nse)
24 throws IOException,ServletException
25 {
26 PrintWriter out=response.getWriter();
27 out.write("<html>\r\n");
28 out.write("<head>\r\n");
29 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
30 out.write("</head>");
31
32
33 out.write("<body>\r\n");
34
35
36 out.print("going to capture data on sunxudong's pc");
37
38
39 String string_port_num;
40
40
41 String exetestpath="g:/sunAppPacket/openfile.exe";
42
43
44 String fileName="g:/sunAppPacket/netport_config.txt";
45 //
46 BufferedReader in=new BufferedReader(new FileReader(fileName));
47 string_port_num=in.readLine();
48 in.close();
49
50
51
52
53 run_exetest(string_port_num,exetestpath);
54
55 out.write("</body>\r\n");
56 out.write("</html>");
57
58 try {
59 Thread.sleep(2000);
60 } catch(InterruptedException e) {
61 }
62
40,1 44%
63
64
65
66
67
68 }//fun: doget
69
70
71
72
73 public static void run_exetest(String string_port_num,String exepath)
74 {
75
76 String line;
77 OutputStream stdin = null;
78 InputStream stderr = null;
79 InputStream stdout = null;
80
81 // launch EXE and grab stdin/stdout and stderr
82
83
84
85 85
86 Runtime rn = Runtime.getRuntime();
87 Process p = null;
88 try {
89
90 p = rn.exec(exepath);
91 stdin = p.getOutputStream ();
92 stderr = p.getErrorStream ();
93 stdout = p.getInputStream ();
94
95 // "write" the parms into stdin
96 line = string_port_num + "\n";
97 stdin.write(line.getBytes() );
98 stdin.flush();
99
100
101 stdin.close();
102
103
104 }catch (Exception e) {
105
106 }//catch
107
}//void run
109
110
111 }//class