1、java编程,获取局域网内服务器端的ip地址
socket.connect(new InetSocketAddress(ip, port), timeout)
看有没有抛异常 没异常就是已经连接上了
想获取服务器名称 可以用ARP协议 或者测试连接的时候服务器回应一个名称
2、java 读取服务器上的文件
http的话就用httpclient。open后,可以返回一个InputStream。这个就是你要读到文件流。
原理的话,参考你用浏览器打开这个链接显示的内容。
这个返回的是一个HTML网页,需要你解析出里面的文字(一般来说取body中间的内容就行)
其实对于这种文件一般用FTP来下载的。楼上写的那个不对,哈哈。
需要的话自己最好去查一下,怎么用,我有代码,不过告诉你的话也不太好?
URL url = new URL("http://你的地址");
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312"));
下面就是解析这个字符串来,自己来吧
3、java获取远程服务器的系统时间
这个要看用什么协议了,如果是http协议,数据包的头部是有服务器当前时间的。
如果用UDP或其他的可以自己定制一个包头,把服务器时间传回。
4、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")调用全路路径了。
5、java获得当前服务器的操作系统是什么?怎么获得
import java.util.Properties;
public class Test{
public static void main (String args[]){
Properties props=System.getProperties(); //系统属性
System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
System.out.println("Java的安装路径:"+props.getProperty("java.home"));
System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
System.out.println("操作系统的名称:"+props.getProperty("os.name"));
System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
System.out.println("操作系统的版本:"+props.getProperty("os.version"));
System.out.println("文件分隔符:"+props.getProperty("file.separator")); //在 unix 系统中是”/”
System.out.println("路径分隔符:"+props.getProperty("path.separator")); //在 unix 系统中是”:”
System.out.println("行分隔符:"+props.getProperty("line.separator")); //在 unix 系统中是”/n”
System.out.println("用户的账户名称:"+props.getProperty("user.name"));
System.out.println("用户的主目录:"+props.getProperty("user.home"));
System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
}
}
6、java获取服务器文件,怎样用url返回
第一种复; response.setStatus(302);
response.setHeader("location", "/dayX/MyHtml.html"); 该方式制可以重定向到服务器指定页面
当然还有以下方式:
第二种;请求转发
请求转发是指将请求再转发到另一资源(一般为JSP或Servlet)。此过程依然在同一个请求范围内,转发后浏览器地址栏内容不变
请求转发使用RequestDispatcher接口中的forward()方法来实现,该方法可以把请求转发到另外一个资源,并让该资源对浏览器的请求进行响应request.getRequestDispatcher(path) .forward(request,response);
第三种 重定向
重定向是指页面重新定位到某个新地址,之前的请求失效,进入一个新的请求,且跳转后浏览器地址栏内容将变为新的指定地址
重定向是通过HttpServletResponse对象的sendRedirect()来实现,该方法相当于浏览器重新发送一个请求
response.sendRedirect(path);
7、Java中服务器端ServerSocket对象怎么获取服务器端地址和端口号??,怎么获取远程请求的
ServerSocket s = new ServerSocket(8888);
while (true) {
// 建立连接
Socket socket = s.accept();
/ /getInetAddress()获取远程ip地址,getPort()远程客户端的断后好
"你好,客户端地址信息: " + socket.getInetAddress() + "\t客户端通信端口号: " + socket.getPort()
8、java 如何获取服务器当前时间
Date dateAndTime = new Date() //Java获取服务器当前日期和时间
System.out.println(dateAndTime .toString());
9、java 获取服务器的时间,年月日时分秒
您这还挺神奇的。如果用java后台获取到时间,然后传到jsp页面。
还得动态不停的走,这传输的多频繁啊?我确实没见过
建议你找找javascript的代码。有很多的。
给出一种
function getCustomTime()
{
var nowtime=new Date();
var hours=nowtime.getHours();
hours=hours>9?hours:"0"+hours;
var minutes=nowtime.getMinutes();
minutes=minutes>9?minutes:"0"+minutes;
var disptime=hours+":"+minutes;
document.getElementById("hourminutes").innerHTML=disptime;
setTimeout("getCustomTime()",1000);
}
function getCustomMonth(){
time=new Date();
year=time.getYear();
month=time.getMonth()+1;
month=month>9?month:"0"+month;
day=time.getDate();
day=day>9?day:"0"+day;
var disptime=year+"/"+month+"/"+day+'星期'+'日一二三四五六'.charAt(time.getDay());
document.getElementById("xq").innerHTML=disptime;
setTimeout("getCustomMonth()",1000);
}
然后再你需要的地方引用这两个函数就可以了