当前位置: 代码迷 >> J2EE >> java调用console程序打开文件的有关问题
  详细解决方案

java调用console程序打开文件的有关问题

热度:75   发布时间:2016-04-22 00:35:35.0
java调用console程序打开文件的问题
我用如下的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;
}



------解决方案--------------------
换成bat调用文件就好了
  相关解决方案