当前位置: 首页 >  焦点 >  >  详情
Java获取访问者IP-每日热闻
2023-02-06 03:57:03    来源:哔哩哔哩


(资料图片)

记录下点进链接或访问网页设备的公网IP地址;我记得这个当时这是个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();        }        //获取当前日期精确到秒        String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());     //保存IP即可    }}

关键词: 访问网页 当前日期

上一篇:我为什么选择福特新全顺短轴低顶款改装床车 热点
下一篇:最后一页