0

如何在 Web 应用程序中指定文件的路径?我在 WEB-INF 下有一个名为“模板”的文件夹,有人告诉我,在 GlassFish v3 下,路径应该如下所示:

./WebContent/WEB-INF/templates

但这样我得到了一个找不到文件的异常。为了使它工作,我必须改变什么?

4

4 回答 4

2

如果我理解正确,您不能依赖当前工作目录来定位您已部署的资源。如果您的资源是相对于类路径资源物理放置的(例如在 jar 中),您可以询问资源在哪里,然后从那里导航。

servlet 如何获取 servlet 外部文件的绝对路径?这又来自http://www.exampledepot.com/egs/java.lang/ClassOrigin.html

Class cls = this.getClass();
ProtectionDomain pDomain = cls.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();  // file:/c:/almanac14/examples/
于 2010-06-30T12:33:35.643 回答
1

当您在 Eclipse 中创建动态 Web 应用程序项目时,将进入 war 文件根目录的内容将从 WebContent 文件夹中打包。

听起来您想在运行时为您的 Web 应用程序访问目录 WEB-INF/templates 中的文件。

我假设您目前正在使用绝对路径从那里访问文件。您已经发现,一旦部署,这可能不适用于您的应用程序。

您将需要使用ServletContext.getResourceAsStream(String)访问文件的内容。

以下代码片段从 servlet 中查找名为 WEB-INF/templatez/myfile.txt 的文件,该 servlet 是包含 myfile.txt 文件的 Web 应用程序的一部分。其他 Web 应用程序和用户将无法通过 http GET 请求访问该文件。

package a;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="FileFinder", urlPatterns={"/FileFinder"})
public class FileFinder extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FileFinder</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FileFinder at " + request.getContextPath () + "</h1>");
            InputStream is = null;
            try {
                is = request.getServletContext().getResourceAsStream("/WEB-INF/templatez/myfile.txt");
                out.println((null == is ? "did not " : "did ") + "find the file myfile.txt");
            } finally {
                if (null != is) is.close();
            }

            out.println("</body>");
            out.println("</html>");
            //*/
        } finally { 
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }

}
于 2010-06-03T06:04:40.727 回答
0

Ok, I figured this out. Can't believe the solution was this simple. I just moved the templates folder to WebContent folder, same place JSP and HTML pages are at and changed the path in the DD to /templates. Now I'm pretty sure it'll work under any web container on any server.

于 2010-06-18T05:49:29.193 回答
0

servlet 3.0 的资源 JAR 功能是否有用:http: //blogs.oracle.com/alexismp/entry/web_inf_lib_jar_meta

于 2010-06-24T10:53:02.160 回答