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 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短