1、tomcat是一款免費(fèi)的開源Web服務(wù)器,如果部署在本地,那么對應(yīng)的那么為localhost,對應(yīng)地址為127.0.0.1。
例子:可以通過http://localhost:8080/項(xiàng)目root值訪問,也可以通過http://127.0.0.1/項(xiàng)目root值訪問。
如果部署在服務(wù)器(linux)系統(tǒng)類,則需要通過服務(wù)器的Ip地址進(jìn)行訪問。
2、下面說說怎么獲取Ip地址:
獲取本地的Ip地址:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost(); //獲取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121 String hostAddress = address.getHostAddress()); //192.168.0.121 InetAddress address1 = InetAddress.getByName( "www.wodexiangce.cn" ); //獲取的是該網(wǎng)站的ip地址,比如我們所有的請求都通過nginx的,所以這里獲取到的其實(shí)是nginx服務(wù)器的IP地 String hostAddress1 = address1.getHostAddress()); //124.237.121.122 InetAddress[] addresses = InetAddress.getAllByName( "www.baidu.com" ); //根據(jù)主機(jī)名返回其可能的所有InetAddress對象 for (InetAddress addr:addresses){ System.out.println(addr); //www.baidu.com/14.215.177.38 //www.baidu.com/14.215.177.37 } } catch (UnknownHostException e) { e.printStackTrace(); } } |
獲取服務(wù)器的Ip地址(其他人寫的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/** * 獲取服務(wù)器IP地址 * @return */ @SuppressWarnings ( "unchecked" ) public static String getServerIp(){ String SERVER_IP = null ; try { Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces(); InetAddress ip = null ; while (netInterfaces.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement(); ip = (InetAddress) ni.getInetAddresses().nextElement(); SERVER_IP = ip.getHostAddress(); if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf( ":" ) == - 1 ) { SERVER_IP = ip.getHostAddress(); break ; } else { ip = null ; } } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SERVER_IP; } } |
基于SSM框架的農(nóng)業(yè)物聯(lián)網(wǎng)智能養(yǎng)殖系統(tǒng)中的養(yǎng)殖日志要求上傳一張圖片到服務(wù)器中。本地測試時,由于保存的路徑在本地磁盤E中,所以后臺直接從本地獲取了資源文件。傳入服務(wù)器胡,找不到該文件,估計是IP地址無法獲取到,只有對應(yīng)的文件路徑,基于此,想設(shè)計出從服務(wù)器里讀取文件信息,但是并沒有成功。后來發(fā)現(xiàn)localhost與127.0.0.1是一致的,就想起了用服務(wù)器IP地址代替localhost完成讀取操作,但本質(zhì)仍然是前臺界面的讀取。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/george93/p/6306579.html