拿走不谢
在使用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;
}