Java中使用HttpServletRequest获取ip和端口-详解
创始人
2025-01-15 00:33:40
0

目录

1.简单例子

2.详细使用例子---Java项目

2.1.Controller层

2.2service层

2.2.1接口

2.2.2serviceImpl实现类

3.IpUtils工具类


1.简单例子

也可以直接用

public void doGet(HttpServletRequest request, HttpServletResponse response)     throws ServletException, IOException {        String ServerName = request.getServerName();//返回服务器的主机名        String ServerPort = request.getServerPort();//返回服务器的端口号          String uri = request.getRequestURI();//返回请求行中的资源名称        String url = request.getRequestURL().toString();//获得客户端发送请求的完整url        String ip = request.getRemoteAddr();//返回发出请求的IP地址        String params = request.getQueryString();//返回请求行中的参数部分        String host=request.getRemoteHost();//返回发出请求的客户机的主机名        int port =request.getRemotePort();//返回发出请求的客户机的端口号          System.out.println(ServerName);        System.out.println(ServerPort);                System.out.println(ip);        System.out.println(url);        System.out.println(uri);        System.out.println(params);        System.out.println(host);        System.out.println(port); }

2.详细使用例子---Java项目

真正用到在serviceImpl实现类中

2.1.Controller层

@RestController @RequestMapping("/api") public class DataReportController {  private final DataReportService dataReportService; private final BusinessMsgCodeProperties props; @Autowired public DataReportController(DataReportService dataReportService, BusinessMsgCodeProperties props){     this.dataReportService = dataReportService;     this.props = props; }  /**  * 数据上报接⼝  */ @PostMapping("/dataReport")//这个接口既传文件参数又传实体实参,request不是参数,前端不传后端可自行获取 public Resource dataReport(@RequestPart(value = "file", required = true) MultipartFile file, @RequestPart  @Validated DataReportInDto dto, HttpServletRequest request){     String code = dataReportService.dataReport(file,dto,request);     return new Resource<>(props.getProcessStatus(code));       //Resource为自行封装的返回类型,可以用不用 }  }

2.2service层

2.2.1接口

public interface DataReportService extends IService {      /**      * 数据上报接⼝      */ String dataReport(MultipartFile file, DataReportInDto dto, HttpServletRequest request); }

2.2.2serviceImpl实现类

@Service @Slf4j public class DataReportServiceImpl extends ServiceImpl implements DataReportService {  private final DataReportMapper dataReportMapper; @Autowired public DataReportServiceImpl(DataReportMapper dataReportMapper){     this.dataReportMapper = dataReportMapper; }   @Override public String dataReport(MultipartFile file, DataReportInDto dto, HttpServletRequest request) {     //一、获取调用者的ip+port     String ip = "";     String port = "";     String loginName = "";     try {         ip = IpUtils.getRemoteHost(request);     //自己封装一个工具类(对特殊情况做了一定处理)         String ip1 =  request.getRemoteAddr();   //直接获取         port = String.valueOf(request.getRemotePort());         System.out.println("远程端的ip1:"+ip1);         System.out.println("远程的端口:"+ip);         System.out.println("远程端口:"+port);         log.info("获取调用者ip:"+ip+" ,"+"port:"+port);     } catch (Exception e) {         e.printStackTrace();         log.info("获取调用者ip+port失败");     } return "COMMON_ERROR"; }  }

3.IpUtils工具类

直接整个粘贴就能用

package com.nengyy.speed_rest_server.utils;        //所在包  import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest;  public class IpUtils {     public static String getRemoteHost(HttpServletRequest request){         String ip = request.getHeader("x-forwarded-for");         if(StringUtils.isNoneBlank(ip) && !"unknown".equalsIgnoreCase(ip)){             int idx = ip.indexOf(",");             if(idx != -1){                 ip = ip.substring(0, idx);             }         }         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();         }         return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;     } }

相关内容

热门资讯

交流学习经验!微信小程序免费黑... 交流学习经验!微信小程序免费黑科技(外挂),微信小程序微乐自建房脚本下载切实有挂规律亲,关键说明,透...
最终!功夫川麻老是输什么情况,... 最终!功夫川麻老是输什么情况,仟众部落辅助(总是真的有平台)-哔哩哔哩1、任何功夫川麻老是输什么情况...
今日焦点!微乐小程序免费黑科技... 今日焦点!微乐小程序免费黑科技(外挂),微信微乐小程序辅助软件原来真的有挂1、上手简单,内置详细流程...
据公告内容!微信微乐辅助器免费... 据公告内容!微信微乐辅助器免费安装,皮皮游戏辅助器(确实真的有插件)-哔哩哔哩据公告内容!微信微乐辅...
玩家必看科普!!微信小程序免费... 玩家必看科普!!微信小程序免费黑科技(外挂),在哪里下载微乐自建房辅助软件真是有挂方针暗藏猫腻,小编...
为了进一步!腾威互娱 辅助工具... 为了进一步!腾威互娱 辅助工具,盛世辅助(果然真的有插件)-哔哩哔哩1、腾威互娱 辅助工具公共底牌简...
一起来讨论!微乐小程序免费黑科... 一起来讨论!微乐小程序免费黑科技(外挂),微信微乐有挂没有本来有挂详细1、完成辅助器v3.3的残局,...
连日来!小程序牵手跑得辅助器下... 连日来!小程序牵手跑得辅助器下载,星悦软件辅助(确实是有平台)-哔哩哔哩1、上手简单,内置详细流程视...
让我来分享经验!微乐小程序免费... 让我来分享经验!微乐小程序免费黑科技(外挂),微信小程序微乐内蒙破解器都是真的有挂1、完成有辅助插件...
据权威媒体报道!关春天辅助器安... 据权威媒体报道!关春天辅助器安全吗,小唐家乐园山西辅助软件(好像存在有工具)-哔哩哔哩1、小唐家乐园...