共計 497 個字符,預計需要花費 2 分鐘才能閱讀完成。
有多種方法可以獲取 服務器 路徑,以下是其中的一些方法:
- 使用
ServletContext
對象的getRealPath()
方法:
String path = request.getServletContext().getRealPath("/");
- 使用
HttpServletRequest
對象的getServletContext()
方法獲取ServletContext
對象,然后再通過getRealPath()
方法獲取路徑:
String path = request.getServletContext().getRealPath("/");
- 使用
ClassLoader
類的getResource()
方法獲取類路徑下的資源,然后再通過getFile()
方法獲取路徑:
String path = getClass().getClassLoader().getResource("").getFile();
- 使用
System.getProperty()
方法獲取當前工作目錄:
String path = System.getProperty("user.dir");
請根據具體的應用場景選擇合適的方法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完