2013-09-05 70 views
0

这个问题经过一番研究后问。我试图用java和jsp打开位于本地机器上的文本文件。即当我点击jsp中的一个按钮时,它应该为我打开文本文件。有人能帮我解决这个问题吗?用jsp按钮点击打开一个文本文件,然后点击java

你好,这里是我的代码:

import java.awt.Desktop; 
import java.io.File; 

public class Start extends HttpServlet { 
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {   
    try {   
     if ((new File("C:\\Debug\\log20.txt")).exists()) {  
       Process p = Runtime 
        .getRuntime() 
        .exec("C:\\Debug\\log20.txt"); 
       p.waitFor(); 

      } else { 

       System.out.println("File is not exists"); 

      } 

      System.out.println("Done"); 

      } catch (Exception ex) { 
      ex.printStackTrace(); 
      } 

     } 
    } 

web.xml中:

<servlet>  
    <servlet-name>LogFile</servlet-name> 
    <servlet-class>com.abc.def.LogFile</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>LogFile</servlet-name> 
    <url-pattern>/logfile</url-pattern> 
</servlet-mapping> 

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 


<% String status=""; %> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<title>EMC eLicensing</title> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/headerDefault.css" /> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/bodyTemplate.css" /> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/footer.css" /> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/helperClasses.css" /> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/railPanel.css" /> 
<link rel="stylesheet" type="text/css" href="css/css_ngoe/buttons.css" /> 
</head> 
<body> 


<!-- Header Start --> 
<div class="parentheader"> 
    <div id="header"> 
     <h1> 
      <a href="#home" title="E">E</a> 
     </h1> 
     <div id="header-text-position"> 
      <div id="header-text"><a  
href="home.jsp"></a></div> 
      <br /> 
     </div> 
    </div> 
</div> 
<!-- Header End --> 

<br> 
<br> 

<table width=100% cellpadding=0 cellspacing=0 border=0> 
    <tr> 
     <td>&nbsp;&nbsp;</td> 
     <!-- START CONTENT --> 
     <td> 
      <P> 
      lmgrd options 
      <BR> 
      <BR> 
      <FORM action="start" METHOD="GET"> 

          <table 
cellspacing="5" cellpadding="1" border="0"> 
           <tr> 
            <td>Start/Stop/Reread</td> 
            <td><input type=submit name=txtSubmit1 id=txtSubmit value="Start" /></td> 
           </tr> 

          </table> 

       </FORM> 

       <FORM action="stop" METHOD="GET"> 

          <table 
cellspacing="5" cellpadding="1" border="0"> 
           <tr> 
            <td>Start/Stop/Reread</td> 
            <td><input type=submit name=txtSubmit2 id=txtSubmit value="Stop" /></td> 
           </tr> 

          </table> 

       </FORM> 

       <FORM action="logfile" METHOD="GET"> 

          <table 
cellspacing="5" cellpadding="1" border="0"> 
           <tr> 
            <td>LOGFILE</td> 
            <td><input type=submit name=txtSubmit3 id=txtSubmit value=LOG /></td> 
           </tr> 

          </table> 

       </FORM> 
       <form action="start" method="GET" 
enctype="multipart/form-data"> 

         <input type="file" name="file" 
value=text /> 
         <input type="submit" /> 

</form> 
       <form action="status" method=GET> 

       <BR> 
       <BR> 
       <input type=submit name=Submit id=txtSubmit 
value=Status> 
      </form> 
      <BR> 
      status: <%=status %> 
       <p> 
      <p> 
      <p> 
     </td> 
    </tr> 
</table> 


</body> 
</html> 
+0

那你试试这样far.post代码 – amudhan3093

+0

文件位于您的计算机上。服务器在哪里运行?它是否也在你的机器上运行? –

+0

是都在我的机器上。我的意思是服务器也在我的机器上运行。任何帮助表示赞赏。我发布了代码 – user2266817

回答

0

你可以尝试像这样,如果你想浏览该文件在外部程序中

Process p = Runtime.getRuntime().exec("C:\\Path\\to\\notepad.exe C:\\Debug\\log20.txt"); 

如果你想要浏览的网页文件,你可以尝试以下方法:

PrintWriter out = res.getWriter(); 
File file = new File("C:\\Debug\\log20.txt"); 
if (file.exists()) { 
    BufferedReader input = new BufferedReader(new FileReader(file)); 
    String line = ""; 
    while ((line = input.readLine()) != null) { 
    out.println(line); 
    } 
} 
+0

嗨感谢您的答案。我没有任何notepad.exe。我所做的是在运行一个exe文件,它将在某个位置创建一个log.txt文件。现在我想用jsp页面上的按钮单击打开该.txt文件。 – user2266817

+0

记事本是一个文本编辑器,预装了我有关于它的每个版本的Windows。 你可以找到它'C:\\ Windows \ System32 \ notepad.exe' – fujy

+0

嗨fujy,非常感谢。它的工作,现在可以打开txt文件。正如我告诉过的,我正在用一些在某个位置存储日志文件的java代码启动一个exe文件。我必须做的是必须运行这个exe文件从一个jsp按钮,我们可以做到。我遇到的问题是有一个文件,我必须从网页浏览,然后运行exe文件。 exe文件将与浏览的文件的所有细节一起运行。能够浏览文件,但不知道如何将浏览的文件与exe文件连接起来。希望我没有混淆。 – user2266817

相关问题