当前位置:首页 » 编程语言 » java访问外网

java访问外网

发布时间: 2025-06-30 07:44:52

‘壹’ java 从外网连接自己的电脑

对!!就是这样

用你的手机,用流量,在手机浏览器上登录这个网址123.234.211.214:4567

服务端在收到客户端连接后,print一句话。。

这样就能测试对不对啦。。。如果能print出来,说明ip等端口号是对的,,,但是你现在为什么会报错?可能是client需要在外网,不能也在内网,还在同一台机子上

‘贰’ 我开发了一个java web项目 如何部署到外网服务器上 供外部人员访问

首先把tomcat端口和你的IP地址通过路由器映射到外网去,比如说你的服务IP是192.168.1.2 tomcat端口是8080 只要去路由器把192.168.1.2:8080映射就行了。然后通过你的外网ip访问

‘叁’ java怎么获取请求的ip

java获取外网ip地址方法:
public class Main {

public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}

public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果没有配置外网IP则返回它
String netip = null;// 外网IP

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外网IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}

if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}

‘肆’ Java怎样获取当前机器外网IP

java获取本机的外网ip示例:
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* 获取本机外网IP地址
* 思想是访问网站http://checkip.dyndns.org/,得到返回的文本后解析出本机在外网的IP地址
* @author pieryon
*
*/
public class ExternalIpAddressFetcher {
// 外网IP提供者的网址
private String externalIpProviderUrl;

// 本机外网IP地址
private String myExternalIpAddress;

public ExternalIpAddressFetcher(String externalIpProviderUrl) {
this.externalIpProviderUrl = externalIpProviderUrl;

String returnedhtml = fetchExternalIpProviderHTML(externalIpProviderUrl);

parse(returnedhtml);
}

/**
* 从外网提供者处获得包含本机外网地址的字符串
* 从http://checkip.dyndns.org返回的字符串如下
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.147.226.222</body></html>
* @param externalIpProviderUrl
* @return
*/
private String fetchExternalIpProviderHTML(String externalIpProviderUrl) {
// 输入流
InputStream in = null;

// 到外网提供者的Http连接
HttpURLConnection httpConn = null;

try {
// 打开连接
URL url = new URL(externalIpProviderUrl);
httpConn = (HttpURLConnection) url.openConnection();

// 连接设置
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");

// 获取连接的输入流
in = httpConn.getInputStream();
byte[] bytes=new byte[1024];// 此大小可根据实际情况调整

// 读取到数组中
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

// 将字节转化为为UTF-8的字符串
String receivedString=new String(bytes,"UTF-8");

// 返回
return receivedString;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}

// 出现异常则返回空
return null;
}

/**
* 使用正则表达式解析返回的HTML文本,得到本机外网地址
* @param html
*/
private void parse(String html){
Pattern pattern=Pattern.compile("(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})", Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher(html);
while(matcher.find()){
myExternalIpAddress=matcher.group(0);
}
}

/**
* 得到本机外网地址,得不到则为空
* @return
*/
public String getMyExternalIpAddress() {
return myExternalIpAddress;
}

public static void main(String[] args){
ExternalIpAddressFetcher fetcher=new ExternalIpAddressFetcher("http://checkip.dyndns.org/");

System.out.println(fetcher.getMyExternalIpAddress());
}
}

‘伍’ java项目连接外网数据库 特别慢,得等10来分钟才能连上,而我同事的电脑连接就很正常

如果每次都是这样的话,建议你换台电脑,可能是你电脑配置问题,如果配置没问题,那重新装下系统。另外跟你的网速也有关,你最好检查一下你的网络和你电脑是否有什么漏洞需要修复之类,其实最简单就是重新装系统。对了数据软件也可能影响的哦。这几种情况你可以逐一排查

‘陆’ 怎样通过java使用socks代理访问服务器

packagetest;

importjava.io.IOException;
importjava.util.Date;

importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.HttpException;
importorg.apache.commons.httpclient.UsernamePasswordCredentials;
importorg.apache.commons.httpclient.auth.AuthScope;
importorg.apache.commons.httpclient.methods.PostMethod;

publicclasstest
{
publicstaticvoidmain(Stringargs[])
{
HttpClienthc=newHttpClient();

System.out.println("开始时间:"+System.currentTimeMillis());
for(inti=0;i<=100;i++)
{

try
{
//每10秒才会保存一次
Thread.sleep(12000);
}catch(InterruptedExceptione1)
{
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
Dated=newDate();
PostMethodpm=newPostMethod(
"http://www.tongaichina.com/post.asp?type=int&name=click&time="
+d.getTime());
try
{

//这里写代理地址及端口
hc.getHostConfiguration().setProxy("代理地址",端口);

//这里是用户名与密码
=(
"用户名","密码");
hc.getState().setProxyCredentials(AuthScope.ANY,creds);

hc.executeMethod(pm);
System.out.println(pm.getResponseBodyAsString());
}catch(HttpExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println(i);
}
System.out.println("结束时间:"+System.currentTimeMillis());
}
}

热点内容
坦克世界电脑版最新服务器 发布:2025-06-30 15:13:53 浏览:332
dhcp服务器配置linux 发布:2025-06-30 15:12:13 浏览:237
oraclesql周 发布:2025-06-30 15:12:10 浏览:873
jsp中删除数据库数据 发布:2025-06-30 15:12:08 浏览:708
怎么升级安卓444 发布:2025-06-30 14:42:41 浏览:18
怎么配置tomcat的jdk环境 发布:2025-06-30 14:21:35 浏览:543
php开发在哪里学 发布:2025-06-30 14:19:58 浏览:156
图片处理清晰用什么服务器 发布:2025-06-30 14:01:29 浏览:167
怎么配置好的声音 发布:2025-06-30 13:53:33 浏览:423
配置文件为什么配内网 发布:2025-06-30 13:52:19 浏览:369