在WebContent下读取文件/文件夹的端点URL

时间:2014-07-07 05:38:10

标签: java servlets

我使用下面的代码来获取我的端点url

   URL url=getServletContext().getResource("/PdfToHtml");
    System.out.println(url);

输出显示为

  **jndi:/ipaddress/LoginValidation/PdfToHtml** 

但是我需要将实际输出显示为像这样的完整URL

  **http://ipaddress:8080/LoginValidation/PdfToHtml**

请告诉我在这种情况下该怎么做。

2 个答案:

答案 0 :(得分:0)

可能你可以尝试这样..

    String url=getServletContext().getResource("/index101.jsp").getPath();
    url=url.replace("localhost", "localhost:8080");//if you need port otherwise comment it
    System.out.println(url);

答案 1 :(得分:0)

最后,我得到了许多努力和所有帮助的回答,

String fullcontextpath = request.getScheme()+“://”+ InetAddress.getHostName()。getHostAddress()+“:”+ request.getServerPort()+“/”+ request.getContextPath();

它返回url路径,getScheme - >返回http或https。 Inet地址返回ip地址,serverport返回服务器端口号,最后是curent prog \ ject context。

谢谢大家的帮助。

相关问题