获取重写后的 URL 的方法

huangapple go评论83阅读模式
英文:

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)

&lt;Connector protocol=&quot;AJP/1.3&quot; address=&quot;::1&quot; port=&quot;8009&quot;
       redirectPort=&quot;8443&quot; secretRequired=&quot;false&quot; /&gt;

in Httpd apache I use the following code
httpd.conf (partial)

&lt;Location /myapp&gt;
    ProxyPass ajp://localhost:8009/myapp-app-0.0.1-SNAPSHOT
&lt;/Location&gt;

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 = &quot;http&quot;;
    String host = &quot;192.168.10.1&quot;;
    int port = 8080;
    String path = &quot;/myapp/files/PRN1/001234/firmware/file.zip&quot;;
    String auth = null;
    String fragment = null;
    URI uri = new URI(protocol, auth, host, port, path, query, fragment);
    URL url = uri.toURL();

huangapple
  • 本文由 发表于 2020年8月19日 15:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63481726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定