使用jsp按钮单击和java打开文本文件

时间:2013-09-05 18:11:33

标签: java jsp button text click

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

嗨,这是我的代码:

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>

1 个答案:

答案 0 :(得分: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);
  }
}