好的,我正在我的本地服务器上测试我的WebApp,它有像这样的主页url "http://127.0.0.0:8080“。
好的,在我的谷歌爸爸VPS中,我安装了同样的应用程序,我的家庭地址是"mydomain.com“。
在根文件夹中,我有一个文件夹"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中,它是映射到http://127.0.0.0:8080的mydomain.com
上面的代码在VPS中可以正常工作吗?
发布于 2014-05-21 14:15:41
让你的生活变得更简单,只使用相对路径。
发布于 2014-05-21 15:07:07
找到一个解决方案:
String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
InputStream is = new FileInputStream(absolutePath+"/project/server/staticpage/test.txt");https://stackoverflow.com/questions/23775166
复制相似问题