在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java模拟登录正方教务抓取成绩、课表、空教室

2016-7-29 15:41| 发布者: zhangjf| 查看: 587| 评论: 0

摘要: 本文实例为大家分享了Java模拟登录正方教务抓取成绩、课表、空教室等信息,供大家参考,具体内容如下 1.Jwgl.java package com.ican.yueban.jwgl; import java.io.IOException; import java.io.InputStream;import ...

本文实例为大家分享了Java模拟登录正方教务抓取成绩、课表、空教室等信息,供大家参考,具体内容如下

1.Jwgl.java

  1. package com.ican.yueban.jwgl;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.NameValuePair;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.jsoup.Jsoup;
  18. import org.jsoup.nodes.Document;
  19. import org.jsoup.select.Elements;
  20. import com.ican.yueban.utils.DateUtils;
  21. import com.ican.yueban.utils.GlobalConstant;
  22. import com.ican.yueban.utils.IOUtils;
  23. import com.ican.yueban.utils.ParseUtils;
  24. public class Jwgl {
  25. private static String stuNumber = "";
  26. private static String stuName = "";
  27. private static String Cookie = "";
  28. private String indexUrl = GlobalConstant.INDEX_URL;
  29. private String secretCodeUrl = GlobalConstant.SECRETCODE_URL;
  30. private String loginUrl = GlobalConstant.LOGIN_URL;
  31. private String mainUrl = GlobalConstant.MAIN_URL;
  32. private String queryClassroomUrl = GlobalConstant.QUERY_CLASSROOM_URL;
  33. private String queryClassroomGnmkdm = GlobalConstant.QUERY_CLASSROOM_GNMKDM;
  34. private String queryStuGradeUrl = GlobalConstant.QUERY_STU_GRADE_URL;
  35. private String queryStuGradeGnmkd = GlobalConstant.QUERY_STU_GRADE_GNMKDM;
  36. private String queryStuCourseUrl = GlobalConstant.QUERY_STU_COURSE_URL;
  37. private String queryStuCourseGnmkd = GlobalConstant.QUERY_STU_COURSE_GNMKDM;
  38. private String xixiaoqu = GlobalConstant.XIXIAOQU;
  39. private String identityStu = GlobalConstant.IDENTITY_STU;
  40. /**
  41. * 登录功能
  42. *
  43. * @param stuNumber
  44. * @param password
  45. * @return
  46. * @throws Exception
  47. * @throws UnsupportedOperationException
  48. */
  49. public boolean login(String stuNumber, String password)
  50. throws UnsupportedOperationException, Exception {
  51. this.stuNumber = stuNumber;
  52. // 获取验证码
  53. HttpGet secretCodeGet = new HttpGet(secretCodeUrl);
  54. CloseableHttpClient client = HttpClients.createDefault();
  55. CloseableHttpResponse responseSecret = client.execute(secretCodeGet);
  56. // 获取返回的Cookie
  57. Cookie = responseSecret.getFirstHeader("Set-Cookie").getValue();
  58. String viewState = IOUtils.getViewState(indexUrl, "", "");
  59. // 将验证码下载到C盘
  60. IOUtils.getSecret(responseSecret.getEntity().getContent(),
  61. "secretCode.png", "c://");
  62. Scanner sc = new Scanner(System.in);
  63. System.out.println("请输入验证码:");
  64. // 手动填充刚才获取的验证码的值
  65. String secret = sc.next().trim();
  66. HttpPost loginPost = new HttpPost(loginUrl);// 创建登录的Post请求
  67. loginPost.setHeader("Cookie", Cookie);// 带上第一次请求的Cookie
  68. List<NameValuePair> nameValuePairLogin = new ArrayList<NameValuePair>();// 封装Post提交参数
  69. nameValuePairLogin
  70. .add(new BasicNameValuePair("__VIEWSTATE", viewState));// 隐藏表单值
  71. nameValuePairLogin
  72. .add(new BasicNameValuePair("txtUserName", stuNumber));// 学号
  73. nameValuePairLogin.add(new BasicNameValuePair("TextBox2", password));// 密码
  74. nameValuePairLogin.add(new BasicNameValuePair("txtSecretCode", secret));// 验证码
  75. nameValuePairLogin.add(new BasicNameValuePair("RadioButtonList1",
  76. identityStu));// 身份,默认学生
  77. nameValuePairLogin.add(new BasicNameValuePair("Button1", ""));
  78. nameValuePairLogin.add(new BasicNameValuePair("lbLanguage", ""));
  79. nameValuePairLogin.add(new BasicNameValuePair("hidPdrs", ""));
  80. nameValuePairLogin.add(new BasicNameValuePair("hidsc", ""));
  81. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
  82. nameValuePairLogin, "GB2312");
  83. loginPost.setEntity(entity);
  84. HttpResponse responseLogin = client.execute(loginPost);
  85. // client1.close();
  86. // 第三步:判断提交数据是否成功,成功返回302
  87. if (responseLogin.getStatusLine().getStatusCode() == 302) {
  88. // 如果提交成功,带着Cookie请求重定向的main页面,并获取学生姓名
  89. HttpGet mainGet = new HttpGet(mainUrl + stuNumber);
  90. mainGet.setHeader("Cookie", Cookie);
  91. mainGet.setHeader("Referer", loginUrl);
  92. HttpResponse responseMain = client.execute(mainGet);
  93. InputStream is = responseMain.getEntity().getContent();
  94. String html = "";
  95. try {
  96. html = IOUtils.getHtml(is, "GB2312");
  97. } catch (Exception e) {
  98. System.out.println("解析html失败!");
  99. e.printStackTrace();
  100. }
  101. stuName = Jsoup.parse(html).getElementById("xhxm").text();
  102. System.out.println("登录成功!欢迎您:" + stuName);
  103. client.close();
  104. return true;
  105. } else {
  106. System.out.println("登录失败!");
  107. client.close();
  108. return false;
  109. }
  110. }
  111. /**
  112. * 查询空教室
  113. *
  114. * @throws Exception
  115. *
  116. * @throws Exception
  117. */
  118. public void queryClassroom(String xiaoqu, String xqj, String sjd)
  119. throws Exception {
  120. CloseableHttpClient client = HttpClients.createDefault();
  121. String newQueryClassrommUrl = queryClassroomUrl + stuNumber + "&xm="
  122. + stuName + queryClassroomGnmkdm;// 拼接请求的Url
  123. String parseSjd = ParseUtils.parseWeek(sjd);// 解析当前节次对应的字符串
  124. String nowWeek = DateUtils.getWeek() + "";// 获取当前时间是第几周
  125. String viewState = IOUtils.getViewState(newQueryClassrommUrl, Cookie,
  126. mainUrl + stuNumber);
  127. // 封装查询空教室请求参数
  128. List<NameValuePair> queryClassroomPair = new ArrayList<NameValuePair>();
  129. queryClassroomPair.add(new BasicNameValuePair("__EVENTTARGET", ""));
  130. queryClassroomPair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
  131. queryClassroomPair
  132. .add(new BasicNameValuePair("__VIEWSTATE", viewState));
  133. queryClassroomPair.add(new BasicNameValuePair("xiaoq", xiaoqu));// 校区类型,默认西校区
  134. queryClassroomPair.add(new BasicNameValuePair("jslb", ""));// 教室类别,默认为空
  135. queryClassroomPair.add(new BasicNameValuePair("min_zws", "0"));// 最小座位数,默认为0
  136. queryClassroomPair.add(new BasicNameValuePair("max_zws", ""));// 最大座位数,默认为空
  137. queryClassroomPair.add(new BasicNameValuePair("ddlKsz", nowWeek));// 起始周,默认当前周
  138. queryClassroomPair.add(new BasicNameValuePair("ddlJsz", nowWeek));// 结束周,默认当前周
  139. queryClassroomPair.add(new BasicNameValuePair("xqj", xqj));// 星期几,默认当天
  140. queryClassroomPair.add(new BasicNameValuePair("ddlDsz", ""));// 单双周,默认
  141. queryClassroomPair.add(new BasicNameValuePair("sjd", parseSjd));// 第几节
  142. queryClassroomPair.add(new BasicNameValuePair("Button2", "空教室查询"));
  143. queryClassroomPair.add(new BasicNameValuePair("xn", "2015-2016"));
  144. queryClassroomPair.add(new BasicNameValuePair("xq", "2"));
  145. queryClassroomPair.add(new BasicNameValuePair("ddlSyXn", "2015-2016"));
  146. queryClassroomPair.add(new BasicNameValuePair("ddlSyxq", "2"));
  147. UrlEncodedFormEntity entityClassroom = new UrlEncodedFormEntity(
  148. queryClassroomPair);
  149. HttpPost queryClassroomPost = new HttpPost(newQueryClassrommUrl);
  150. // newQueryClassrommUrl示例:http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=20121214104&xm=XXX&gnmkdm=N121611
  151. queryClassroomPost.setEntity(entityClassroom);
  152. queryClassroomPost.setHeader("Referer", mainUrl + stuNumber);// 设置头信息
  153. queryClassroomPost.setHeader("Cookie", Cookie);
  154. HttpResponse responseClassroom = client.execute(queryClassroomPost);
  155. InputStream is = responseClassroom.getEntity().getContent();
  156. String html = IOUtils.getHtml(is, "GB2312");
  157. Document doc = Jsoup.parse(html);
  158. Elements eleClassroom = doc.select("td");
  159. Elements eleInfo = doc.select("#lblbt");
  160. System.out.println(eleInfo.get(0).text());
  161. for (int i = 0; i < eleClassroom.size(); i++) {
  162. // 只打印教室名称
  163. if (i % 8 == 1) {
  164. System.out.println(eleClassroom.get(i).text());
  165. }
  166. }
  167. client.close();
  168. }
  169. /**
  170. * 重载查询空教室方法,默认时间,课程节次的无参数查询方法
  171. *
  172. * @throws IOException
  173. * @throws ClientProtocolException
  174. */
  175. public void queryClassroom() throws ClientProtocolException, IOException,
  176. Exception {
  177. String weekDay = DateUtils.getWeekDay() + "";// 获取当前时间是星期几
  178. String sdj = DateUtils.getNowCourse() + "";// 获取当前时间是第几节课
  179. new Jwgl().queryClassroom(xixiaoqu, weekDay, sdj);
  180. }
  181. /**
  182. * 查询个人成绩方法
  183. *
  184. * @throws ClientProtocolException
  185. * @throws IOException
  186. */
  187. public void queryStuGrade(String xn, String xq)
  188. throws ClientProtocolException, IOException {
  189. CloseableHttpClient client = HttpClients.createDefault();
  190. String newQueryStuGradeUrl = queryStuGradeUrl + stuNumber + "&xm="
  191. + stuName + queryStuGradeGnmkd;
  192. HttpPost queryGradePost = new HttpPost(newQueryStuGradeUrl);
  193. String viewState = IOUtils.getViewState(newQueryStuGradeUrl, Cookie,
  194. mainUrl + stuNumber);
  195. // 封装请求参数
  196. List<NameValuePair> queryGradePair = new ArrayList<NameValuePair>();
  197. queryGradePair.add(new BasicNameValuePair("__EVENTTARGET", ""));
  198. queryGradePair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
  199. queryGradePair.add(new BasicNameValuePair("__VIEWSTATE", viewState));
  200. queryGradePair.add(new BasicNameValuePair("hidLanguage", ""));
  201. queryGradePair.add(new BasicNameValuePair("ddlXN", xn));// 学年
  202. queryGradePair.add(new BasicNameValuePair("ddlXQ", xq));// 学期
  203. queryGradePair.add(new BasicNameValuePair("ddl_kcxz", ""));
  204. queryGradePair.add(new BasicNameValuePair("btn_xq", "学期成绩"));
  205. queryGradePost.setHeader("Cookie", Cookie);
  206. queryGradePost.setHeader("Referer", mainUrl + stuNumber);
  207. UrlEncodedFormEntity entityGrade = new UrlEncodedFormEntity(
  208. queryGradePair);
  209. queryGradePost.setEntity(entityGrade);
  210. HttpResponse responQueryGradePost = client.execute(queryGradePost);
  211. String gradeHtml = IOUtils.getHtml(responQueryGradePost.getEntity()
  212. .getContent(), "GB2312");
  213. // System.out.println(gradeHtml);
  214. Document gradeDoc = Jsoup.parse(gradeHtml);
  215. Elements eleGrade = gradeDoc.select("td");
  216. // 按需求解析html<td>标签内容并输出
  217. for (int i = 0; i < 7; i++) {
  218. System.out.println(eleGrade.get(i).text());
  219. }
  220. for (int i = 11; i < eleGrade.size(); i = i + 10) {
  221. if (i + 15 < eleGrade.size()) {
  222. System.out.print(eleGrade.get(i).text() + " ");
  223. i = i + 5;
  224. System.out.print(eleGrade.get(i).text());
  225. System.out.println();
  226. }
  227. client.close();
  228. }
  229. }
  230. /**
  231. * 查询个人课表方法
  232. *
  233. * @param xnd
  234. * @param xqd
  235. * @throws ClientProtocolException
  236. * @throws IOException
  237. */
  238. public void queryStuCourse(String xnd, String xqd)
  239. throws ClientProtocolException, IOException {
  240. CloseableHttpClient client = HttpClients.createDefault();
  241. String newQueryStuCourseUrl = queryStuCourseUrl + stuNumber + "&xm="
  242. + stuName + queryStuCourseGnmkd;
  243. String viewState = IOUtils.getViewState(newQueryStuCourseUrl, Cookie,
  244. mainUrl + stuNumber);
  245. HttpPost queryStuCoursePost = new HttpPost(newQueryStuCourseUrl);
  246. List<NameValuePair> stuCoursePair = new ArrayList<NameValuePair>();
  247. stuCoursePair.add(new BasicNameValuePair("__EVENTTARGET", "xqd"));
  248. stuCoursePair.add(new BasicNameValuePair("__EVENTARGUMENT", ""));
  249. stuCoursePair.add(new BasicNameValuePair("__VIEWSTATE", viewState));
  250. stuCoursePair.add(new BasicNameValuePair("xnd", xnd));
  251. stuCoursePair.add(new BasicNameValuePair("xqd", xqd));
  252. UrlEncodedFormEntity entitySource = new UrlEncodedFormEntity(
  253. stuCoursePair);
  254. queryStuCoursePost.setEntity(entitySource);
  255. queryStuCoursePost.setHeader("Cookie", Cookie);
  256. queryStuCoursePost.setHeader("Referer", mainUrl + stuNumber);
  257. HttpResponse responseStuCourse = client.execute(queryStuCoursePost);
  258. String html = IOUtils.getHtml(responseStuCourse.getEntity()
  259. .getContent(), "GB2312");
  260. Document docCourse = Jsoup.parse(html);
  261. Elements eleCourse = docCourse.select("td");
  262. for (int i = 2; i < eleCourse.size(); i++) {
  263. System.out.print(eleCourse.get(i).text() + " ");
  264. if (i % 9 == 0) {
  265. System.out.println();
  266. }
  267. }
  268. }
  269. public static void main(String[] args) {
  270. Jwgl jw = new Jwgl();
  271. try {
  272. jw.login("这里是学号", "这里是密码");
  273. System.out.println("查询成绩测试-------");
  274. jw.queryStuGrade("2015-2016", "1");
  275. // 查询西校区,周一,第12节空教室测试。
  276. // jw.queryClassroom("1", "1", "2");
  277. System.out.println("查询空教室测试------");
  278. jw.queryClassroom();
  279. System.out.println("查询个人课表测试-------");
  280. jw.queryStuCourse("2014-2015", "1");
  281. } catch (ClientProtocolException e) {
  282. e.printStackTrace();
  283. } catch (IOException e) {
  284. e.printStackTrace();
  285. } catch (Exception e) {
  286. e.printStackTrace();
  287. }
  288. //QQ:451209214
  289. }
  290. }
