在Eclipse Dynamic Web Project中放置静态文件的目录 [英] Where to place a directory of static files in Eclipse Dynamic Web Project

查看:1303
本文介绍了在Eclipse Dynamic Web Project中放置静态文件的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse创建了一个动态Web项目。我有几个Java程序,我放在Java Resources / src文件夹中。这些程序使用我放在 WebContent / WEB-INF / lib 文件夹中的 Lucene Java程序需要访问几个文本文件和包含由 Lucene 生成的索引文件的目录。我将这些静态文件放在eclipse中的 WebContent 下,以便它们出现在导出的WAR文件中。



我正在通过直接在Java程序中引用它们来访问这些静态文件。

  BufferedReader br = new BufferedReader(new FileReader(abc.txt)); 




// abc.txt位于Eclipse项目的WebContent文件夹中。


从JSP页面,我正在调用java程序(其中包含上述行),但它显示一个 FileNotFoundException异常。请帮助我解决这个问题。

解决方案

您无法从Java直接访问webapp内的资源。

由于 /src/YourClass.java 中的文件位于 / WEB-INF / classes / 编译时。所以,当你尝试访问 BufferedReader br = new BufferedReader(new FileReader(abc.txt));



根据您的示例,在/ WEB-INF / classes / abc.txt中搜索abc.txt。



使用 servletContext.getRealPath(/); 返回您的Web应用程序的 webapps 目录的路径,然后您可以使用此路径访问资源。



注意 servletContext.getRealPath(/); 也取决于您如何部署Web应用程序。默认情况下,eclipse使用自己的内部机制来部署Web应用程序。



下面是关于如何应用



Servlet代码

  import java.io.File; 
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StaticTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletContext servletContext;
private String rootPath;

public StaticTestServlet(){
super();
// TODO自动生成的构造函数stub
}
@Override
public void init(ServletConfig config)throws ServletException {
// TODO自动生成的方法stub
super.init(config);
servletContext = config.getServletContext();
rootPath = servletContext.getRealPath(/);
}

protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
// TODO自动生成的方法存根
System.out.println 在Get和我的路径:+ rootPath +文档); //文件是静态文件的直接名称
}

protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
// TODO自动生成的方法存根

System.out.println(在Post和我的路径:+ rootPath +documents); //文件是静态文件的目录名称
}
}


I created a dynamic web project using Eclipse. I have a few java programs which I placed in the "Java Resources/src" folder. These programs use Lucene libraries which I placed in the "WebContent/WEB-INF/lib" folder. The Java programs need access to a few text files and a directory containing the index files generated by Lucene. I placed these static files under WebContent in eclipse so that they appear in the exported WAR file.

I am accessing these static files by referring to them directly in the Java program.

BufferedReader br = new BufferedReader(new FileReader("abc.txt"));

//abc.txt is in the WebContent folder of Eclipse project.

From the JSP page, I am calling the java program (which contains the above line) but it shows a FileNotFoundException. Please help me regarding this.

解决方案

You can not access resources available inside webapp directly from Java.

As files from /src/YourClass.java goes under /WEB-INF/classes/ when compiled. So, When you try to access BufferedReader br = new BufferedReader(new FileReader("abc.txt"));.

It searches "abc.txt" at ``/WEB-INF/classes/abc.txt` as per your given example.

Use servletContext.getRealPath("/"); which returns path of your web application's webapps directory and then you can access resources using this path.

Note: The path returned by servletContext.getRealPath("/"); also depends on how you have deployed web application. As by default eclipse used its own internal mechanism to deploy web application.

Here is the sample screenshot on how it should be

Servlet Code:

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StaticTestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private ServletContext servletContext;
    private String rootPath;

    public StaticTestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        super.init(config);
        servletContext = config.getServletContext();
        rootPath = servletContext.getRealPath("/");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("In Get and my path: " + rootPath + "documents"); // documents is the direcotry name for static files
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("In Post and my path: " + rootPath + "documents"); // documents is the direcotry name for static files
    }
}

这篇关于在Eclipse Dynamic Web Project中放置静态文件的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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