Java 如何执行批处理文件&。使用jspservlet的exe文件?
我正在以Java 如何执行批处理文件&。使用jspservlet的exe文件?,java,jsp,servlets,Java,Jsp,Servlets,我正在以FASTA格式为blast提供输入。之后,我想在我的项目文件夹中创建序列.FASTA文件。我想执行批处理文件,参数为blastp.exefile、sequence.FASTAfile、数据库文件、输出文件 创建sequence.FASTA文件时没有问题 ERROR IN: the batch file is not execute, So the I am not able to get the output file for display. ERROR is : java.io.
FASTA格式
为blast提供输入。之后,我想在我的项目文件夹中创建序列.FASTA
文件。我想执行批处理文件,参数为blastp.exe
file、sequence.FASTA
file、数据库文件、输出文件
创建sequence.FASTA文件时没有问题
ERROR IN: the batch file is not execute, So the I am not able to get the output file for display.
ERROR is : java.io.FileNotFoundException: D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\toxin-data\outputprotein.txt (The system cannot find the file specified)
我的代码如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String values=request.getParameter("t1");
ServletContext servletContext = request.getSession().getServletContext();
servletContext = request.getSession().getServletContext();
String path="//sequence.FASTA"; /*file is created in getRealPath()*/
String uploadFile=servletContext.getRealPath(path);
File outputFile = new File(uploadFile);
FileWriter fout = new FileWriter(outputFile);
fout.write(values);
fout.close();
servletContext = request.getSession().getServletContext();
String blastPath="//blast.bat";
String blastBat_path=servletContext.getRealPath(blastPath);
Process process = new ProcessBuilder(blastBat_path).start();
try {
process.waitFor();
} catch (InterruptedException e) {e.printStackTrace();}
process.destroy();
/* to read output file for display */
BufferedReader br1=null;
br1= new BufferedReader(new FileReader(servletContext.getRealPath("//outputprotein.txt")));
try {
StringBuilder sb = new StringBuilder();
String line1 = br1.readLine();
while (line1 != null) {
sb.append(line1);
sb.append(System.lineSeparator());
line1 = br1.readLine();
}
String everything = sb.toString();
out.println("<pre>"+everything+"</pre>");
} finally {
br1.close();
}
fout.close();
}
protectedvoiddopost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
字符串值=request.getParameter(“t1”);
ServletContext=request.getSession().getServletContext();
servletContext=request.getSession().getServletContext();
String path=“//sequence.FASTA”/*文件是在getRealPath()中创建的*/
String uploadFile=servletContext.getRealPath(路径);
File outputFile=新文件(上传文件);
FileWriter fout=新的FileWriter(outputFile);
fout.写入(值);
fout.close();
servletContext=request.getSession().getServletContext();
字符串blastPath=“//blast.bat”;
字符串blastBat_path=servletContext.getRealPath(blastPath);
Process Process=新的ProcessBuilder(blastBat_路径).start();
试一试{
process.waitFor();
}catch(InterruptedException e){e.printStackTrace();}
process.destroy();
/*读取输出文件以进行显示的步骤*/
BufferedReader br1=null;
br1=新的BufferedReader(新的文件阅读器(servletContext.getRealPath(“//outputprotein.txt”);
试一试{
StringBuilder sb=新的StringBuilder();
字符串line1=br1.readLine();
while(第1行!=null){
sb.追加(第1行);
sb.append(System.lineSeparator());
line1=br1.readLine();
}
String everything=sb.toString();
out.println(“+一切+”);
}最后{
br1.close();
}
fout.close();
}
这里的getRealpath()
是:
D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp4\wtpwebapps\toxin data
您确定文件位于您尝试执行它的位置吗?您是说
getRealPath()
为您提供…\tmp4\wtpwebapps\public\u html
,并且您可以调用servletContext.getRealPath(“//outputprotein.txt”)
。但与此同时,您会收到一条错误消息,表示无法在处找到该文件…\tmp4\wtpwebapps\toxin data\outputprotein.txt
。这些目录不匹配,您真的确定文件在Servlet认为的位置吗?它不是关于outputprotein.txt的。它s关于blast.batch文件,该文件不用于创建outputprotein.txt文件.ya。。该文件位于要执行的同一文件夹中