2017-12-28 30 views
0

サーバアドレスのパスを取得すると、私はこの実際の名前と私は問題を使用してHttpServletRequest.Theことにより、実際のサーバーアドレスを取得しようとしています

String hostdonate = request.getScheme() + "://" + 
    request.getHeader("host") + 
    request.getContextPath()+"/getDonationDetails" 

のようなコードを使用する場合には、テストプロジェクト名ですhttp://localhost:8084/Test/getDonationDetails.Hereを返しますです私のcomputer.Now内のlocalhostのためにこれは問題ありません。サーバアドレス33.124.165.12.Nowにアプリケーションをデプロイしたいのですが、同じコードを使用していれば、http://localhost:8080/getDonationDetails.Hereのようなものが返されます。getDonationDetailsは返す後に実行したいメソッドですそれは、 "site not reachable"というエラーで空のページにリダイレクトされています。33.124.165.12/getDonationDetailsや正しい名前のような私のサーバの実際のアドレスを取得する方法oサーバーアドレスのパス。

答えて

0

あなたはこのようないくつかのものを使用することができます。

public String getInetAddress() { 
     Enumeration<NetworkInterface> e = null; 
     String address = null; 
     try { 
      e = NetworkInterface.getNetworkInterfaces(); 
     } catch (SocketException e1) { 
      LOGGER.info("Error in : " + e1); 
      e1.printStackTrace(); 
     } 
     while (e.hasMoreElements()) { 
      NetworkInterface n = (NetworkInterface) e.nextElement(); 
      if ("eth0".equalsIgnoreCase(n.getName())) { 
       Enumeration<InetAddress> ee = n.getInetAddresses(); 
       while (ee.hasMoreElements()) { 
        InetAddress i = (InetAddress) ee.nextElement(); 
        address = i.getHostAddress(); 
       } 
      } 
     } 
     return address; 
    } 

これは、既存のサーバーのプライベートIPを返します。

+0

住所に自分のメソッド名を追加するにはどうすればいいですか? –

+0

動作していません。エラーページが返されています –

0

getRequestURL()HttpServletRequestとしてみてください。これは、サーバーのIPを取得できるはずのプロトコル、サーバー名、ポート番号、およびサーバーパスを返します。

関連する問題