拿走不謝
在使用springboot獲取屏幕參數時,可能會報關於headless爲NULL的錯誤,不要慌,這樣來:只需要在啓動的application中替換main函數內容爲:
@SpringBootApplication
public class AdApplication{
public static void main(String[] args) {
new SpringApplicationBuilder(AdApplication.class).headless(false).run(args);
}
}
如果這樣紙還沒有解決你的問題,那麼參考大神回覆!
我使用的是百度地圖開發,這就需要你先註冊成爲一個開發者,獲取到服務祕鑰
/**
* 根據地址獲取經緯度
* @return
*/
public static Map<String, Double> getLatAndLngByAddress() {
String address = getPublicIPAndOperator().split(",")[2];
String lat = "";
String lng = "";
try {
address = URLEncoder.encode(address, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String url = String.format(
"http://api.map.baidu.com/geocoder/v2/?"+"ak=你獲取的祕鑰&output=json&address=%s",
address);
URL myURL = null;
URLConnection httpsConn = null;
// 進行轉碼
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
lat = data.substring(data.indexOf("\"lat\":") + ("\"lat\":").length(),
data.indexOf("},\"precise\""));
lng = data.substring(data.indexOf("\"lng\":") + ("\"lng\":").length(), data.indexOf(",\"lat\""));
}
insr.close();
}
} catch (IOException e) {
}
Map<String, Double> map = new HashMap<String, Double>();
map.put("lat", Double.valueOf(lat));
map.put("lng", Double.valueOf(lng));
return map;
}
/**
* 獲取本機訪問外網的真實IP 和 運營商 和 所在省市
*
* @return
*/
public static String getPublicIPAndOperator() {
String ip = "";
String operator = "";
String address = "";
Document doc = null;
Connection con = null;
con = Jsoup.connect("http://2017.ip138.com/ic.asp").timeout(10000);
try {
doc = con.get();
// 獲得包含本機ip的文本串:您的IP是:[xxx.xxx.xxx.xxx]
Elements els = doc.body().select("center");
for (Element el : els) {
ip = el.text();
}
System.out.println(ip);
operator = ip.split(" ")[2];
address = ip.split(" ")[1].split(":")[1];
// 從文本串過濾出ip,用正則表達式將非數字和.替換成空串""
ip = ip.replaceAll("[^0-9.]", "");
} catch (IOException e) {
e.printStackTrace();
}
return ip + "," + operator+","+address;
}
/**
* 獲取設備屏幕的寬度和高度
*
* @return
*/
public static String GetScreenSize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
String screenWidth = String.valueOf(screenSize.getWidth());
String screenHeight = String.valueOf(screenSize.getHeight());
return screenWidth + "," + screenHeight;
}
/**
* 獲取當前時間
*
* @return
*/
public static String getCurrentTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(System.currentTimeMillis());
}
/**
* 檢測用戶使用的瀏覽器類型,版本號以及系統類別
*
* @param request
* @return
*/
public static String getOsAndBrowser(HttpServletRequest request) {
String browserDetails = request.getHeader("User-Agent");
String userAgent = browserDetails;
String user = userAgent.toLowerCase();
System.out.println(browserDetails);
String os = "";
String browser = "";
// =================OS Info=======================
if (userAgent.toLowerCase().indexOf("windows") >= 0) {
os = "Windows";
} else if (userAgent.toLowerCase().indexOf("mac") >= 0) {
os = "Mac";
} else if (userAgent.toLowerCase().indexOf("x11") >= 0) {
os = "Unix";
} else if (userAgent.toLowerCase().indexOf("android") >= 0) {
os = "Android";
} else if (userAgent.toLowerCase().indexOf("iphone") >= 0) {
os = "IPhone";
} else {
os = "UnKnown, More-Info: " + userAgent;
}
// ===============Browser===========================
if (user.contains("edge")) {
browser = (userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");
} else if (user.contains("msie")) {
String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
} else if (user.contains("safari") && user.contains("version")) {
browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0] + "-"
+ (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
} else if (user.contains("opr") || user.contains("opera")) {
if (user.contains("opera")) {
browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0] + "-"
+ (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
} else if (user.contains("opr")) {
browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))
.replace("OPR", "Opera");
}
} else if (user.contains("chrome")) {
browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
} else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1)
|| (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1)
|| (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
browser = "Netscape-?";
} else if (user.contains("firefox")) {
browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
} else if (user.contains("rv")) {
String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");
browser = "IE" + IEVersion.substring(0, IEVersion.length() - 1);
} else {
browser = "UnKnown, More-Info: " + userAgent;
}
return os + "," + browser;
}