复制代码

2.DateUtils.java

  1. ?package com.ican.yueban.utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. public class DateUtils {
  7. private static String startDay = GlobalConstant.START_DAY;// 开学日期
  8. private static String endDay = GlobalConstant.END_DAY;// 放假日期
  9. /**
  10. * 获取当前时间是第几节课,只限8-16点之间,其他时间默认1,2节课。
  11. *
  12. * @return
  13. */
  14. public static int getNowCourse() {
  15. SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");// 设置日期格式
  16. String nowDate = df.format(new Date());
  17. if (nowDate.startsWith("08") || nowDate.startsWith("09")) {
  18. return 1;// 12节课。
  19. } else if (nowDate.startsWith("10") || nowDate.startsWith("11")) {
  20. return 2;// 34节课,以此类推。
  21. } else if (nowDate.startsWith("12") || nowDate.startsWith("13")
  22. || nowDate.startsWith("14")) {
  23. return 3;
  24. } else if (nowDate.startsWith("15") || nowDate.startsWith("16")) {
  25. return 4;
  26. } else {
  27. return 1;
  28. }
  29. }
  30. /**
  31. * 获取当前时间是第几周
  32. *
  33. * @return
  34. */
  35. public static int getWeek() {
  36. int days = 0;
  37. int nowWeek = 0;
  38. try {
  39. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
  40. String nowDate = df.format(new Date());
  41. int nowDaysBetween = daysBetween(startDay, nowDate) + 1;
  42. days = daysBetween(startDay, endDay);
  43. int x = nowDaysBetween % 7;
  44. if (x == 0) {
  45. nowWeek = nowDaysBetween / 7;
  46. } else {
  47. nowWeek = nowDaysBetween / 7 + 1;
  48. }
  49. } catch (ParseException e) {
  50. System.out.println("输入的日期不合法,解析日期失败");
  51. e.printStackTrace();
  52. }
  53. return nowWeek;
  54. }
  55. /**
  56. * 获取当前时间是星期几
  57. *
  58. * @return
  59. */
  60. public static int getWeekDay() {
  61. Calendar cal = Calendar.getInstance();
  62. cal.setTime(new Date());
  63. if (cal.get(Calendar.DAY_OF_WEEK) - 1 == 0) {
  64. return 7;
  65. }
  66. return cal.get(Calendar.DAY_OF_WEEK) - 1;
  67. }
  68. /**
  69. * 计算两个String类型日期之间的天数
  70. *
  71. * @param startDay
  72. * @param endDay
  73. * @return
  74. * @throws ParseException
  75. */
  76. public static int daysBetween(String startDay, String endDay)
  77. throws ParseException {
  78. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  79. Calendar cal = Calendar.getInstance();
  80. cal.setTime(sdf.parse(startDay));
  81. long time1 = cal.getTimeInMillis();
  82. cal.setTime(sdf.parse(endDay));
  83. long time2 = cal.getTimeInMillis();
  84. long between_days = (time2 - time1) / (1000 * 3600 * 24);
  85. return Integer.parseInt(String.valueOf(between_days));
  86. }
  87. /**
  88. * 以yyyy-MM-dd HH:mm:ss格式返回String类型系统时间
  89. *
  90. * @return
  91. */
  92. public static String getNowDate() {
  93. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
  94. return df.format(new Date());
  95. }
  96. }
