Java如何获取Web应用程序的真实路径?
此代码可帮助您获取在服务器上部署Web应用程序的物理路径。这可能很有用,因此您可以在服务器上读取或写入文件。但是请注意,此方法仅在以爆炸方式部署Web应用程序时才有效,如果以某种war格式部署,则该getRealPath()方法只需return即可null。
package org.nhooo.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetWebApplicationPathServlet extends HttpServlet {
public GetWebApplicationPathServlet() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String path = getServletContext().getRealPath("/");
PrintWriter writer = res.getWriter();
writer.println("Application path: " + path);
}
}
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短