当前位置:首页 » 云服务器 » java获取服务器地址

java获取服务器地址

发布时间: 2022-01-11 23:59:38

A. java中服务器端ServerSocket对象怎么获取服务器端地址和端口号,怎么获取远程请求的

ServerSocket s = new ServerSocket(8888);
while (true) {
// 建立连接
Socket socket = s.accept();

/ /getInetAddress()获取远程ip地址,getPort()远程客户端的断后好
"你好,客户端地址信息: " + socket.getInetAddress() + "\t客户端通信端口号: " + socket.getPort()

B. java里面如何获取服务器的ip地址,帮帮忙

获取本机ip地址
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;

C. java 如何把服务器获取的ip地址和主机名写入TXT文件

给你一个方法,自己调一下吧.

host就是主机名

ip就是ip,

filepath就是文件路径

	publicvoidcreateFile(Stringhost,Stringip,Stringfilepath)
{
BufferedWriterbw=null;
try{
bw=newBufferedWriter(newFileWriter(newFile(filepath)));
bw.write("host-name:"+host+",ip:"+ip);
bw.flush();
bw.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

D. JAVA如何获得服务器端重定向后的URL

request.getHeader("REDIRECT_URL");

或试试
那在servlet里边或者action调用request.getRequestURL()就是了。
如果是获得容器内部的请求URI:request.getRequestURI(),两个方法挺象的。

E. 如何通过Java代码获取tomcat服务器的绝对路径

//也许有你想要的,前提是在项目布署运行起来后,在servlet中或controller中运行以下代码即可。
System.out.println(this.getClass().getResource("/"));//结果:file:/E:/work/service/tomcat7.0.65/webapps/mvc_adminlte/WEB-INF/classes/
System.out.println(System.getProperty("catalina.home"));//结果:E:workservice omcat7.0.65
System.getProperty("user.dir");//结果:E:workservice omcat7.0.65in
System.out.println(request.getSession().getServletContext().getRealPath("/"));//结果:E:workservice omcat7.0.65webapps你的项目名称
希望能帮到你!

F. JAVA怎么获取服务器IP

首先IP为一个字符串,例如:
class test{
static void Split(string ip,out string str1)
{
int i=ip.length;
while(i>0)
{
char ch=ip[i-1];
if(ch==':')
break;
i--;
}
str1=ip.Substring(0,i);
}
static void Main()
{
string str1;
Split("192.168.0.255:8080",out str1)
Console.WriteLine("{0}",str1);
}
}
str1中保存的就是你的ip,192.168.0.255

G. java获取服务器文件,怎样用url返回

下面提供二种方法会使用java发送url请求,并获取服务器返回的值

第一种方法:
代码如下:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
HttpClienthttpclient=newDefaultHttpClient();
Propertiesproperties=newProperties();
HttpEntityentity=null;
StringxmlContent="";
try
{

//设置超时时间
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);

//封装需要传递的参数
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvps.add(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvps.add(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//客户端的请求方法类型
HttpPosthttpPost=newHttpPost(urlStr);
httpPost.setEntity(newUrlEncodedFormEntity(nvps,"GBK"));
HttpResponseresponse=httpclient.execute(httpPost);

//获取服务器返回Http的Content-Type的值
tempStr=response.getHeaders("Content-Type")[0].getValue().toString();

//获取服务器返回页面的值
entity=response.getEntity();
xmlContent=EntityUtils.toString(entity);
Stringstrmessage=null;
System.out.println(xmlContent);
System.out.println(response.getHeaders("Content-Type")[0].getValue().toString());
httpPost.abort();

}
catch(SocketTimeoutExceptione)
{
}
catch(Exceptionex)
{
ex.printStackTrace();
}
finally{
httpclient.getConnectionManager().shutdown();
}
第二种方法:

代码如下:


(StringurlStr,Stringparam1,Stringparam2)throwsException{

HttpURLConnectionurl_con=null;
try{
URLurl=newURL(urlStr);
StringBufferbankXmlBuffer=newStringBuffer();
//创建URL连接,提交到数据,获取返回结果
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","directclient");

PrintWriterout=newPrintWriter(newOutputStreamWriter(connection.getOutputStream(),"GBK"));
out.println(param);
out.close();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection
.getInputStream(),"GBK"));

StringinputLine;

while((inputLine=in.readLine())!=null){
bankXmlBuffer.append(inputLine);
}
in.close();
tempStr=bankXmlBuffer.toString();
}
catch(Exceptione)
{
System.out.println("发送GET请求出现异常!"+e);
e.printStackTrace();

}finally{
if(url_con!=null)
url_con.disconnect();
}

returntmpeStr;
}

总结:多练习代码,熟练之后才能更快速的去了解代码的学习的方法。多去获取一些思维方面的书籍可以看看。

H. 在Java中如何得到本地IP和服务器的机器名

import java.net.*; public class catchserverip{ InetAddress ServerIPaddress=null; //取得Server的IP地址 public InetAddress getServerIP() { try { ServerIPaddress=InetAddress.getLocalHost();} catch (UnknownHostException e) {} return (ServerIPaddress);}}就可以了在jsp中写 <jsp:useBean id=catchipbean scope=page class=catchip / String servername=catchipbean.getServerIP().getHostName(); ---------------------------------------------------------------

I. java编程,获取局域网内服务器端的ip地址

socket.connect(new InetSocketAddress(ip, port), timeout)

看有没有抛异常 没异常就是已经连接上了

想获取服务器名称 可以用ARP协议 或者测试连接的时候服务器回应一个名称

package;

importjava.io.IOException;
importjava.net.InetSocketAddress;
importjava.net.Socket;

publicclassClient{

publicstaticvoidmain(String[]args){
/**
*端口号
*/
intport=10000;
/**
*连接延时
*/
inttimeout=300;
System.out.println("ScannerStart...");
Socketsocket;
/**
*扫描
*/
for(inti=1,k=254;i<k;i++){
if((socket=isOnLine("192.168.1."+i,port,timeout))!=null){
System.out.println("Server:"
+socket.getInetAddress().getHostAddress()
+":"+socket.getPort()+"IsWaiting...");
}

/**
*关闭连接
*/
if(socket!=null&&!socket.isClosed()){
try{
socket.close();
}catch(IOExceptione){
socket=null;
}
}
}
System.out.println("Scannerend...");
}

/**
*测试连接服务器,返回连接成功后的Socket
*
*@paramip
*服务器Ip
*@paramport
*服务器端口号
*@paramtimeout
*连接延时
*@return返回连接成功后的Socket
*/
privatestaticSocketisOnLine(Stringip,intport,inttimeout){
Socketsocket=newSocket();
try{
socket.connect(newInetSocketAddress(ip,port),timeout);
}catch(IOExceptione){
returnnull;
}
returnsocket;
}

}

J. java 怎么获取服务器webroot的路径

使用JAVA后台代码取得WEBROOT物理路径,可以有如下两种方式:
1、使用JSP Servlet取得WEB根路径可以用request.getContextPath(),相对路径request.getSession().getServletContext().getRealPath("/"),它们可以使用我们很容易取得根路径。

2、如果使用了spring, 在WEB-INF/web.xml中,创建一个webAppRootKey的param,指定一个值(默认为webapp.root)作为键值,然后通过Listener,或者Filter,或者Servlet执行String webAppRootKey = getServletContext().getRealPath("/"); 并将webAppRootKey对应的webapp.root分别作为Key,Value写到System Properties系统属性中。之后在程序中通过System.getProperty("webapp.root")来获得WebRoot的物理路径。
具体示例代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>csc2.root</param-value>
</context-param>
<listener>
<listener-class>test.ApplicationListener</listener-class>
</listener>
</web-app>

ApplicationListener.java
package test;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
public class ApplicationListener extends ContextLoaderListener {
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
String webAppRootKey = sce.getServletContext().getRealPath("/");
System.setProperty("csc2.root" , webAppRootKey);
String path =System.getProperty("csc2.root");
System.out.println("sssss:::"+path);
}
}

test.java
public class test {
public void remve(){
String path =System.getProperty("csc2.root");
System.out.println("result::::::::"+path);
}

}

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="test.test" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
test t = new test();
t.remve();
%>
<html>
</html>
部署程序发布 启动TOMCAT 运行index.jsp 就可以调用JAVA中全局设置的物理路径了(说明这里的JSP 只是调用了TEST.JAVA 的remove方法,不做其他使用。原理解释,TOMCAT启动和读取WEB.XML 监听方式加载SPRING ApplicationListener继承SPRING ContextLoaderListener加载SPRING顺便吧全局路径赋值给csc2.root 描述,这样之后JAVA 代码中就可以使用System.getProperty("csc2.root")调用全路路径了。

热点内容
wincc图形编译在哪里 发布:2024-09-17 03:58:26 浏览:977
androidubuntu 发布:2024-09-17 03:50:27 浏览:701
识梦源码 发布:2024-09-17 03:50:18 浏览:26
诺基亚密码忘了打什么电话 发布:2024-09-17 03:27:09 浏览:555
树深度优先算法 发布:2024-09-17 03:26:58 浏览:472
跳转页源码 发布:2024-09-17 03:13:05 浏览:543
html文件上传表单 发布:2024-09-17 03:08:02 浏览:785
聊天软件编程 发布:2024-09-17 03:00:07 浏览:726
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207