解析http user-agent信息,使用uasparser-0.6.1.jar和jregex-1.2_01.jar兩個包
import cz.mallat.uasparser.OnlineUpdater;import cz.mallat.uasparser.UASparser;import cz.mallat.uasparser.UserAgentInfo;import java.io.IOException;/** * Created by Edward on 2016/7/1. */public class UserAgentUtil { static UASparser uasParser = null; // 初始化uasParser對象 static { try { uasParser = new UASparser(OnlineUpdater.getVendoredInputStream()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String str = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36"; System.out.println(str); try { UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(str); System.out.println("操作系統名稱:"+userAgentInfo.getOsFamily());// System.out.println("操作系統:"+userAgentInfo.getOsName());// System.out.println("瀏覽器名稱:"+userAgentInfo.getUaFamily());// System.out.println("瀏覽器版本:"+userAgentInfo.getBrowserVersionInfo());// System.out.println("設備類型:"+userAgentInfo.getDeviceType()); System.out.println("瀏覽器:"+userAgentInfo.getUaName()); System.out.println("類型:"+userAgentInfo.getType()); } catch (IOException e) { e.printStackTrace(); } }}運行結果:

User-Agent信息通過瀏覽器自帶工具可查看:
如下使用的是chrome瀏覽器

程序中static塊,在初始化的時候被執行,初始化情況如下:
1.當創建某個類的新實例時;
2.當調用某個類的靜態方法時;
3.當使用某個類或接口的靜態字段時;
4.當調用Java API中的某些反射方法時,比如類Class中的方法,或者java.lang.reflect中的類的方法時;
5.當初始化某個子類時;
6.當虛擬機啟動某個被標明為啟動類的類(main方法);
以上就是本文的全部內容了,希望大家能夠喜歡
新聞熱點
疑難解答