import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Map;
//实现打开浏览器并跳到指定网址的类
public class SignIn {
public static void openURL(String browser,String userName,String passWord) {
String head = "http://000.000.000.000:0000/logincheck.php?UNAME=";
String tail = "&PASSWORD=";
String logUrl = head+userName+tail+passWord;
try {
// 日期格式化
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前时间
Date nowTime = new Date();
String time = df.format(nowTime);
// 判断今天是周几
int weekDay = dayForWeek(time);
if (weekDay != 7) {
// 处理浏览器路径
Map map = System.getenv();
for (Iterator itr = map.keySet().iterator(); itr.hasNext();) {
String value = (String) map.get((String) itr.next());
if (value.contains("firefox.exe")) {
browser = value;
break;
} else if (value.contains("chrome.exe")) {
browser = value;
}
}
int a = 5 ;
int b = 6 ;
int c = 5;
int d = 6;
// 登录
for (int i = 0; i < 5; i++) {//进行五次尝试
if (a != b) {
Process process = Runtime.getRuntime().exec(new String[] { browser, logUrl });
a = process.waitFor();
b = process.exitValue();
}
// 签到
String signInUrlOne = "http://服务器IP:端口/general/attendance/personal/duty/submit.php?REGISTER_TYPE=1&USER_DUTP=1";
String signInUrlTwo = "http://服务器IP:端口/general/attendance/personal/duty/submit.php?REGISTER_TYPE=2&USER_DUTP=1";
String signInUrlThree = "http://服务器IP:端口/general/attendance/personal/duty/submit.php?REGISTER_TYPE=3&USER_DUTP=1";
String signInUrlFour = "http://服务器IP:端口/general/attendance/personal/duty/submit.php?REGISTER_TYPE=4&USER_DUTP=1";
// 判断时间为(早上签到,中午签退,中午签到,下午签退)
if (c != d) {
SimpleDateFormat clock = new SimpleDateFormat("HH:mm");//设置时间格式
String now = clock.format(nowTime);
// 早上签到
String beginTimeOne = "07:00";
String endTimeOne = "08:00";
if (belongCalendar(now, beginTimeOne, endTimeOne)) {
Process one = Runtime.getRuntime().exec(new String[] { browser, signInUrlOne });
System.out.println("早上签到");
c = one.waitFor();
d = one.exitValue();
}
// 中午签退
String beginTimeTwo = "12:10";
String endTimeTwo = "12:25";
if (belongCalendar(now, beginTimeTwo, endTimeTwo)) {
Process two = Runtime.getRuntime().exec(new String[] { browser, signInUrlTwo });
System.out.println("中午签退");
c = two.waitFor();
d = two.exitValue();
}
// 中午签到
String beginTimeThree = "12:30";
String endTimeThree = "13:00";
if (belongCalendar(now, beginTimeThree, endTimeThree)) {
Process three = Runtime.getRuntime().exec(new String[] { browser, signInUrlThree });
System.out.println("中午签到");
c = three.waitFor();
d = three.exitValue();
}
// 下午签退
String beginTimeFour = "17:50";
String endTimeFour = "24:00";
if (belongCalendar(now, beginTimeFour, endTimeFour)) {
Process four = Runtime.getRuntime().exec(new String[] { browser, signInUrlFour });
System.out.println("下午签退");
c = four.waitFor();
d = four.exitValue();
}
}
}
System.out.println("时间:"+time+",签到");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 判断输入的日期是星期几
* @param pTime
* @return
* @throws Throwable
*/
public static int dayForWeek(String time) throws Throwable {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date tmpDate = format.parse(time);
Calendar cal = new GregorianCalendar();
cal.set(tmpDate.getYear(), tmpDate.getMonth(), tmpDate.getDay());
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 判断时间是否在时间段内
* @param nowTime
* @param beginTime
* @param endTime
* @return
*/
public static boolean belongCalendar(String nowTime, String beginTime, String endTime) {
SimpleDateFormat clock = new SimpleDateFormat("HH:mm");//设置时间格式
Calendar date = Calendar.getInstance();
Calendar begin = Calendar.getInstance();
Calendar end = Calendar.getInstance();
try {
Date now = clock.parse(nowTime);
Date begins = clock.parse(beginTime);
Date ends = clock.parse(endTime);
date.setTime(now);
begin.setTime(begins);
end.setTime(ends);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
}
以上就是具体的操作流程,前提是你得知道你们公司的服务器IP以及端口
public class Demo {
public static void main(String[] args) {
// 浏览器地址(注意自己的浏览器安装位置)
String browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
// "D:\\DevelopmentTools\\FireFoxDeveloperEdition\\firefox.exe";// 火狐浏览器
// "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";// 谷歌浏览器
// 登录网址 后面要拼接
//String logUrl = "http://服务器IP:端口/logincheck.php?UNAME=用户名&PASSWORD=密码";
// 这里可以填写多个账户进行登陆
String [] userNameList = {"用户名"};
String [] passWordList = {"密码"};
for (int i = 0 ;i<userNameList.length ;i++ ) {
SignIn.openURL(browser,userNameList[i],passWordList[i]);
}
}
}