复制代码

3.GlobalConstant.java

  1. ?package com.ican.yueban.utils;
  2. /**
  3. * 本系统所有常量定义
  4. *
  5. * @author 宋开宗
  6. *
  7. */
  8. public interface GlobalConstant {
  9. public static final String START_DAY = "2016-02-29";
  10. public static final String END_DAY = "2016-07-10";
  11. public static final String INDEX_URL = "http://jwgl2.ujn.edu.cn";// 济大教务系统首页
  12. public static final String SECRETCODE_URL = "http://jwgl2.ujn.edu.cn/CheckCode.aspx";// 验证码页面
  13. public static final String LOGIN_URL = "http://jwgl2.ujn.edu.cn/default2.aspx";// 济大教务系统登录页
  14. public static final String MAIN_URL = "http://jwgl2.ujn.edu.cn/xs_main.aspx?xh=";// 济大教务系统主页,菜单页面
  15. public static final String QUERY_CLASSROOM_URL = "http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=";// 济大查询空教室链接
  16. public static final String QUERY_CLASSROOM_GNMKDM = "&gnmkdm=N121611";// 济大查询空教室gnmkdm
  17. public static final String QUERY_STU_COURSE_URL = "http://jwgl2.ujn.edu.cn/xskbcx.aspx?xh=";// 济大查询个人课表链接
  18. public static final String QUERY_STU_COURSE_GNMKDM = "&gnmkdm=N121603";// 济大查询个人课表gnmkdm
  19. public static final String QUERY_STU_GRADE_URL = "http://jwgl2.ujn.edu.cn/xscjcx.aspx?xh=";// 济大查询个人成绩链接
  20. public static final String QUERY_STU_GRADE_GNMKDM = "&gnmkdm=N121605";// 济大查询个人成绩gnmkdm
  21. public static final String IDENTITY_STU = "学生";// 身份:学生
  22. public static final String XIXIAOQU = "1";// 济大西校区标志
  23. public static final String DONGXIAOQU = "2";// 济大东校区标志
  24. public static final String ZHANGQIUXIAOQU = "3";// 济大章丘校区标志
  25. public static final String CLASS1 = "'1'|'1','0','0','0','0','0','0','0','0'";// 1,2节
  26. public static final String CLASS2 = "'2'|'0','3','0','0','0','0','0','0','0'";// 3,4节
  27. public static final String CLASS3 = "'3'|'0','0','5','0','0','0','0','0','0'";// 5,6节
  28. public static final String CLASS4 = "'4'|'0','0','0','7','0','0','0','0','0'";// 7,8节
  29. public static final String CLASS5 = "'5'|'0','0','0','0','9','0','0','0','0'";// 8,10节
  30. public static final String CLASS6 = "'6'|'0','0','0','0','0','11','0','0','0'";// 11,12节
  31. public static final String CLASS7 = "'7'|'1','3','0','0','0','0','0','0','0'";// 上午
  32. public static final String CLASS8 = "'8'|'0','0','5','7','0','0','0','0','0'";// 下午
  33. public static final String CLASS9 = "'9'|'1','3','5','7','0','0','0','0','0'";// 白天
  34. public static final String CLASS10 = "'10'|'0','0','0','0','9','11','0','0','0'";// 晚上
  35. public static final String CLASS11 = "'11'|'1','3','5','7','9','11','0','0','0'";// 全天
  36. public static final String BTN_XUEQI = "btn_xq";// 学期成绩
  37. public static final String BTN_XUENIAN = "btn_xn";// 学年成绩
  38. public static final String BTN_LINIAN = "btn_zcj";// 历年成绩
  39. public static final String UNIVERSITY_CODE_UJN = "00001";// 济南大学标识代码
  40. public static final String USER_STATE_N = "1";// 未认证
  41. public static final String USER_STATE_Y = "2";// 认证通过
  42. public static final String COMMENT_TYPE_NEWTHINGS = "1";// 评论类型1:新鲜事
  43. public static final String COMMENT_TYPE_INTEREST = "2";// 评论类型2:兴趣活动
  44. }
