英文:
How to get rewritten url
问题
部署我的应用(.war)到Tomcat时,在webapps中得到一个名为'myapp-0.0.1-SNAPSHOT'的目录
这个应用我通过AJP重定向
server.xml(部分)
<Connector protocol="AJP/1.3" address="::1" port="8009"
redirectPort="8443" secretRequired="false" />
在Apache Httpd中,我使用以下代码
httpd.conf(部分)
<Location /myapp>
ProxyPass ajp://localhost:8009/myapp-0.0.1-SNAPSHOT
</Location>
为了在我的应用程序中创建下载链接,我使用
final String baseUrl =
ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
当我在测试环境中启动应用程序时,我得到的网址如下:
对于网页 http://192.168.10.1.85/
对于下载 http://192.168.10.1:85/files/PRN1/001234/firmware/file.zip
(此链接基于数据库中的数据动态生成)
好的,我看到了,一切正常。
当我在网页中打开Httpd链接时
http://192.168.10.1/myapp
- 没问题,正确
但是对于我的动态链接,我得到了错误的上下文路径
http://192.168.10.1/myapp-0.0.1-SNAPSHOT/files/PRN1/001234/firmware/file.zip
- 不正确,我的应用无法从此链接提供数据
正确的链接 http://192.168.10.1/myapp/files/PRN1/001234/firmware/file.zip
如何正确地描述baseUrl?
或者我应该在配置中添加关于Location的一些数据?
英文:
When I deploy my app (.war) at tomcat I get directory in webapps named like 'myapp-0.0.1-SNAPSHOT'
this app I redirect by AJP
server.xml (partial)
<Connector protocol="AJP/1.3" address="::1" port="8009"
redirectPort="8443" secretRequired="false" />
in Httpd apache I use the following code
httpd.conf (partial)
<Location /myapp>
ProxyPass ajp://localhost:8009/myapp-app-0.0.1-SNAPSHOT
</Location>
For creating download url in my app I'm using
final String baseUrl =
ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
when I start app on test I get url like
for web http://192.168.10.1.85/
for downloading http://192.168.10.1:85/files/PRN1/001234/firmware/file.zip
(this link generated based on data from database and it is dynamically)
Ok. it's good I see all correctly.
When I open httpd link in web
http://192.168.10.1/myapp
- ok it's correct
but for my dynamic link I get incorrect context path
http://192.168.10.1/myapp-0.0.1-SNAPSHOT/files/PRN1/001234/firmware/file.zip
- it's not correct and my app couldn't provide data from this link
good link http://192.168.10.1/myapp/files/PRN1/001234/firmware/file.zip
How to correct describe baseUrl?
Or I should add data about Location some in configuration?
答案1
得分: 1
据我理解,从数据库中获取的路径是动态生成的,类似于 "files/PRN1/001234/firmware/file.zip",然后您将此路径与您的网络应用程序地址结合。
接下来,您可以在Java中使用以下URL生成方法:
String protocol = "http";
String host = "192.168.10.1";
int port = 8080;
String path = "/myapp/files/PRN1/001234/firmware/file.zip";
String auth = null;
String fragment = null;
URI uri = new URI(protocol, auth, host, port, path, query, fragment);
URL url = uri.toURL();
英文:
As far as I understand, from Database "files/PRN1/001234/firmware/file.zip" this path generated dynamically and then you combine your web application's address with this
path.
Then you can use below url generation method in java
String protocol = "http";
String host = "192.168.10.1";
int port = 8080;
String path = "/myapp/files/PRN1/001234/firmware/file.zip";
String auth = null;
String fragment = null;
URI uri = new URI(protocol, auth, host, port, path, query, fragment);
URL url = uri.toURL();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论