JSP - 系统找不到指定的文件

时间:2017-05-25 08:52:05

标签: java jsp

我在java中创建了一个函数,用于验证文件属性是否存在,如果存在,则检索其内容,否则通过将其写入内容来创建它。 通过eclipse从java应用程序启动此函数,它运行正常,而从jsp页面调用函数时找不到该文件并给出错误。

这是我的java函数:

private static File fileName = new File("properties.properties");
Properties props = new Properties();

public void writeProperties() throws FileNotFoundException, IOException{        
    if(fileName.exists()){
        props.load(new FileInputStream(fileName));
    }

    boolean equal = false;
    if(!UtilsFunction.isEmpty(props.getProperty("table-list"))){
        String [] arr = props.getProperty("table-list").split(",");

        for(int i=0;i<arr.length;i++){
            if(arr[i].trim().equals(tableName)){
                equal = true;
                break;
            }
        }
        if(!equal){
            props.setProperty("table-list", props.getProperty("table-list")+","+tableName);
        }
    }else{
        props.setProperty("table-list", tableName);
    }

    OutputStream out = new FileOutputStream(fileName);
    props.store(out, "properties tables");
}

这是我的JSP页面:

<% ClassNameJava tableExecute = new ClassNameJava(tableName);%>
<% tableExecute.writeProperties(); %>

这是错误:

    HTTP Status [500] – [Internal Server Error]
    Type Exception Report

Message java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    java.io.IOException: java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Root Cause

    java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        java.io.FileInputStream.open0(Native Method)
        java.io.FileInputStream.open(Unknown Source)
        java.io.FileInputStream.<init>(Unknown Source)
        packageTableManager.TableExecute.writeProperties(TableExecute.java:233)
        org.apache.jsp.config_jsp._jspService(config_jsp.java:199)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Note The full stack trace of the root cause is available in the server logs.

    Apache Tomcat/8.

5.15

1 个答案:

答案 0 :(得分:0)

尝试以下代码。将您的properties.properties文件保存在src包中。

        Properties prop = new Properties();
        try
        {
            // load a properties file from class path, inside static method
            prop.load(ClassNameJava.class.getClassLoader().getResourceAsStream("properties.properties"));

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