gpt4 book ai didi

java - InetAddress 无法解析为类型

转载 作者:行者123 更新时间:2023-12-02 06:22:12 63 4
gpt4 key购买 nike

我使用以下代码来检查所提供的 IP 在 java 中是否可访问。

public String isNetworkReachable(){
int timeout = 2000;
String status = "SUCCESS";
try {
InetAddress address = InetAddress.getByAddress("10.2.240.74".getBytes());

if(!address.isReachable(timeout)){
status = "ERROR";
}

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return status;

}

当我运行代码时,我收到一条错误消息

java.lang.Error: Unresolved compilation problems: 
InetAddress cannot be resolved to a type
InetAddress cannot be resolved
InetAddress cannot be resolved to a type

at com.jkcs.touchpos.sync.SyncUtil.synchronzeManually(SyncUtil.java:83)
at com.jkcs.touchpos.sync.SyncUtil$$FastClassByCGLIB$$6e1c4a07.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.jkcs.touchpos.sync.SyncUtil$$EnhancerByCGLIB$$f7d7791c.synchronzeManually(<generated>)
at com.jkcs.touchpos.application.controller.TouchPosWebServiceController.syncStatus(TouchPosWebServiceController.java:864)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:530)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:426)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:118)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:495)
at org.eclipse.jetty.server.session.SessionHandler.handle(SessionHandler.java:179)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:930)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:361)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:864)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:116)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:243)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:112)
at org.eclipse.jetty.server.Server.handle(Server.java:337)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:552)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:979)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:533)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:203)
at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:399)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:437)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:435)
at java.lang.Thread.run(Thread.java:662)

我在这里做错了什么。请帮忙!

最佳答案

1) 导入java.net.InetAddress

2) 稍微修改函数isNetworkReachable():

public static String isNetworkReachable(){
int timeout = 2000;
String status = "ERROR";
try {
InetAddress address = InetAddress.getByName("10.2.240.74");

if(address.isReachable(timeout)){
status = "SUCCESS";
}

} catch (Exception e) {
e.printStackTrace();
}

return status;

}

关于java - InetAddress 无法解析为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918733/

63 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com