生活随笔
收集整理的這篇文章主要介紹了
java 用sevlet实现ip定位以及天气预报的功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近兩天想實現(xiàn)一個登陸網(wǎng)站就可以自動顯示該地區(qū)的的天氣情況。很是苦惱。慢慢研究然后才其所得。
研究的思路大致是這樣的。ip 定位–>通過位置獲取天氣。首先聲明一下,以前國家氣象局的接口已經(jīng)被封,以前直接傳一個json數(shù)據(jù)就有天氣情況,現(xiàn)在需要手動解析下。
所以,這樣的實際思路為:ip獲取地址(操作獲取城市名稱)------百度下載各城市對應(yīng)編號---------io字符串處理(你方便得到的)------通過城市名獲取編號---------通過編號組成網(wǎng)頁url訪問---------截取你所需要的部分。
1:首先,你要做的事可以通過ip訪問到你的地理位置,百度地圖api免費并且挺好用的。百度api
http://api.map.baidu.com/location/ip //HTTP協(xié)議
https://api.map.baidu.com/location/ip //HTTPS
協(xié)議你要先申請下才能獲取ak,你要訪問的地址 其實是http://api.map.baidu.com/location/ip?ak=“你的ak”;如果有ip加上:&ip="";他返回的是一個json串,你可以用阿里的fastjson解析他,獲取你想要的東西。
2:有了地址之后,在上中國氣象局官網(wǎng)
你會發(fā)現(xiàn)每個城市都有一個編號,你可以百度復(fù)制到然后通過io字符串處理例如這樣
通過百度接口獲取的名稱,在通過io按照行讀取遍歷找到這個城市的編號,返回
3:通過返回的號拼接地址,抓取,谷歌瀏覽器先抓包分析要抓去的層次內(nèi)容:
下面先附上沒涉及web端的測試代碼:爬取分析工具:jsuop fastjson
獲取地址 調(diào)用方法主類
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.net
.HttpURLConnection
;
import java
.net
.MalformedURLException
;
import java
.net
.URL
;import org
.jsoup
.Jsoup
;
import org
.jsoup
.nodes
.Document
;import com
.alibaba
.fastjson
.JSON
;
import com
.alibaba
.fastjson
.JSONObject
;public class 地址
{public static void main(String
[] args
) throws IOException
{Document doc
=Jsoup
.connect("http://api.map.baidu.com/location/ip?ak=nZFzpfoHLDVD3pEPGaSrGCYebppWx7ge").ignoreContentType(true).get();JSONObject jsonObj
= JSON
.parseObject(doc
.text());
JSONObject jsonObj1
=jsonObj
.getJSONObject("content").getJSONObject("address_detail");String jsonObj2
=(String
) jsonObj1
.get("city");System
.out
.println(jsonObj2
);city city
=new city();String number
= city
.getcity(jsonObj2
);System
.out
.println(number
);String weather
=new search().weather(number
);System
.out
.println(jsonObj2
":" weather
);}
}
返回城市編號
import java
.io
.BufferedReader
;
import java
.io
.BufferedWriter
;
import java
.io
.File
;
import java
.io
.FileNotFoundException
;
import java
.io
.FileReader
;
import java
.io
.FileWriter
;
import java
.io
.IOException
;public class city {public city(){}public String
getcity(String city
) throws IOException
{{String citynumber
="";File file
=new File("E:/bianhao2.txt");FileReader in
=new FileReader(file
);BufferedReader buf
=new BufferedReader(in
);String s
="";while((s
=buf
.readLine())!=null
){String a
[]=s
.split(":");if(a
[0].equals(city
.substring(0, city
.length()-1))){citynumber
=a
[1];}} in
.close();buf
.close();return citynumber
; }}
}
返回天氣信息,簡單爬蟲不做過多解釋
import java
.io
.IOException
;import org
.jsoup
.Jsoup
;
import org
.jsoup
.nodes
.Document
;
import org
.jsoup
.nodes
.Element
;
import org
.jsoup
.select
.Elements
;public class search {public search() {}public String
weather(String bianhao
) throws IOException
{String url
="http://www.weather.com.cn/weather/" bianhao
".shtml";Document doc
=Jsoup
.connect(url
).get();Elements links
=doc
.getElementsByClass("sky skyid lv3 on");return links
.text();}
}
上述是測試主機。如果整合到web上,有幾點注意的
1:request方法獲取客戶端ip正常事可以的,但是獲取本機會是ipv6。并且這個ip也查不到地理位置,所以就需要判斷是否為本機,如果市本機就默認使用不帶ip(不帶ip是解析本機)。
2:通過session傳遞數(shù)據(jù)。跳轉(zhuǎn)到該網(wǎng)頁但url不變。
3:輸入流,這就是很坑的地方,如果正常的字符流在windows上是沒問題的,但是部署到linnux服務(wù)器上會亂碼,并且linux的sevlet不好調(diào)試還得從新導(dǎo)入。試了很多方法才發(fā)現(xiàn)原來是我的字符編碼沒設(shè)置好(設(shè)置成utf不行你可以試試)。注意流的關(guān)閉
4:sesson.jsp網(wǎng)頁自行設(shè)計,傳遞的內(nèi)容只需session.getAttribute(“tf”),即可獲取。注意訪問頁面第一次要訪問sevlet。我也不知道為什么。
sevlet代碼為
import java
.io
.*
;import javax
.servlet
.RequestDispatcher
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.ServletRequest
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;import org
.jsoup
.Jsoup
;
import org
.jsoup
.nodes
.Document
;
import org
.jsoup
.select
.Elements
;import com
.alibaba
.fastjson
.JSON
;
import com
.alibaba
.fastjson
.JSONObject
;public class weather extends HttpServlet {public void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{request
.setCharacterEncoding("UTF-8");response
.setCharacterEncoding("UTF-8");String path
=this.getServletContext().getRealPath("/");String ip
=request
.getRemoteAddr();String localip
=request
.getLocalAddr();String url
="http://api.map.baidu.com/location/ip?ak=nZFzpfoHLDVD3pEPGaSrGCYebppWx7ge";if(!ip
.equals(localip
)){url
="&ip=" ip
;}Document doc
=Jsoup
.connect(url
).ignoreContentType(true).get();JSONObject jsonObj
= JSON
.parseObject(doc
.text());
JSONObject jsonObj1
=jsonObj
.getJSONObject("content").getJSONObject("address_detail");String jsonObj2
=(String
) jsonObj1
.get("city");String number
= getcity(request
, jsonObj2
, path
);String weather
=jsonObj2
":" getweather(number
);request
.getSession().setAttribute("tf",weather
);RequestDispatcher dis
= request
.getRequestDispatcher("session.jsp");dis
.forward(request
,response
);}public void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{this.doGet(request
, response
);}public String
getweather(String bianhao
) throws IOException
{String url
="http://www.weather.com.cn/weather/" bianhao
".shtml";Document doc
=Jsoup
.connect(url
).get();Elements links
=doc
.getElementsByClass("sky skyid lv3 on");return links
.text();}public String
getcity(HttpServletRequest request
,String city
,String path
) throws IOException
{{String citynumber
="";File file
=new File(path
"image/bianhao2.txt");citynumber
=file
.getAbsolutePath();InputStreamReader isr
= new InputStreamReader(new FileInputStream(file
),"GB2312");BufferedReader buf
=new BufferedReader(isr
); String s
="";while((s
=buf
.readLine())!=null
){String a
[]=s
.split(":");if(a
[0].equals(city
.substring(0, city
.length()-1))){ citynumber
=a
[1];return citynumber
; } } isr
.close();buf
.close();return citynumber
; }}
}
祝君好運,一起加油。
總結(jié)
以上是生活随笔為你收集整理的java 用sevlet实现ip定位以及天气预报的功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。