2017-12-28 15 views
0

서버 주소의 경로를 얻는 것은 내가이실제 이름과 내가 문제를 사용 HttpServletRequest.The하여 실제 서버 주소를 얻기 위해 노력하고

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

과 같은 코드를 사용하는 경우 그것은 테스트 프로젝트 이름입니다 http://localhost:8084/Test/getDonationDetails.Here을 반환 할 것이다 . 내 컴퓨터에있는 localhost에 대해 괜찮습니다. 이제 서버 주소 33.124.165.12. 내에서 응용 프로그램을 배포하고 싶습니다. 같은 코드를 사용하는 경우 http://localhost:8080/getDonationDetails.Here과 같은 것을 반환합니다. getDonationDetails는 반환 후 실행하려는 메서드입니다. 주소로. 그리고 그것은 "사이트에 연결할 수 없다"라는 오류가있는 빈 페이지로 리디렉션됩니다. 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를 검색 할 수있는 프로토콜, 서버 이름, 포트 번호 및 서버 경로를 반환합니다.

관련 문제