servlet中的文件读取问题

发布于 2024-12-23 06:40:18 字数 1189 浏览 1 评论 0 原文

我必须使用 servlet 读取文件。这是我正在使用的代码。但是文件没有使用此代码读取。始终打印 File contains null value---------------- -

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setContentType("text/html");
        String filename = "D/root.properties";
        ServletContext context = getServletContext();

        InputStream inp = context.getResourceAsStream(filename);
        if (inp != null) {
            InputStreamReader isr = new InputStreamReader(inp);
            BufferedReader reader = new BufferedReader(isr);
            PrintWriter pw = response.getWriter();

            String text = "";

            while ((text = reader.readLine()) != null) {                     
            }
        } else {
            System.out.println("File contains null value-----------------");
        }
    } catch(Exception e) {
        System.out.println("Rxpn............................................."+e);
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}

I have to read a file using servlet.here is the code iam using.but file is not reading using this code.Always printing File contains null value-----------------:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        response.setContentType("text/html");
        String filename = "D/root.properties";
        ServletContext context = getServletContext();

        InputStream inp = context.getResourceAsStream(filename);
        if (inp != null) {
            InputStreamReader isr = new InputStreamReader(inp);
            BufferedReader reader = new BufferedReader(isr);
            PrintWriter pw = response.getWriter();

            String text = "";

            while ((text = reader.readLine()) != null) {                     
            }
        } else {
            System.out.println("File contains null value-----------------");
        }
    } catch(Exception e) {
        System.out.println("Rxpn............................................."+e);
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜司空 2024-12-30 06:40:18

javadoc救援:

java.net.URL getResource(java.lang.String 路径)
抛出 java.net.MalformedURLException

返回映射到给定路径的资源的 URL。

路径必须以 / 开头,并被解释为相对于当前上下文根目录,或相对于 /META-INF/resources 目录
Web 应用程序的 /WEB-INF/lib 目录中的 JAR 文件。
该方法将首先搜索Web应用程序的文档根目录
在搜索任何 JAR 文件之前查找所请求的资源
/WEB-INF/lib 内。 JAR文件里面的顺序
搜索到的 /WEB-INF/lib 未定义。

如果您想读取 Web 应用程序中的资源,请使用上面指定的路径。如果要从文件系统读取,请使用文件 IO(以及正确的文件名):new FileInputStream("D:/root.properties")

javadoc to the rescue :

java.net.URL getResource(java.lang.String path)
throws java.net.MalformedURLException

Returns a URL to the resource that is mapped to the given path.

The path must begin with a / and is interpreted as relative to the current context root, or relative to the /META-INF/resources directory
of a JAR file inside the web application's /WEB-INF/lib directory.
This method will first search the document root of the web application
for the requested resource, before searching any of the JAR files
inside /WEB-INF/lib. The order in which the JAR files inside
/WEB-INF/lib are searched is undefined.

If you want to read from a resource in the web app, use a path as indicated above. If you want to read from the file system, use file IO (and the correct file name): new FileInputStream("D:/root.properties")

天生の放荡 2024-12-30 06:40:18

使用以下代码。
这样你就可以读取文件

    File file = new File("Filepath");

    try {
        if (file.exists()) {
            BufferedReader objBufferReader = new BufferedReader(
                    new FileReader(file));

            ArrayList<String> arrListString = new ArrayList<String>();
            String sLine = "";
            int iCount = 0;

            while ((sLine = objBufferReader.readLine()) != null) {
                arrListString.add(sLine);
            }
            objBufferReader.close();

            for (iCount = 0; iCount < arrListString.size(); iCount++) {
                if (iCount == 0) {
                    createTable(arrListString.get(iCount).trim());
                } else {
                    insertIntoTable(arrListString.get(iCount).trim());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

Use following code.
With this you can read file

    File file = new File("Filepath");

    try {
        if (file.exists()) {
            BufferedReader objBufferReader = new BufferedReader(
                    new FileReader(file));

            ArrayList<String> arrListString = new ArrayList<String>();
            String sLine = "";
            int iCount = 0;

            while ((sLine = objBufferReader.readLine()) != null) {
                arrListString.add(sLine);
            }
            objBufferReader.close();

            for (iCount = 0; iCount < arrListString.size(); iCount++) {
                if (iCount == 0) {
                    createTable(arrListString.get(iCount).trim());
                } else {
                    insertIntoTable(arrListString.get(iCount).trim());
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
放肆 2024-12-30 06:40:18
 public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream file = new FileInputStream("c:\\hi.txt");
        DataInputStream input = new DataInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        String text = "";
        while ((text = br.readLine()) != null) {
            System.out.println(text);
        }
    }
}

请尝试上面的代码示例。我认为在您的代码中找不到文件。请给出上面代码中的文件路径并尝试。

 public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileInputStream file = new FileInputStream("c:\\hi.txt");
        DataInputStream input = new DataInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        String text = "";
        while ((text = br.readLine()) != null) {
            System.out.println(text);
        }
    }
}

Please try the above code sample. I think in your code , file is not found. Please give the file path in the above code and try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文