网络通显示,不通不显示(由于java运行在服务端,所以测试的其实是服务端的上网情况)
2009-02-14 15:23
513 查看
package share.tools;
import java.awt.Toolkit;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetworkManagement implements Runnable {
private int htmlCodeSize;
private int sleepMillisecond;
private int sleepMillisecondWhenNetWorkUnLinked;
private boolean isSpontaneousNotice;
private static boolean networkIsLinked;
private Thread thread = new Thread(this);
private Toolkit toolkit;
private String[] urls;
public NetworkManagement() {
this.urls = new String[]{"http://weather.265.com/weather.htm", "http://weather.qq.com/inc/ss258.htm"};
this.htmlCodeSize = 50;
this.sleepMillisecond = 5000;
this.sleepMillisecondWhenNetWorkUnLinked = 10000;
this.toolkit = Toolkit.getDefaultToolkit();
thread.start();
}
public void setURLs(String[] urls) {
if (urls != null && urls.length > 0) {
this.urls = urls;
}
}
public void setHtmlCodeSize(int htmlCodeSize) {
if (htmlCodeSize > 0) {
this.htmlCodeSize = htmlCodeSize;
}
}
public void isSpontaneousNotice(boolean isSpontaneousNotice) {
this.isSpontaneousNotice = isSpontaneousNotice;
}
public void setSleepMillisecont(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecond = sleepMillisecont;
}
}
public void setSleepMillisecondWhenNetWorkUnLinked(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecondWhenNetWorkUnLinked = sleepMillisecont;
}
}
public static boolean IsNetWordLinking() {
return NetworkManagement.networkIsLinked;
}
public void run() {
while (true) {
try {
this.isNetWorkLinked();
if (!NetworkManagement.networkIsLinked) {
this.isPrintMessage(this.isSpontaneousNotice);
Thread.sleep(this.sleepMillisecondWhenNetWorkUnLinked);
}
// System.out.println(NetworkManagement.IsNetWordLinking());
Thread.sleep(this.sleepMillisecond);
} catch (Exception e) {
}
}
}
private boolean canGetHtmlCode(String httpUrl) {
String htmlCode = "";
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
byte[] buffer = new byte[this.htmlCodeSize];
in.read(buffer);
htmlCode = new String(buffer);
} catch (Exception e) {
}
if (htmlCode == null || htmlCode.equals("")) {
return false;
}
return true;
}
private void isNetWorkLinked() {
boolean tempIsNetWorkLinked = false;
for (int urlsCount = 0; urlsCount < this.urls.length; urlsCount++) {
if (this.canGetHtmlCode(this.urls[urlsCount])) {
tempIsNetWorkLinked = true;
break;
}
}
NetworkManagement.networkIsLinked = tempIsNetWorkLinked;
}
private void isPrintMessage(boolean isPrint) {
if (isPrint) {
// toolkit.beep();
StringBuffer message = new StringBuffer();
message.append("------------->");
message.append("�����网络中断, ");
message.append(this.sleepMillisecondWhenNetWorkUnLinked);
message.append(" 毫秒后再次检测!<-------------");
System.out.println(message.toString());
}
}
public static void main(String[] args) {
NetworkManagement n = new NetworkManagement();
n.isSpontaneousNotice(true);
}
}
--------------------------------------
jsp文件:
<th width="459" rowspan="2" scope="col" align="right">
<iframe id=tq1 style="z-index:2;position:relative;margin-right:0px
" src='http://weather.qq.com/inc/ss258.htm' id='ifm2' width='41%' height='90%' marginwidth='0' marginheight='0' hspace='0' vspace='0' frameborder='0' scrolling='NO'></iframe>
<FRAMESET border=0 frameSpacing=0 frameBorder=NO >
<iframe id=tq2 style="z-index:1;position:relative;
margin-right:0px" align="right" src="http://weather.265.com/weather.htm" width="42%" height="100%" frameborder="no" border="1" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</FRAMESET>
</th>
<%
share.tools.NetworkManagement n = new share.tools.NetworkManagement();
n.isSpontaneousNotice(true);
if(n.IsNetWordLinking())
{
%>
<script>
document.all.tq1.src="http://weather.qq.com/inc/ss258.htm";
document.all.tq2.src="http://weather.265.com/weather.htm";
</script>
<%
}else{%>
<script>
document.all.tq1.src="#";
document.all.tq2.src="#";
</script>
<%}%>
import java.awt.Toolkit;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetworkManagement implements Runnable {
private int htmlCodeSize;
private int sleepMillisecond;
private int sleepMillisecondWhenNetWorkUnLinked;
private boolean isSpontaneousNotice;
private static boolean networkIsLinked;
private Thread thread = new Thread(this);
private Toolkit toolkit;
private String[] urls;
public NetworkManagement() {
this.urls = new String[]{"http://weather.265.com/weather.htm", "http://weather.qq.com/inc/ss258.htm"};
this.htmlCodeSize = 50;
this.sleepMillisecond = 5000;
this.sleepMillisecondWhenNetWorkUnLinked = 10000;
this.toolkit = Toolkit.getDefaultToolkit();
thread.start();
}
public void setURLs(String[] urls) {
if (urls != null && urls.length > 0) {
this.urls = urls;
}
}
public void setHtmlCodeSize(int htmlCodeSize) {
if (htmlCodeSize > 0) {
this.htmlCodeSize = htmlCodeSize;
}
}
public void isSpontaneousNotice(boolean isSpontaneousNotice) {
this.isSpontaneousNotice = isSpontaneousNotice;
}
public void setSleepMillisecont(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecond = sleepMillisecont;
}
}
public void setSleepMillisecondWhenNetWorkUnLinked(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecondWhenNetWorkUnLinked = sleepMillisecont;
}
}
public static boolean IsNetWordLinking() {
return NetworkManagement.networkIsLinked;
}
public void run() {
while (true) {
try {
this.isNetWorkLinked();
if (!NetworkManagement.networkIsLinked) {
this.isPrintMessage(this.isSpontaneousNotice);
Thread.sleep(this.sleepMillisecondWhenNetWorkUnLinked);
}
// System.out.println(NetworkManagement.IsNetWordLinking());
Thread.sleep(this.sleepMillisecond);
} catch (Exception e) {
}
}
}
private boolean canGetHtmlCode(String httpUrl) {
String htmlCode = "";
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
byte[] buffer = new byte[this.htmlCodeSize];
in.read(buffer);
htmlCode = new String(buffer);
} catch (Exception e) {
}
if (htmlCode == null || htmlCode.equals("")) {
return false;
}
return true;
}
private void isNetWorkLinked() {
boolean tempIsNetWorkLinked = false;
for (int urlsCount = 0; urlsCount < this.urls.length; urlsCount++) {
if (this.canGetHtmlCode(this.urls[urlsCount])) {
tempIsNetWorkLinked = true;
break;
}
}
NetworkManagement.networkIsLinked = tempIsNetWorkLinked;
}
private void isPrintMessage(boolean isPrint) {
if (isPrint) {
// toolkit.beep();
StringBuffer message = new StringBuffer();
message.append("------------->");
message.append("�����网络中断, ");
message.append(this.sleepMillisecondWhenNetWorkUnLinked);
message.append(" 毫秒后再次检测!<-------------");
System.out.println(message.toString());
}
}
public static void main(String[] args) {
NetworkManagement n = new NetworkManagement();
n.isSpontaneousNotice(true);
}
}
--------------------------------------
jsp文件:
<th width="459" rowspan="2" scope="col" align="right">
<iframe id=tq1 style="z-index:2;position:relative;margin-right:0px
" src='http://weather.qq.com/inc/ss258.htm' id='ifm2' width='41%' height='90%' marginwidth='0' marginheight='0' hspace='0' vspace='0' frameborder='0' scrolling='NO'></iframe>
<FRAMESET border=0 frameSpacing=0 frameBorder=NO >
<iframe id=tq2 style="z-index:1;position:relative;
margin-right:0px" align="right" src="http://weather.265.com/weather.htm" width="42%" height="100%" frameborder="no" border="1" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</FRAMESET>
</th>
<%
share.tools.NetworkManagement n = new share.tools.NetworkManagement();
n.isSpontaneousNotice(true);
if(n.IsNetWordLinking())
{
%>
<script>
document.all.tq1.src="http://weather.qq.com/inc/ss258.htm";
document.all.tq2.src="http://weather.265.com/weather.htm";
</script>
<%
}else{%>
<script>
document.all.tq1.src="#";
document.all.tq2.src="#";
</script>
<%}%>
相关文章推荐
- 一、问题 MyEclipse怎么导出可运行的jar包。 二、测试环境 MyEclipse V8.6 三、操作过程 1、java项目没有导入第三方jar包的情况 这时候打包就比较简单: ①首先在MyE
- JAVA聊天室客户端不显示内容,服务端没问题的原因总算找到了(其实问题不是这个)
- javaweb网页上传图片并显示在页面上,并在服务端存到磁盘(base64编码解码)
- 真正最正确的用BAT运行JAVA不显示DOS窗口(连闪一下都不闪)
- Android网络传输中必用的两个加密算法:MD5 和 RSA (附java完成测试代码)
- jdbc连接数据库的时候,显示出乱码。所以修改my.ini下的编码格式,把utf-8改为gbk,但是保存的时候出现说my.ini拒绝访问的情况。保存不成功,希望大家能够帮我解决,谢谢!
- 【JAVA】【NIO】对系列翻译的总结,一个综合实例的分析,网络服务端接收客户端输入,实时将内容写文件
- 测试显示内存使用情况的JSP脚本
- eclipse 编的java程序运行后显示问号
- java网络编程,服务端关闭后释放端口
- 『黑马程序员』---java--网络编程--客户端和服务端
- Linux下查看Java进程的运行期间堆栈情况
- java测试网络是否连通
- js测试网络连通情况
- 电脑显示链接网络正常但是不能上网,用某些软件会提示系统丢失libeay32.dll
- 运行基准测试hadoop集群中的问题:org.apache.hadoop.ipc.RemoteException: java.io.IOException: File /benchmarks/TestDFSIO/io_data/test_
- 解决两个虚拟机网络适配器都为NAT模式,其中一台无法上网的情况
- java测试程序运行时间
- 尽管关于Java中文问题的讨论已经相当多了,但由于Java的相关技术标准繁多,面向Java的Web服务器、应用服务器以及JDBC数据库驱动等都没有官方的标准,所以Java应用在处理中文时所存在的问
- java如何测试网络连通性