Java獲取訪問者IP

記錄下點進鏈接或訪問網(wǎng)頁設(shè)備的公網(wǎng)IP地址;我記得這個當(dāng)時這是個AI幫我寫的,挺好用!
package com.example.myweb.controller;
import com.example.myweb.entiy.Ip;
import com.example.myweb.mapper.IpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/get")
public class GetIp {
? ?@Autowired
? ?private IpMapper ipMapper;
? ?@Autowired
? ?private Ip ips;
? ?//獲取訪問者IP
? ?@RequestMapping("/ip")
? ?public void getIp(HttpServletRequest request) {
? ? ? ?String ip = request.getHeader("x-forwarded-for");
? ? ? ?if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
? ? ? ? ? ?ip = request.getHeader("Proxy-Client-IP");
? ? ? ?}
? ? ? ?if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
? ? ? ? ? ?ip = request.getHeader("WL-Proxy-Client-IP");
? ? ? ?}
? ? ? ?if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
? ? ? ? ? ?ip = request.getRemoteAddr();
? ? ? ?}
? ? ? ?//獲取當(dāng)前日期精確到秒
? ? ? ?String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
? ? //保存IP即可
? ?}
}