应用程序不读取输入文本文件 [英] app not reading input text files

查看:176
本文介绍了应用程序不读取输入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have successfully deployed a Java EE app to openshift with tomcat7. I deployed the war file and the welcome page loads but when my app tries to read an input .txt file to the application it can't find it. Any help would be appreciated.

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

My input files (2) are in the /WEB-INF folder in my project in eclipse.

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

This is my relevant code from my startup 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(与Megamillions servlet readnums相同)

Powerball servlet readnums (same as Megamillions servlet readnums)

    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--;


    }


推荐答案

确保 server.xml中的 unpackWARs 属性设置为 true ,否则war文件不会被扩展,Tomcat将无法找到你的文件。

Make sure that the unpackWARs attribute is set to true in server.xml, otherwise the war file will not be expanded and Tomcat will not be able to find your file.

这篇关于应用程序不读取输入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