尝试从JSP页面读取txt文件。

时间:2012-07-26 13:03:56

标签: file jsp

我正在尝试从.txt文件中读取.jsp页面中的一些数据。但是我得到了这个错误:

有根本原因

java.io.FileNotFoundException: Predicciones\predicciones.txt (System can't find path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at org.apache.jsp.getDatosPredicciones_jsp._jspService(getDatosPredicciones_jsp.java:123)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

代码是这样的:

{
        int partido = Integer.parseInt(request.getParameter("partido"));
        int irabazlea;
        float probabilidad;


        FileInputStream fstream = new FileInputStream("Predicciones\\predicciones.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int kont=1;
        while (((strLine = br.readLine()) != null) && (kont<=partido))

问题出在哪里? “Predicciones”目录与jsp位于同一级别。

我试图将“Predicciones”目录放在WEB-INF中,但它不起作用。

2 个答案:

答案 0 :(得分:2)

如果您的文件位于您的Web应用程序根目录中,请尝试使用

FileInputStream fstream = new FileInputStream(application.getRealPath("/Predicciones/predicciones.txt"));

答案 1 :(得分:0)

是的,但是jsp被编译并放在与实际jsp文件不同的地方。

要从WEB-INF文件夹中访问文件,请使用

/WEB-INF/foldername/resource.ext

请注意 /

如果您使用/folder/me.jpg作为路径,那么您可以访问此文件ww.mydomain.com/appname/folder/me.jpg,除非应用程序以root身份部署。在这种情况下,您将访问这个资源ww.mydomain.com/folder/me.jpg

要访问资源,请阅读http://docs.oracle.com/javase/1.4.2/docs/guide/resources/resources.html

在您的情况下,您可以使用绝对路径访问该文件,例如://apache //webapps/myapp/myfolder...et或您可以使用相对路径,其中资源相对于已编译的.class文件