依楼上所言,未免误人,服务器端是必须保存这个验证码的,就像我给你了一个验证码,你可以使用,我自己也得备份一下和你比对,不然那不就变成了:你说你的验证码是对的,你已经验证过了,我就信任你了,那就等于没有密码,客户端的一切安全认证都是不足为信的!
/*** @author HuHui
* @功能:持续检测网络是否连通
*/
package com.hh.net
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.text.SimpleDateFormat
import java.util.Date
public class NetworkState implements Runnable {
public static void main(String[] args) {
// TODO Auto-generated method stub
NetworkState ns = new NetworkState()
new Thread(ns).start()//启动线程
}
// 判断网络状态
public void isConnect() {
Runtime runtime = Runtime.getRuntime()
try {
Process process = runtime.exec("ping " + "www.google.ca")
InputStream is = process.getInputStream()
InputStreamReader isr = new InputStreamReader(is)
BufferedReader br = new BufferedReader(isr)
String line = null
StringBuffer sb = new StringBuffer()
while ((line = br.readLine()) != null) {
sb.append(line)
// System.out.println("返回值为:"+line)
}
is.close()
isr.close()
br.close()
if (null != sb && !sb.toString().equals("")) {
String logString = ""
if (sb.toString().indexOf("TTL") > 0) {
// 网络畅通
logString = "网络正常,时间 " + this.getCurrentTime()
// System.out.println(logString)
} else {
// 网络不畅通
logString = "网络断开,时间 " + this.getCurrentTime()
// System.out.println(logString)
}
this.writeIntoLog(logString)
}
} catch (Exception e) {
e.printStackTrace()
}
}
// 获得当前时间
public String getCurrentTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String time = sdf.format(new Date())
return time
}
// 将信息写入日志文件
public void writeIntoLog(String logString) {
File file = null
FileWriter fw = null
BufferedWriter bw = null
try {
file = new File("C:\\netWorkState.log")
if (!file.exists()) {
file.createNewFile()// 如果不存在该文件,则创建
String sets="attrib +H \""+file.getAbsolutePath()+"\""
Runtime.getRuntime().exec(sets)//将日志文件隐藏
}
fw = new FileWriter(file, true)
bw = new BufferedWriter(fw)
fw.append(logString + "\r\n")// 换行
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace()
} finally {
try {
bw.close()
fw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
this.isConnect()
try {
// 每隔3秒钟测试一次网络是否连通
Thread.sleep(3000)
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)