本文實例為大家分享了Java模擬登錄正方教務抓取成績、課表、空教室等信息,供大家參考,具體內容如下
1.Jwgl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
package com.ican.yueban.jwgl; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import com.ican.yueban.utils.DateUtils; import com.ican.yueban.utils.GlobalConstant; import com.ican.yueban.utils.IOUtils; import com.ican.yueban.utils.ParseUtils; public class Jwgl { private static String stuNumber = "" ; private static String stuName = "" ; private static String Cookie = "" ; private String indexUrl = GlobalConstant.INDEX_URL; private String secretCodeUrl = GlobalConstant.SECRETCODE_URL; private String loginUrl = GlobalConstant.LOGIN_URL; private String mainUrl = GlobalConstant.MAIN_URL; private String queryClassroomUrl = GlobalConstant.QUERY_CLASSROOM_URL; private String queryClassroomGnmkdm = GlobalConstant.QUERY_CLASSROOM_GNMKDM; private String queryStuGradeUrl = GlobalConstant.QUERY_STU_GRADE_URL; private String queryStuGradeGnmkd = GlobalConstant.QUERY_STU_GRADE_GNMKDM; private String queryStuCourseUrl = GlobalConstant.QUERY_STU_COURSE_URL; private String queryStuCourseGnmkd = GlobalConstant.QUERY_STU_COURSE_GNMKDM; private String xixiaoqu = GlobalConstant.XIXIAOQU; private String identityStu = GlobalConstant.IDENTITY_STU; /** * 登錄功能 * * @param stuNumber * @param password * @return * @throws Exception * @throws UnsupportedOperationException */ public boolean login(String stuNumber, String password) throws UnsupportedOperationException, Exception { this .stuNumber = stuNumber; // 獲取驗證碼 HttpGet secretCodeGet = new HttpGet(secretCodeUrl); CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse responseSecret = client.execute(secretCodeGet); // 獲取返回的Cookie Cookie = responseSecret.getFirstHeader( "Set-Cookie" ).getValue(); String viewState = IOUtils.getViewState(indexUrl, "" , "" ); // 將驗證碼下載到C盤 IOUtils.getSecret(responseSecret.getEntity().getContent(), "secretCode.png" , "c://" ); Scanner sc = new Scanner(System.in); System.out.println( "請輸入驗證碼:" ); // 手動填充剛才獲取的驗證碼的值 String secret = sc.next().trim(); HttpPost loginPost = new HttpPost(loginUrl); // 創建登錄的Post請求 loginPost.setHeader( "Cookie" , Cookie); // 帶上第一次請求的Cookie List<NameValuePair> nameValuePairLogin = new ArrayList<NameValuePair>(); // 封裝Post提交參數 nameValuePairLogin .add( new BasicNameValuePair( "__VIEWSTATE" , viewState)); // 隱藏表單值 nameValuePairLogin .add( new BasicNameValuePair( "txtUserName" , stuNumber)); // 學號 nameValuePairLogin.add( new BasicNameValuePair( "TextBox2" , password)); // 密碼 nameValuePairLogin.add( new BasicNameValuePair( "txtSecretCode" , secret)); // 驗證碼 nameValuePairLogin.add( new BasicNameValuePair( "RadioButtonList1" , identityStu)); // 身份,默認學生 nameValuePairLogin.add( new BasicNameValuePair( "Button1" , "" )); nameValuePairLogin.add( new BasicNameValuePair( "lbLanguage" , "" )); nameValuePairLogin.add( new BasicNameValuePair( "hidPdrs" , "" )); nameValuePairLogin.add( new BasicNameValuePair( "hidsc" , "" )); UrlEncodedFormEntity entity = new UrlEncodedFormEntity( nameValuePairLogin, "GB2312" ); loginPost.setEntity(entity); HttpResponse responseLogin = client.execute(loginPost); // client1.close(); // 第三步:判斷提交數據是否成功,成功返回302 if (responseLogin.getStatusLine().getStatusCode() == 302 ) { // 如果提交成功,帶著Cookie請求重定向的main頁面,并獲取學生姓名 HttpGet mainGet = new HttpGet(mainUrl + stuNumber); mainGet.setHeader( "Cookie" , Cookie); mainGet.setHeader( "Referer" , loginUrl); HttpResponse responseMain = client.execute(mainGet); InputStream is = responseMain.getEntity().getContent(); String html = "" ; try { html = IOUtils.getHtml(is, "GB2312" ); } catch (Exception e) { System.out.println( "解析html失敗!" ); e.printStackTrace(); } stuName = Jsoup.parse(html).getElementById( "xhxm" ).text(); System.out.println( "登錄成功!歡迎您:" + stuName); client.close(); return true ; } else { System.out.println( "登錄失敗!" ); client.close(); return false ; } } /** * 查詢空教室 * * @throws Exception * * @throws Exception */ public void queryClassroom(String xiaoqu, String xqj, String sjd) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); String newQueryClassrommUrl = queryClassroomUrl + stuNumber + "&xm=" + stuName + queryClassroomGnmkdm; // 拼接請求的Url String parseSjd = ParseUtils.parseWeek(sjd); // 解析當前節次對應的字符串 String nowWeek = DateUtils.getWeek() + "" ; // 獲取當前時間是第幾周 String viewState = IOUtils.getViewState(newQueryClassrommUrl, Cookie, mainUrl + stuNumber); // 封裝查詢空教室請求參數 List<NameValuePair> queryClassroomPair = new ArrayList<NameValuePair>(); queryClassroomPair.add( new BasicNameValuePair( "__EVENTTARGET" , "" )); queryClassroomPair.add( new BasicNameValuePair( "__EVENTARGUMENT" , "" )); queryClassroomPair .add( new BasicNameValuePair( "__VIEWSTATE" , viewState)); queryClassroomPair.add( new BasicNameValuePair( "xiaoq" , xiaoqu)); // 校區類型,默認西校區 queryClassroomPair.add( new BasicNameValuePair( "jslb" , "" )); // 教室類別,默認為空 queryClassroomPair.add( new BasicNameValuePair( "min_zws" , "0" )); // 最小座位數,默認為0 queryClassroomPair.add( new BasicNameValuePair( "max_zws" , "" )); // 最大座位數,默認為空 queryClassroomPair.add( new BasicNameValuePair( "ddlKsz" , nowWeek)); // 起始周,默認當前周 queryClassroomPair.add( new BasicNameValuePair( "ddlJsz" , nowWeek)); // 結束周,默認當前周 queryClassroomPair.add( new BasicNameValuePair( "xqj" , xqj)); // 星期幾,默認當天 queryClassroomPair.add( new BasicNameValuePair( "ddlDsz" , "" )); // 單雙周,默認 queryClassroomPair.add( new BasicNameValuePair( "sjd" , parseSjd)); // 第幾節 queryClassroomPair.add( new BasicNameValuePair( "Button2" , "空教室查詢" )); queryClassroomPair.add( new BasicNameValuePair( "xn" , "2015-2016" )); queryClassroomPair.add( new BasicNameValuePair( "xq" , "2" )); queryClassroomPair.add( new BasicNameValuePair( "ddlSyXn" , "2015-2016" )); queryClassroomPair.add( new BasicNameValuePair( "ddlSyxq" , "2" )); UrlEncodedFormEntity entityClassroom = new UrlEncodedFormEntity( queryClassroomPair); HttpPost queryClassroomPost = new HttpPost(newQueryClassrommUrl); // newQueryClassrommUrl示例:http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=20121214104&xm=XXX&gnmkdm=N121611 queryClassroomPost.setEntity(entityClassroom); queryClassroomPost.setHeader( "Referer" , mainUrl + stuNumber); // 設置頭信息 queryClassroomPost.setHeader( "Cookie" , Cookie); HttpResponse responseClassroom = client.execute(queryClassroomPost); InputStream is = responseClassroom.getEntity().getContent(); String html = IOUtils.getHtml(is, "GB2312" ); Document doc = Jsoup.parse(html); Elements eleClassroom = doc.select( "td" ); Elements eleInfo = doc.select( "#lblbt" ); System.out.println(eleInfo.get( 0 ).text()); for ( int i = 0 ; i < eleClassroom.size(); i++) { // 只打印教室名稱 if (i % 8 == 1 ) { System.out.println(eleClassroom.get(i).text()); } } client.close(); } /** * 重載查詢空教室方法,默認時間,課程節次的無參數查詢方法 * * @throws IOException * @throws ClientProtocolException */ public void queryClassroom() throws ClientProtocolException, IOException, Exception { String weekDay = DateUtils.getWeekDay() + "" ; // 獲取當前時間是星期幾 String sdj = DateUtils.getNowCourse() + "" ; // 獲取當前時間是第幾節課 new Jwgl().queryClassroom(xixiaoqu, weekDay, sdj); } /** * 查詢個人成績方法 * * @throws ClientProtocolException * @throws IOException */ public void queryStuGrade(String xn, String xq) throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); String newQueryStuGradeUrl = queryStuGradeUrl + stuNumber + "&xm=" + stuName + queryStuGradeGnmkd; HttpPost queryGradePost = new HttpPost(newQueryStuGradeUrl); String viewState = IOUtils.getViewState(newQueryStuGradeUrl, Cookie, mainUrl + stuNumber); // 封裝請求參數 List<NameValuePair> queryGradePair = new ArrayList<NameValuePair>(); queryGradePair.add( new BasicNameValuePair( "__EVENTTARGET" , "" )); queryGradePair.add( new BasicNameValuePair( "__EVENTARGUMENT" , "" )); queryGradePair.add( new BasicNameValuePair( "__VIEWSTATE" , viewState)); queryGradePair.add( new BasicNameValuePair( "hidLanguage" , "" )); queryGradePair.add( new BasicNameValuePair( "ddlXN" , xn)); // 學年 queryGradePair.add( new BasicNameValuePair( "ddlXQ" , xq)); // 學期 queryGradePair.add( new BasicNameValuePair( "ddl_kcxz" , "" )); queryGradePair.add( new BasicNameValuePair( "btn_xq" , "學期成績" )); queryGradePost.setHeader( "Cookie" , Cookie); queryGradePost.setHeader( "Referer" , mainUrl + stuNumber); UrlEncodedFormEntity entityGrade = new UrlEncodedFormEntity( queryGradePair); queryGradePost.setEntity(entityGrade); HttpResponse responQueryGradePost = client.execute(queryGradePost); String gradeHtml = IOUtils.getHtml(responQueryGradePost.getEntity() .getContent(), "GB2312" ); // System.out.println(gradeHtml); Document gradeDoc = Jsoup.parse(gradeHtml); Elements eleGrade = gradeDoc.select( "td" ); // 按需求解析html<td>標簽內容并輸出 for ( int i = 0 ; i < 7 ; i++) { System.out.println(eleGrade.get(i).text()); } for ( int i = 11 ; i < eleGrade.size(); i = i + 10 ) { if (i + 15 < eleGrade.size()) { System.out.print(eleGrade.get(i).text() + " " ); i = i + 5 ; System.out.print(eleGrade.get(i).text()); System.out.println(); } client.close(); } } /** * 查詢個人課表方法 * * @param xnd * @param xqd * @throws ClientProtocolException * @throws IOException */ public void queryStuCourse(String xnd, String xqd) throws ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); String newQueryStuCourseUrl = queryStuCourseUrl + stuNumber + "&xm=" + stuName + queryStuCourseGnmkd; String viewState = IOUtils.getViewState(newQueryStuCourseUrl, Cookie, mainUrl + stuNumber); HttpPost queryStuCoursePost = new HttpPost(newQueryStuCourseUrl); List<NameValuePair> stuCoursePair = new ArrayList<NameValuePair>(); stuCoursePair.add( new BasicNameValuePair( "__EVENTTARGET" , "xqd" )); stuCoursePair.add( new BasicNameValuePair( "__EVENTARGUMENT" , "" )); stuCoursePair.add( new BasicNameValuePair( "__VIEWSTATE" , viewState)); stuCoursePair.add( new BasicNameValuePair( "xnd" , xnd)); stuCoursePair.add( new BasicNameValuePair( "xqd" , xqd)); UrlEncodedFormEntity entitySource = new UrlEncodedFormEntity( stuCoursePair); queryStuCoursePost.setEntity(entitySource); queryStuCoursePost.setHeader( "Cookie" , Cookie); queryStuCoursePost.setHeader( "Referer" , mainUrl + stuNumber); HttpResponse responseStuCourse = client.execute(queryStuCoursePost); String html = IOUtils.getHtml(responseStuCourse.getEntity() .getContent(), "GB2312" ); Document docCourse = Jsoup.parse(html); Elements eleCourse = docCourse.select( "td" ); for ( int i = 2 ; i < eleCourse.size(); i++) { System.out.print(eleCourse.get(i).text() + " " ); if (i % 9 == 0 ) { System.out.println(); } } } public static void main(String[] args) { Jwgl jw = new Jwgl(); try { jw.login( "這里是學號" , "這里是密碼" ); System.out.println( "查詢成績測試-------" ); jw.queryStuGrade( "2015-2016" , "1" ); // 查詢西校區,周一,第12節空教室測試。 // jw.queryClassroom("1", "1", "2"); System.out.println( "查詢空教室測試------" ); jw.queryClassroom(); System.out.println( "查詢個人課表測試-------" ); jw.queryStuCourse( "2014-2015" , "1" ); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } //QQ:451209214 } } |
2.DateUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
? package com.ican.yueban.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtils { private static String startDay = GlobalConstant.START_DAY; // 開學日期 private static String endDay = GlobalConstant.END_DAY; // 放假日期 /** * 獲取當前時間是第幾節課,只限8-16點之間,其他時間默認1,2節課。 * * @return */ public static int getNowCourse() { SimpleDateFormat df = new SimpleDateFormat( "HH:mm:ss" ); // 設置日期格式 String nowDate = df.format( new Date()); if (nowDate.startsWith( "08" ) || nowDate.startsWith( "09" )) { return 1 ; // 12節課。 } else if (nowDate.startsWith( "10" ) || nowDate.startsWith( "11" )) { return 2 ; // 34節課,以此類推。 } else if (nowDate.startsWith( "12" ) || nowDate.startsWith( "13" ) || nowDate.startsWith( "14" )) { return 3 ; } else if (nowDate.startsWith( "15" ) || nowDate.startsWith( "16" )) { return 4 ; } else { return 1 ; } } /** * 獲取當前時間是第幾周 * * @return */ public static int getWeek() { int days = 0 ; int nowWeek = 0 ; try { SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" ); // 設置日期格式 String nowDate = df.format( new Date()); int nowDaysBetween = daysBetween(startDay, nowDate) + 1 ; days = daysBetween(startDay, endDay); int x = nowDaysBetween % 7 ; if (x == 0 ) { nowWeek = nowDaysBetween / 7 ; } else { nowWeek = nowDaysBetween / 7 + 1 ; } } catch (ParseException e) { System.out.println( "輸入的日期不合法,解析日期失敗" ); e.printStackTrace(); } return nowWeek; } /** * 獲取當前時間是星期幾 * * @return */ public static int getWeekDay() { Calendar cal = Calendar.getInstance(); cal.setTime( new Date()); if (cal.get(Calendar.DAY_OF_WEEK) - 1 == 0 ) { return 7 ; } return cal.get(Calendar.DAY_OF_WEEK) - 1 ; } /** * 計算兩個String類型日期之間的天數 * * @param startDay * @param endDay * @return * @throws ParseException */ public static int daysBetween(String startDay, String endDay) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(startDay)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(endDay)); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / ( 1000 * 3600 * 24 ); return Integer.parseInt(String.valueOf(between_days)); } /** * 以yyyy-MM-dd HH:mm:ss格式返回String類型系統時間 * * @return */ public static String getNowDate() { SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); // 設置日期格式 return df.format( new Date()); } } |
3.GlobalConstant.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
? package com.ican.yueban.utils; /** * 本系統所有常量定義 * * @author 宋開宗 * */ public interface GlobalConstant { public static final String START_DAY = "2016-02-29" ; public static final String END_DAY = "2016-07-10" ; public static final String INDEX_URL = "http://jwgl2.ujn.edu.cn" ;// 濟大教務系統首頁 public static final String SECRETCODE_URL = "http://jwgl2.ujn.edu.cn/CheckCode.aspx" ;// 驗證碼頁面 public static final String LOGIN_URL = "http://jwgl2.ujn.edu.cn/default2.aspx" ;// 濟大教務系統登錄頁 public static final String MAIN_URL = "http://jwgl2.ujn.edu.cn/xs_main.aspx?xh=" ;// 濟大教務系統主頁,菜單頁面 public static final String QUERY_CLASSROOM_URL = "http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=" ;// 濟大查詢空教室鏈接 public static final String QUERY_CLASSROOM_GNMKDM = "&gnmkdm=N121611" ; // 濟大查詢空教室gnmkdm public static final String QUERY_STU_COURSE_URL = "http://jwgl2.ujn.edu.cn/xskbcx.aspx?xh=" ;// 濟大查詢個人課表鏈接 public static final String QUERY_STU_COURSE_GNMKDM = "&gnmkdm=N121603" ; // 濟大查詢個人課表gnmkdm public static final String QUERY_STU_GRADE_URL = "http://jwgl2.ujn.edu.cn/xscjcx.aspx?xh=" ;// 濟大查詢個人成績鏈接 public static final String QUERY_STU_GRADE_GNMKDM = "&gnmkdm=N121605" ; // 濟大查詢個人成績gnmkdm public static final String IDENTITY_STU = "學生" ; // 身份:學生 public static final String XIXIAOQU = "1" ; // 濟大西校區標志 public static final String DONGXIAOQU = "2" ; // 濟大東校區標志 public static final String ZHANGQIUXIAOQU = "3" ; // 濟大章丘校區標志 public static final String CLASS1 = "'1'|'1','0','0','0','0','0','0','0','0'" ; // 1,2節 public static final String CLASS2 = "'2'|'0','3','0','0','0','0','0','0','0'" ; // 3,4節 public static final String CLASS3 = "'3'|'0','0','5','0','0','0','0','0','0'" ; // 5,6節 public static final String CLASS4 = "'4'|'0','0','0','7','0','0','0','0','0'" ; // 7,8節 public static final String CLASS5 = "'5'|'0','0','0','0','9','0','0','0','0'" ; // 8,10節 public static final String CLASS6 = "'6'|'0','0','0','0','0','11','0','0','0'" ; // 11,12節 public static final String CLASS7 = "'7'|'1','3','0','0','0','0','0','0','0'" ; // 上午 public static final String CLASS8 = "'8'|'0','0','5','7','0','0','0','0','0'" ; // 下午 public static final String CLASS9 = "'9'|'1','3','5','7','0','0','0','0','0'" ; // 白天 public static final String CLASS10 = "'10'|'0','0','0','0','9','11','0','0','0'" ; // 晚上 public static final String CLASS11 = "'11'|'1','3','5','7','9','11','0','0','0'" ; // 全天 public static final String BTN_XUEQI = "btn_xq" ; // 學期成績 public static final String BTN_XUENIAN = "btn_xn" ; // 學年成績 public static final String BTN_LINIAN = "btn_zcj" ; // 歷年成績 public static final String UNIVERSITY_CODE_UJN = "00001" ; // 濟南大學標識代碼 public static final String USER_STATE_N = "1" ; // 未認證 public static final String USER_STATE_Y = "2" ; // 認證通過 public static final String COMMENT_TYPE_NEWTHINGS = "1" ; // 評論類型1:新鮮事 public static final String COMMENT_TYPE_INTEREST = "2" ; // 評論類型2:興趣活動 } |
4.IOUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
? package com.ican.yueban.utils; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.jsoup.Jsoup; public class IOUtils { /** * 指定編碼格式 ,把輸入流轉化為字符串 * * @param is * @return * @throws IOException */ public static String getHtml(InputStream is, String encoding) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte [] buffer = new byte [ 1024 ]; int len = 0 ; while ((len = is.read(buffer)) != - 1 ) { bos.write(buffer, 0 , len); } is.close(); return new String(bos.toByteArray(), encoding); } /** * 下載圖片 * * @param urlString * @param filename * @param savePath * @throws Exception */ public static void download(String urlString, String filename, String savePath) throws Exception { // 構造URL URL url = new URL(urlString); // 打開連接 URLConnection con = url.openConnection(); // 設置請求超時為5s con.setConnectTimeout( 5 * 1000 ); // 輸入流 InputStream is = con.getInputStream(); // 1K的數據緩沖 byte [] bs = new byte [ 1024 ]; // 讀取到的數據長度 int len; // 輸出的文件流 File sf = new File(savePath); if (!sf.exists()) { sf.mkdirs(); } OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename); // 開始讀取 while ((len = is.read(bs)) != - 1 ) { os.write(bs, 0 , len); } // 完畢,關閉所有鏈接 os.close(); is.close(); } /** * 圖片裁剪工具類 * * @param src * @param dest * @param x * @param y * @param w * @param h * @throws IOException */ public static void cutImage(String src, String dest, int x, int y, int w, int h) throws IOException { Iterator iterator = ImageIO.getImageReadersByFormatName( "jpg" ); ImageReader reader = (ImageReader) iterator.next(); InputStream in = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(in); reader.setInput(iis, true ); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x, y, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read( 0 , param); ImageIO.write(bi, "jpg" , new File(dest)); in.close(); } /** * 判斷字符編碼集 * * @param str * @return */ public static String getEncoding(String str) { String encode = "GB2312" ; try { if (str.equals( new String(str.getBytes(encode), encode))) { String s = encode; return s; } } catch (Exception exception) { } encode = "ISO-8859-1" ; try { if (str.equals( new String(str.getBytes(encode), encode))) { String s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8" ; try { if (str.equals( new String(str.getBytes(encode), encode))) { String s2 = encode; return s2; } } catch (Exception exception2) { } encode = "GBK" ; try { if (str.equals( new String(str.getBytes(encode), encode))) { String s3 = encode; return s3; } } catch (Exception exception3) { } return "未知" ; } /** * 把輸入流轉換成圖片---》獲取驗證碼 * * @param is * @param filename * @param savePath * @throws Exception */ public static void getSecret(InputStream is, String filename, String savePath) throws Exception { // 1K的數據緩沖 byte [] bs = new byte [ 1024 ]; // 讀取到的數據長度 int len; // 輸出的文件流 File sf = new File(savePath); if (!sf.exists()) { sf.mkdirs(); } OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename); // 開始讀取 while ((len = is.read(bs)) != - 1 ) { os.write(bs, 0 , len); } // 完畢,關閉所有鏈接 os.close(); is.close(); } /** * 獲取隱藏字段的__VIEWSTATE值 * * @param url * @param cookie * @param referer * @return * @throws UnsupportedOperationException * @throws ClientProtocolException * @throws IOException */ public static String getViewState(String url, String cookie, String referer) throws UnsupportedOperationException, ClientProtocolException, IOException { CloseableHttpClient client = HttpClients.createDefault(); HttpGet getViewState = new HttpGet(url); getViewState.setHeader( "Cookie" , cookie); getViewState.setHeader( "Referer" , referer); // 設置頭信息 String s = IOUtils.getHtml(client.execute(getViewState).getEntity() .getContent(), "GB2312" ); String viewstate = Jsoup.parse(s).select( "input[name=__VIEWSTATE]" ) .val(); client.close(); return viewstate; } } |
5. ParseUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
? package com.ican.yueban.utils; public class ParseUtils { /** * 獲取課程節次對應的字符串 * @param course * @return */ public static String parseWeek(String course) { String sjd= "" ; int nowCourse = Integer.parseInt(course); switch (nowCourse) { case 1 : sjd=GlobalConstant.CLASS1; break ; case 2 : sjd=GlobalConstant.CLASS2; break ; case 3 : sjd=GlobalConstant.CLASS3; break ; case 4 : sjd=GlobalConstant.CLASS4; break ; case 5 : sjd=GlobalConstant.CLASS5; break ; case 6 : sjd=GlobalConstant.CLASS6; break ; case 7 : sjd=GlobalConstant.CLASS7; break ; case 8 : sjd=GlobalConstant.CLASS8; break ; case 9 : sjd=GlobalConstant.CLASS9; break ; case 10 : sjd=GlobalConstant.CLASS10; break ; case 11 : sjd=GlobalConstant.CLASS11; break ; default : sjd=GlobalConstant.CLASS1; break ; } return sjd; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助。