Java获取Web工程内网访问URL(形如:http://IP:8080/ProjectName)

本文介绍了一种简便的方法来获取Web项目的内网URL。通过一个Java程序,自动获取局域网内的IP地址并结合项目名称,生成完整的访问路径。

由于有时候Web项目作为外部API调用时,每次启动都要先查一下IP,拼接项目名,然后呼叫该URL,很繁琐。因此写了一个小程序,用于Web项目获取内网URL,作为公用类,测试阶段很实用。如果访问不了局域网tomcat下的项目,可以看我另一篇博客,有详细说明。

地址:http://blog.youkuaiyun.com/womeng2009/article/details/53513731

下面是Java源代码:

package com.GetIP;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/*
 * 功能说明:获取Web工程内网访问URL
 * 开发者:Jiro.Chen
 * 时间:2016-12-14 10:40:12
 * */

public class GetWebProUrl {

	public static String getLocalHostIP(){
		String result = null;
		
		try {
			
			InetAddress IP = Inet4Address.getLocalHost();
			String ip = IP.toString();
			
			int index = ip.indexOf('/');
			
			result = ip.substring(index+1, ip.length());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public static String getProjectURL(String IP){
		String result = "http://";
		
		result += IP;
		result += ":8080/";
		
		String proPath = System.getProperty("user.dir");
		String proName = proPath.substring(proPath.lastIndexOf("\\")+1, proPath.length());
		
		result += proName;
		
		return result;
	}
	
	public static void main(String[] args) {
		String localhostIP = GetWebProUrl.getLocalHostIP();
		
		String ProjectURL = GetWebProUrl.getProjectURL(localhostIP);
		System.out.println(ProjectURL);
	}
}
输出示例:

http://192.168.1.80:8080/EDUToHome


13:15:52.333 [http-nio-7002-exec-9] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - [logException,208] - Resolved [org.springframework.jdbc.UncategorizedSQLException: <EOL><EOL>### Error updating database. Cause: java.sql.SQLException: Failed to read auto-increment value from storage engine<EOL><EOL>### The error may exist in file [F:\DIYOA\BPM_1\ruoyimate-master\ruoyi-system\target\classes\mapper\bpm\RfProjectReportMapper.xml]<EOL><EOL>### The error may involve com.ruoyi.bpm.mapper.RfProjectReportMapper.insertRfProjectReport-Inline<EOL><EOL>### The error occurred while setting parameters<EOL><EOL>### SQL: insert into rf_project_report ( user_id, user_name, title, project_category, sign_company, project_name, client_company_name, client_short_code, sales_leader, initial_contact, contact_info, project_location, plan_start_time, plan_end_time, budget_amount, project_type, after_sales_type, project_intro, project_level, create_time ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )<EOL><EOL>### Cause: java.sql.SQLException: Failed to read auto-increment value from storage engine<EOL>; uncategorized SQLException; SQL state [HY000]; error code [1467]; Failed to read auto-increment value from storage engine; nested exception is java.sql.SQLException: Failed to read auto-increment value from storage engine] 13:15:52.350 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,135] - ==> Preparing: insert into sys_oper_log(title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, cost_time, oper_time) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate()) 13:15:52.350 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,135] - ==> Parameters: 项目报备(String), 1(Integer), com.ruoyi.web.controller.bpm.RfProjectReportController.add()(String), POST(String), 1(Integer), admin(String), 睿丰自动化(String), /bpm/RfProjectReport(String), 127.0.0.1(String), 内网IP(String), {"afterSalesType":"直销","budgetAmount":"55","clientCompanyName":"国药集团医药物流有限公司","clientShortCode":"GYJT","contactInfo":"12345","createTime":"2025-09-15 13:15:52","initialContact":"测试用","params":{},"planEndTime":"2025-09-15","planStartTime":"2025-09-15","projectCategory":"正常项目","projectIntro":"55","projectLevel":"新客户","projectLocation":"55","projectName":"tt","projectType":"仓储物流","salesLeader":"超级管理员","signCompany":"系统公司","title":"超级管理员-项目报备-2025-09-15","userId":1,"userName":"超级管理员"}(String), null, 1(Integer), ### Error updating database. Cause: java.sql.SQLException: Failed to read auto-increment value from storage engine ### The error may exist in file [F:\DIYOA\BPM_1\ruoyimate-master\ruoyi-system\target\classes\mapper\bpm\RfProjectReportMapper.xml] ### The error may involve com.ruoyi.bpm.mapper.RfProjectReportMapper.insertRfProjectReport-Inline ### The error occurred while setting parameters ### SQL: insert into rf_project_report ( user_id, user_name, title, project_category, sign_company, project_name, client_company_name, client_short_code, sales_leader, initial_contact, contact_info, project_location, plan_start_time, plan_end_time, budget_amount, project_type, after_sales_type, project_intro, project_level, create_time ) values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ### Cause: java.sql.SQLException: Failed to read auto-increment value from storage engine ; uncategorized SQLException; SQL state [HY000]; error code [1467]; Failed to read auto-increment value from storage engine; nested exception is java.sql.SQLException: Failed to read auto-increment value from storage engine(String), 102(Long) 13:15:52.365 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,135] - <== Updates: 1 13:15:52.484 [http-nio-7002-exec-24] INFO o.o.e.b.a.p.SubmitProcessImpl - [doProcessButton,49] - Start submit process. param:RfProjectReportVO(userId=1, userName=null, applicant=null, department=null, processNo=null, title=null, projectCategory=null, signCompany=null, projectName=null, projectNo=null, clientCompanyName=null, clientShortCode=null, salesLeader=null, initialContact=null, contactInfo=null, position=null, reportTime=null, projectLocation=null, planStartTime=null, planEndTime=null, budgetAmount=null, projectType=null, afterSalesType=null, projectIntro=null, projectLevel=null, remark=null, createBy=null, createTime=null, updateBy=null, updateTime=null) 13:15:52.500 [http-nio-7002-exec-24] DEBUG c.r.b.m.R.insert - [debug,135] - ==> Preparing: INSERT INTO rf_project_report ( id ) VALUES ( ? ) 13:15:52.500 [http-nio-7002-exec-24] DEBUG c.r.b.m.R.insert - [debug,135] - ==> Parameters: 1559773186(Integer) 13:15:52.500 [http-nio-7002-exec-24] DEBUG c.r.b.m.R.insert - [debug,135] - <== Updates: 1 13:15:53.048 [http-nio-7002-exec-24] INFO o.a.e.i.b.d.BpmnDeployer - [deploy,105] - Processing resource XMBBCSCS_1559773186.bpmn AAP 13:15:53.629 [jimu-Async-Executor-1] INFO o.o.e.b.s.b.ActivitiBpmMsgTemplateServiceImpl - [getContent,535] - content数据转换,activitiBpmMsgVo:{"appPushUrl":"","bpmnCode":"XMBBCSCSLC","emailUrl":"/user/workflow/Upcoming/check/XMBBCSCS/XMBBCSCSLC/XMBBCSCS_1559773186","formCode":"XMBBCSCS","processId":"XMBBCSCS_1559773186","processName":"项目报备测试测试流程","processType":"","taskId":"0d487c61-91f3-11f0-b102-0045e2cb0847","url":"/user/workflow/Upcoming/check/XMBBCSCS/XMBBCSCSLC/XMBBCSCS_1559773186","userId":"1"},msgNoticeType:1 13:15:53.696 [jimu-Async-Executor-1] INFO o.o.e.b.s.b.ActivitiBpmMsgTemplateServiceImpl - [getContent,548] - 转换后数据content:您有1个项目报备测试测试流程XMBBCSCS_1559773186需要处理 13:15:53.698 [jimu-Async-Executor-1] ERROR o.o.e.b.s.b.ActivitiBpmMsgTemplateServiceImpl - [getEmployee,511] - 流程消息查询员工信息失败,消息入参{"appPushUrl":"","bpmnCode":"XMBBCSCSLC","emailUrl":"/user/workflow/Upcoming/check/XMBBCSCS/XMBBCSCSLC/XMBBCSCS_1559773186","formCode":"XMBBCSCS","processId":"XMBBCSCS_1559773186","processName":"项目报备测试测试流程","processType":"","taskId":"0d487c61-91f3-11f0-b102-0045e2cb0847","url":"/user/workflow/Upcoming/check/XMBBCSCS/XMBBCSCSLC/XMBBCSCS_1559773186","userId":"1"} 怎么解决 流程发起失败
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值