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);
}
然後再你需要的地方引用這兩個函數就可以了