728x90
반응형
@RequestMapping(value = "/rivInfo.do", method = RequestMethod.GET)
public ModelAndView rivInfoModelAndView(HttpSession session, HttpServletRequest request
, HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView("sample/rivInfo");
List<EgovMap> lists = RivInfoService.selectWlobsifList();
for(EgovMap list : lists) {
String obscd = (String) list.get("wlobscd");
Crawling(obscd);
}
//Crawling("1001602");
return mv;
}
public void Crawling(String wlobscd) {
try {
Crawler crawler = new Crawler();
String url = URLEncoder.encode(wlobscd, "UTF-8");
rivInfoVO paramVO = new rivInfoVO();
paramVO.setWlobscd(wlobscd);
String _response = crawler.search(url);
String[] fields = {"ymdh", "wl"};
Map<String, Object> result = crawler.getResult(_response, fields);
List<Map<String, Object>> items = (List<Map<String, Object>>) result.get("result");
for(Map<String, Object> item : items) {
for(String field : fields) {
if(field.equals("ymdh")){
paramVO.setYmdh((String) item.get(field));
} else {
paramVO.setWl((String) item.get(field));
}
}
boolean isSuccess = RivInfoService.insertWlhrInfo(paramVO);
}
} catch (Exception e){
e.printStackTrace();;
}
}
public class Crawler{
final String baseUrl = "http://www.wamis.go.kr:8080/wamis/openapi/wkw/wl_hrdata?startdt=20200101&enddt=20201231&obscd=";
public String search(String _url){
HttpURLConnection con = null;
String result = "";
try {
URL url = new URL(baseUrl + _url);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) result = readBody(con.getInputStream());
else result = readBody(con.getErrorStream());
} catch (Exception e) {
System.out.println("연결 오류 : " + e);
} finally {
con.disconnect();
}
return result;
}
public String readBody(InputStream body){
InputStreamReader streamReader = new InputStreamReader(body);
try (BufferedReader lineReader = new BufferedReader(streamReader)) {
StringBuilder responseBody = new StringBuilder();
String line;
while ((line = lineReader.readLine()) != null) {
responseBody.append(line);
}
return responseBody.toString();
} catch (IOException e) {
throw new RuntimeException("API 응답을 읽는데 실패했습니다.", e);
}
}
public Map<String, Object> getResult(String response, String[] fields) {
Map<String, Object> rtnObj = new HashMap<> ();
try {
JSONParser parser = new JSONParser();
JSONObject result = (JSONObject) parser.parse(response);
JSONArray items = (JSONArray) result.get("list");
List<Map<String, Object>> itemList = new ArrayList<>();
for(int i = 0; i < items.size(); i++) {
JSONObject item = (JSONObject) items.get(i);
Map<String, Object> itemMap = new HashMap<>();
for(String field : fields) {
itemMap.put(field, item.get(field));
}
itemList.add(itemMap);
}
rtnObj.put("result", itemList);
} catch (Exception e) {
System.out.println("getResult error -> " + "파싱 실패, " + e.getMessage());
}
return rtnObj;
}
}
728x90
반응형
'Java' 카테고리의 다른 글
[Java] JRE와 JDK의 차이 (0) | 2023.05.30 |
---|---|
[Java] 주니어 자바 개발자를 위한 100가지 질문 (0) | 2023.05.26 |
[Java] split null 값 처리 (0) | 2023.02.06 |
[Java] 한글 인코딩 변환 체크 (0) | 2022.06.27 |
[Java] HashMap과 keySet, EntrySet (0) | 2021.10.30 |