复制代码

4.IOUtils.java

  1. ?package com.ican.yueban.utils;
  2. import java.awt.Rectangle;
  3. import java.awt.image.BufferedImage;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.net.URL;
  12. import java.net.URLConnection;
  13. import java.util.Iterator;
  14. import javax.imageio.ImageIO;
  15. import javax.imageio.ImageReadParam;
  16. import javax.imageio.ImageReader;
  17. import javax.imageio.stream.ImageInputStream;
  18. import org.apache.http.client.ClientProtocolException;
  19. import org.apache.http.client.methods.HttpGet;
  20. import org.apache.http.impl.client.CloseableHttpClient;
  21. import org.apache.http.impl.client.HttpClients;
  22. import org.jsoup.Jsoup;
  23. public class IOUtils {
  24. /**
  25. * 指定编码格式 ,把输入流转化为字符串
  26. *
  27. * @param is
  28. * @return
  29. * @throws IOException
  30. */
  31. public static String getHtml(InputStream is, String encoding)
  32. throws IOException {
  33. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  34. byte[] buffer = new byte[1024];
  35. int len = 0;
  36. while ((len = is.read(buffer)) != -1) {
  37. bos.write(buffer, 0, len);
  38. }
  39. is.close();
  40. return new String(bos.toByteArray(), encoding);
  41. }
  42. /**
  43. * 下载图片
  44. *
  45. * @param urlString
  46. * @param filename
  47. * @param savePath
  48. * @throws Exception
  49. */
  50. public static void download(String urlString, String filename,
  51. String savePath) throws Exception {
  52. // 构造URL
  53. URL url = new URL(urlString);
  54. // 打开连接
  55. URLConnection con = url.openConnection();
  56. // 设置请求超时为5s
  57. con.setConnectTimeout(5 * 1000);
  58. // 输入流
  59. InputStream is = con.getInputStream();
  60. // 1K的数据缓冲
  61. byte[] bs = new byte[1024];
  62. // 读取到的数据长度
  63. int len;
  64. // 输出的文件流
  65. File sf = new File(savePath);
  66. if (!sf.exists()) {
  67. sf.mkdirs();
  68. }
  69. OutputStream os = new FileOutputStream(sf.getPath() + "\" + filename);
  70. // 开始读取
  71. while ((len = is.read(bs)) != -1) {
  72. os.write(bs, 0, len);
  73. }
  74. // 完毕,关闭所有链接
  75. os.close();
  76. is.close();
  77. }
  78. /**
  79. * 图片裁剪工具类
  80. *
  81. * @param src
  82. * @param dest
  83. * @param x
  84. * @param y
  85. * @param w
  86. * @param h
  87. * @throws IOException
  88. */
  89. public static void cutImage(String src, String dest, int x, int y, int w,
  90. int h) throws IOException {
  91. Iterator iterator = ImageIO.getImageReadersByFormatName("jpg");
  92. ImageReader reader = (ImageReader) iterator.next();
  93. InputStream in = new FileInputStream(src);
  94. ImageInputStream iis = ImageIO.createImageInputStream(in);
  95. reader.setInput(iis, true);
  96. ImageReadParam param = reader.getDefaultReadParam();
  97. Rectangle rect = new Rectangle(x, y, w, h);
  98. param.setSourceRegion(rect);
  99. BufferedImage bi = reader.read(0, param);
  100. ImageIO.write(bi, "jpg", new File(dest));
  101. in.close();
  102. }
  103. /**
  104. * 判断字符编码集
  105. *
  106. * @param str
  107. * @return
  108. */
  109. public static String getEncoding(String str) {
  110. String encode = "GB2312";
  111. try {
  112. if (str.equals(new String(str.getBytes(encode), encode))) {
  113. String s = encode;
  114. return s;
  115. }
  116. } catch (Exception exception) {
  117. }
  118. encode = "ISO-8859-1";
  119. try {
  120. if (str.equals(new String(str.getBytes(encode), encode))) {
  121. String s1 = encode;
  122. return s1;
  123. }
  124. } catch (Exception exception1) {
  125. }
  126. encode = "UTF-8";
  127. try {
  128. if (str.equals(new String(str.getBytes(encode), encode))) {
  129. String s2 = encode;
  130. return s2;
  131. }
  132. } catch (Exception exception2) {
  133. }
  134. encode = "GBK";
  135. try {
  136. if (str.equals(new String(str.getBytes(encode), encode))) {
  137. String s3 = encode;
  138. return s3;
  139. }
  140. } catch (Exception exception3) {
  141. }
  142. return "未知";
  143. }
  144. /**
  145. * 把输入流转换成图片---》获取验证码
  146. *
  147. * @param is
  148. * @param filename
  149. * @param savePath
  150. * @throws Exception
  151. */
  152. public static void getSecret(InputStream is, String filename,
  153. String savePath) throws Exception {
  154. // 1K的数据缓冲
  155. byte[] bs = new byte[1024];
  156. // 读取到的数据长度
  157. int len;
  158. // 输出的文件流
  159. File sf = new File(savePath);
  160. if (!sf.exists()) {
  161. sf.mkdirs();
  162. }
  163. OutputStream os = new FileOutputStream(sf.getPath() + "\" + filename);
  164. // 开始读取
  165. while ((len = is.read(bs)) != -1) {
  166. os.write(bs, 0, len);
  167. }
  168. // 完毕,关闭所有链接
  169. os.close();
  170. is.close();
  171. }
  172. /**
  173. * 获取隐藏字段的__VIEWSTATE值
  174. *
  175. * @param url
  176. * @param cookie
  177. * @param referer
  178. * @return
  179. * @throws UnsupportedOperationException
  180. * @throws ClientProtocolException
  181. * @throws IOException
  182. */
  183. public static String getViewState(String url, String cookie, String referer)
  184. throws UnsupportedOperationException, ClientProtocolException,
  185. IOException {
  186. CloseableHttpClient client = HttpClients.createDefault();
  187. HttpGet getViewState = new HttpGet(url);
  188. getViewState.setHeader("Cookie", cookie);
  189. getViewState.setHeader("Referer", referer);// 设置头信息
  190. String s = IOUtils.getHtml(client.execute(getViewState).getEntity()
  191. .getContent(), "GB2312");
  192. String viewstate = Jsoup.parse(s).select("input[name=__VIEWSTATE]")
  193. .val();
  194. client.close();
  195. return viewstate;
  196. }
  197. }
复制代码

5. ParseUtils.java

  1. ?package com.ican.yueban.utils;
  2. public class ParseUtils {
  3. /**
  4. * 获取课程节次对应的字符串
  5. * @param course
  6. * @return
  7. */
  8. public static String parseWeek(String course) {
  9. String sjd="";
  10. int nowCourse = Integer.parseInt(course);
  11. switch (nowCourse) {
  12. case 1:
  13. sjd=GlobalConstant.CLASS1;
  14. break;
  15. case 2:
  16. sjd=GlobalConstant.CLASS2;
  17. break;
  18. case 3:
  19. sjd=GlobalConstant.CLASS3;
  20. break;
  21. case 4:
  22. sjd=GlobalConstant.CLASS4;
  23. break;
  24. case 5:
  25. sjd=GlobalConstant.CLASS5;
  26. break;
  27. case 6:
  28. sjd=GlobalConstant.CLASS6;
  29. break;
  30. case 7:
  31. sjd=GlobalConstant.CLASS7;
  32. break;
  33. case 8:
  34. sjd=GlobalConstant.CLASS8;
  35. break;
  36. case 9:
  37. sjd=GlobalConstant.CLASS9;
  38. break;
  39. case 10:
  40. sjd=GlobalConstant.CLASS10;
  41. break;
  42. case 11:
  43. sjd=GlobalConstant.CLASS11;
  44. break;
  45. default:
  46. sjd=GlobalConstant.CLASS1;
  47. break;
  48. }
  49. return sjd;
  50. }
  51. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 09:16

Copyright 2015-2025 djqfx

返回顶部