共計 406 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中獲取 服務器 路徑,可以使用以下方法之一:
- 使用
System.getProperty()
方法獲取服務器的工作目錄路徑:
String serverPath = System.getProperty("user.dir");
- 使用
ServletContext
對象獲取服務器的絕對路徑:
ServletContext context = request.getServletContext();
String serverPath = context.getRealPath("/");
- 使用
Class
對象獲取類所在的路徑:
String serverPath = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
注意:以上方法獲取的服務器路徑可能因服務器配置不同而有所差異,建議根據具體情況選擇合適的方法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完