在Java中获取根域名或服务器名称+端口?

时间:2014-05-21 06:04:12

标签: java

好的,我正在我的本地服务器中测试我的WebApp,其中包含这样的主页网址" http://127.0.0.0:8080"。

好的,在我的Godaddy VPS中,我安装了相同的应用程序,我的家庭网址是" mydomain.com"

在ROOT文件夹中,我有一个文件夹" staticpage"。那里有很多文件:

/staticpage/test.txt /staticpage/test2.txt ...

如何用Java编程获取静态页面文件夹的位置?

我确实喜欢这个

    StringBuilder rootDomainNameSb = new StringBuilder("http://");
    rootDomainNameSb.append(httpRequest.getServerName());
    if (httpRequest.getServerPort() != 0) {
      rootDomainNameSb.append(":");
      rootDomainNameSb.append(httpRequest.getServerPort());
    }
    String rootDomainName=rootDomainNameSb.toString(); // this will print out http://127.0.0.0:8080
    String finalUrl=rootDomainName+"/staticpage/test.txt";

上面的代码运行正常,但我不确定它是否适用于VPS?

在VPS中,mydomain.com已映射到http://127.0.0.0:8080

以上代码在VPS中是否可以正常工作?

2 个答案:

答案 0 :(得分:1)

让您的生活更轻松,只需使用相对路径。

答案 1 :(得分:0)

找到1个解决方案:

        String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));

        InputStream is = new FileInputStream(absolutePath+"/project/server/staticpage/test.txt");
相关问题