import java.util.Arrays;
施秉ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
public class Array2 {
public static void main(String[] args) {
//聲明一個名為myArray的數組,該數組有2行,每行列數不等,并為其分配內存空間
int[][] myArray = new int[2][];
myArray[0] = new int[5]; //第一行有5個元素,并為其分配內存空間
myArray[1] = new int[10]; //第二行有10個元素,并為其分配內存空間
for (int j = 0; j myArray[0].length; j++)
//用1-10之間的隨機整數給第一行元素賦值
myArray[0][j] = (int)(Math.random() * 10);
//用100-200之間的隨機整數給第二行元素賦值
for (int j=0; j myArray[1].length; j++)
myArray[1][j]=(int)(Math.random() * 100 + 100);
for (int i=0; i myArray.length; i++){ //輸出myArray數組各元素的值
for (int j=0; j myArray[i].length; j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
Arrays.sort(myArray[0]); //對第一行元素排序
Arrays.sort(myArray[1]); //對第二行元素排序
System.out.println("\n排序后的數組元素: ");
for (int i=0; imyArray.length;i++){ //再次輸出myArray數組各元素的值
for (int j=0; jmyArray[i].length;j++){
System.out.print(myArray[i][j]+" ");
}
System.out.println();
}
}
}
7 3 9 6 7
103 165 166 148 103 179 128 109 120 156
排序后的數組元素:
3 6 7 7 9
103 103 109 120 128 148 156 165 166 179
打開項目根目錄文件夾下。執(zhí)行 mvn eclipse:eclipse命令,會生成項目結構,然后在eclipse導入就可以,不過有緣分的是我們用到了一個項目,哈哈,望給個最佳奧,可以一起探討
一、獲取code
將code作為參數傳遞過來
//如果有code,說明是微信小程序,根據code獲取openId
//classify用于標識是哪個小程序
if (!CheckUtil.checkNulls( keUser.getCode(),keUser.getClassify())){
//
String openid = OpenIdUtil.oauth2GetOpenid(keUser.getCode(),keUser.getClassify());
printParamsLog(openid, logger);
keUser.setUserId(openid);
}1234567812345678
二、工具類
package com.util;
import net.sf.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.HashMap;
import java.util.Map;
/**
* @author xsx
*/
public class OpenIdUtil {
public static String oauth2GetOpenid(String code,String classify) {
String appid="";
String appsecret="";
switch (classify){
case "1":
//自己的配置appid
appid = "**********";
//自己的配置APPSECRET;
appsecret = "**********";
break;
case "2":
appid = "**********";
appsecret = "************";
break;
case "3":
appid = "**********";
appsecret = "************";
break;
case "4":
appid = "**********";
appsecret = "************";
break;
case "5":
appid = "**********";
appsecret = "************";
}
//授權(必填)
String grant_type = "authorization_code";
//URL
String requestUrl = "";
//請求參數
String params = "appid=" + appid + "secret=" + appsecret + "js_code=" + code + "grant_type=" + grant_type;
//發(fā)送請求
String data = HttpUtil.get(requestUrl, params);
//解析相應內容(轉換成json對象)
JSONObject json = JSONObject.fromObject(data);
//用戶的唯一標識(openid)
String Openid =String.valueOf(json.get("openid"));
//System.out.println(Openid);
return Openid;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
三、發(fā)送請求的工具類
package com.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* @author xsx
*/
public class HttpUtil {
/**
* 向指定URL發(fā)送GET方法的請求
*
* @param url
* 發(fā)送請求的URL
* @param param
* 請求參數,請求參數應該是 name1=value1name2=value2 的形式。
* @return String 所代表遠程資源的響應結果
*/
public static String get(String url,String param){
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
//System.out.println(urlNameString);
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設置通用的請求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實際的連接
connection.connect();
// 獲取所有響應頭字段
MapString, ListString map = connection.getHeaderFields();
// 遍歷所有的響應頭字段
/*for (String key : map.keySet()) {
System.out.println(key + "---" + map.get(key));
}*/
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送GET請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
對于加密的網站還沒去研究,不知道能不能抓取,現在只是對一些沒有加密的網站進行網頁數據抓取。剛剛開始寫的時候以為很多網站都能抓取,但是發(fā)現很多都加密了,本來以為一些地址可以通過網頁數據檢測工具測出他的數據變化,但是只能監(jiān)測到一些通過js顯示的數據,依然不能抓取到加密的網站。嗨,這個問題以后再說吧。
[java]
import java.net.* ;
import java.io.* ;
import java.util.regex.* ;
public class Capture{
public static void main(String args[])throws Exception{
System.out.println("*************************手機號查詢************************") ;
System.out.println("我的位置是:" + new GrabMobile().grabMobileLocation("15023141745")) ;
System.out.println("手機卡類型是:" + new GrabMobile().grabMobileType("15023141745")) ;
System.out.println("我的郵編是:" + new GrabMobile().grabMobilePost("15023141745")) ;
System.out.println("*************************身份證查詢************************") ;
System.out.println("我的性別是:" + new GrabIdentity().grabIdentitySex("362203199208243575")) ;
System.out.println("我的生日是:" + new GrabIdentity().grabIdentityBirth("362203199208243575")) ;
System.out.println("我的家鄉(xiāng)是:" + new GrabIdentity().grabIdentityHome("362203199208243575")) ;
}
}
class GrabMobile{
public String grabMobileLocation(String m)throws Exception{
String strUrl = ";mobile=" + m;
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = "卡號歸屬地" ;
String strEnd = "卡 類 型";
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+42,end-33) ;
result = drawChMob(result) ;
return result ;
}
public String grabMobileType(String m)throws Exception{
String strUrl = ";mobile=" + m;
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = "卡 類 型" ;
String strEnd = "TD align=\"center\"區(qū) 號/TD";
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+12,end) ;
result = drawChMob(result) ;
result = result.substring(1) ;
return result ;
}
public String grabMobilePost(String m)throws Exception{
String strUrl = ";mobile=" + m;
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = "郵 編" ;
String strEnd = "更詳細的..";
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+40,end-55) ;
return result ;
}
public String drawChMob(String str){
StringBuffer strBuf = new StringBuffer() ;
String regex="([\u4e00-\u9fa5]+)";
Matcher matcher = Pattern.compile(regex).matcher(str);
while(matcher.find()){
strBuf.append(matcher.group(0)).toString() ;
}
return strBuf.toString() ;
}
}
class GrabIdentity{
public String grabIdentitySex(String userid)throws Exception{
String strUrl = ";userid=" + userid + "B1=%B2%E9+%D1%AF";
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = " 別" ;
String strEnd = "出生日期";
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+7,end) ;
result = drawCh(result) ;
return result ;
}
public String grabIdentityBirth(String userid)throws Exception{
String strUrl = ";userid=" + userid + "B1=%B2%E9+%D1%AF";
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = "出生日期:/tdtd class=\"tdc2\"" ;
String strEnd = "/tdtrtrtd class=";
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+27,end) ;
return result ;
}
public String grabIdentityHome(String userid)throws Exception{
String strUrl = ";userid=" + userid + "B1=%B2%E9+%D1%AF";
URL url = new URL(strUrl) ;
HttpURLConnection httpUrlCon = (HttpURLConnection)url.openConnection() ;
InputStreamReader inRead = new InputStreamReader(httpUrlCon.getInputStream(),"GBK") ;
BufferedReader bufRead = new BufferedReader(inRead) ;
StringBuffer strBuf = new StringBuffer() ;
String line = "" ;
while ((line = bufRead.readLine()) != null) {
strBuf.append(line);
}
String strStart = "證 地:/tdtd class=\"tdc2\"" ;
String strEnd = "br//td/trtrtd class=\"tdc3\" valign=\"top\" align=\"right\"部分或" ;
String strAll = strBuf.toString() ;
int start = strAll.indexOf(strStart) ;
int end = strAll.indexOf(strEnd) ;
String result = strAll.substring(start+31,end) ;
return result ;
}
public String drawCh(String str){
StringBuffer strBuf = new StringBuffer() ;
String regex="([\u4e00-\u9fa5]+)";
Matcher matcher = Pattern.compile(regex).matcher(str);
if(matcher.find()){
str = strBuf.append(matcher.group(0)).toString() ;
}
return str ;
}
}
待會傳上改裝成的android小程序,可以手機號查詢和身份證查詢。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Screen{
public static void main(String args[]){
new Win();
}
static class Win extends JFrame implements ActionListener{
JPanel jp = new JPanel();
JButton jb[] = new JButton[4];
public Win(){
this.setBounds(0, 0, 320, 320);
Color c[] = {Color.red,Color.yellow,Color.blue};
jp.setBackground(Color.black);
for(int i = 0 ; i 4 ; i++){
jb[i] = new JButton();
if(i!=3){
jb[i].setBackground(c[i]);
}else{
jb[i].setText("退出");
}
jb[i].addActionListener(this);
jp.add(jb[i]);
}
this.add(jp);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(!((JButton)e.getSource()).getText().equals("退出")){//如果不是退出按鈕,則換顏色
jp.setBackground(((JButton)e.getSource()).getBackground());
}
else
System.exit(0);//退出
}
}
}