Tomcat 应用程序未读取输入文本文件

Tomcat 应用程序未读取输入文本文件,tomcat,jakarta-ee,openshift,Tomcat,Jakarta Ee,Openshift,我已经成功地使用tomcat7将Java EE应用程序部署到openshift。我部署了war文件并加载了欢迎页面,但当我的应用程序尝试向应用程序读取input.txt文件时,却找不到它。任何帮助都将不胜感激 我的输入文件(2)位于eclipse项目中的/WEB-INF文件夹中 这是我的启动servlet中的相关代码: public void doPost(HttpServletRequest req, HttpServletResponse resp) throws S

我已经成功地使用tomcat7将Java EE应用程序部署到openshift。我部署了war文件并加载了欢迎页面,但当我的应用程序尝试向应用程序读取input.txt文件时,却找不到它。任何帮助都将不胜感激

我的输入文件(2)位于eclipse项目中的/WEB-INF文件夹中

这是我的启动servlet中的相关代码:

    public void doPost(HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException {

        //  11 lines of code setting strings according to input parameters
        //  from startup jsp which has forms for submitting operations

        if ("Dates and Draws".equals(dates))  {

            Powerball p = new Powerball();
            Megamillions m = new Megamillions();

            String path = this.getServletConfig().getServletContext().getRealPath("/WEB-INF");

            p.appPath = path; // appPath is a class variable
            m.appPath = path;

            p.readnums(req, resp);  // App is not reading input files in readnums methods

            m.readnums(req, resp);



            try  {

                // code for output using readnums methods, reading input files

                } finally {


                }


        }
    }
Powerball servlet readnums(与Mega数百万servlet readnums相同)

void readnums(HttpServletRequest-req,
HttpServletResponse resp)抛出ServletException、IOException、FileNotFoundException{
int i,j;
字符串温度;
字符串[]Temp2=新字符串[80];
字符串路径=appPath+“/powerballnumbers.txt”;
文件检查=新文件(路径);
如果(!file_check.exists()){
errorMessage=“未找到输入文件”;//这是显示的内容
分别为setContentType(“文本/html”);
PrintWriter out=resp.getWriter();
out.println(“”);
out.println(“”);
out.println(“+errorMessage+”);
out.println(“

”);
out.println("确保
server.xml
中的
unpackWARs
属性设置为
true
,否则war文件将不会展开,Tomcat将无法找到您的文件。

在我的eclipse项目中,server.xml位于Tomcat下。unpackWARs设置为true。这不是Tomcat服务器的配置吗openshift?我可以将此设置包含到我的war文件中吗?请参阅本页底部的
其他提示部分:修复了它。非常感谢!值得思考的是:@BalusC谢谢。getRealPath()但是似乎正在工作。我不想到处去尝试修复一些迄今为止仍在工作的东西。我阅读了该链接。你的文章非常好。如果你认为我绝对不应该使用它,请告诉我,我将尝试你的文章在该链接中提出的更改。我如何使用servletContext.getResourceP引用WEB-INF文件夹中的输入文件aths(/)?由于包含“readnums”方法的servlet类从未被服务器实例化,因此它从未获得上下文。因此,我向该servlet的doPost方法传递了服务器以“ServletContext=getServletContext();”实例化的启动上下文。我传递了“context”到第二个类中的doPost方法,然后到使用getResourceAsStream的“readnums”方法,使用传递的上下文:“InputStream input=context.getResourceAsStream(path);”Info:GetResourcePath在一个字符串变量中返回/WEB-INF中每个文件的所有路径…据我所知不可用。仅供参考。
    void readnums(HttpServletRequest req,
        HttpServletResponse resp) throws ServletException, IOException , FileNotFoundException {

        int i,j;
        String Temp;
        String [] Temp2 = new String [80];


        String path = appPath + "/powerballnumbers.txt";

        File file_check = new File (path);

        if (!file_check.exists()) {

            errorMessage = "Input file not found";  //This is what gets displayed

            resp.setContentType("text/html");
            PrintWriter out = resp.getWriter();


            out.println("<html>");
            out.println("<body>");
            out.println("<h3>" + errorMessage + "</h3>");
            out.println("<br><br>");

            out.println("<form action=\"Startup\" method=\"get\"");
            out.println("Go back to previous page: ");
            out.println("<br><br>");
            out.println("<input type=\"submit\" name=\"action\" value=\"Go Back to previous page\">");
            out.println("</form>");

            out.println("</body>");
            out.println("</html>");

            out.close();

            return;

        } else  {

            errorMessage = "";

        }


        draws = 1;

        BufferedReader br = null;

        br = new BufferedReader (new FileReader(new File(path)));

        while ((Temp = br.readLine()) != null) {

            if (Temp.isEmpty()) break;

            Temp2 = Temp.split("\\s+");

            int LastNonEmpty = -1;

            for (i = 0; i < Temp2.length; i++) {

                if (Temp2[i] != "") {

                    LastNonEmpty += 1;

                    Temp2[LastNonEmpty] = Temp2[i];

                }

            }

            drawdate [draws] = Temp2 [0];

            j = 1;

            for (i = 1; i <= 5; i++) {

                Temp = Temp2 [j];

                nums [draws][i] =  Integer.parseInt(Temp); // nums is class variable

                j ++;
            }

            Temp = Temp2 [6];

            xball [draws] =  Integer.parseInt(Temp); // xball is class variable

            draws++;
        }

        br.close();

        draws--;


    }