Java tutorial
package com.aoyun.serviceOld; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aoyun.db.JdbcOperate; import com.aoyun.po.ParkingLPRBean; import com.aoyun.util.BeanUtil; import com.aoyun.util.Constantz; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by Tom on 2016/11/28. */ public class GetValForYKPOne { public static Logger logger = Logger.getLogger(GetValForYKPOne.class); private static List<File> xmlFileList = new ArrayList<File>(); private static List<File> XMLListForError = new ArrayList<File>(); private BeanUtil beanUtil = new BeanUtil(); /** * XML * * @param path * @return XML? * @throws Exception */ public List<File> getFilePath(String path) throws Exception { File file = new File(path); if (file.isDirectory()) { File[] files = file.listFiles(); for (File f : files) { String reg = "\\d+\\-\\d+\\-\\d+"; Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(f.getParent()); if (matcher.find()) { String dateStr = matcher.group(); if (isTrue(dateStr)) { getFilePath(f.getPath()); } } else { getFilePath(f.getPath()); } } } else if (file.isFile()) { if (file.getName().endsWith(".xml")) { xmlFileList.add(file); } } return xmlFileList; } /** * ?XMLoracle? * * @param xmlFileList * @throws Exception */ public void getValueForXml(List<File> xmlFileList, JdbcOperate jdbcOperate) throws Exception { ArrayList<JSONObject> parkingLPRJSONList = new ArrayList<JSONObject>(); List<ParkingLPRBean> parkingLPRList = new ArrayList<ParkingLPRBean>();//ParkingLPR(?)? ParkingLPRBean parkingLPR = null;// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (File file : xmlFileList) { try { parkingLPR = new ParkingLPRBean(); SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); String code = getCode(); parkingLPR.setId(code); if (root.element("device_id") == null) { parkingLPR.setDeviceId(""); } else { parkingLPR.setDeviceId(root.element("device_id").getText()); } parkingLPR.setVersion("10000001"); if (root.element("plate_license") == null) { parkingLPR.setPlateNumber(""); } else { parkingLPR.setPlateNumber(root.element("plate_license").getText()); } if (root.element("plate_color") == null) { parkingLPR.setPlateColor(""); } else { String plateColor = getPlateColor(root.element("plate_color").getText()); parkingLPR.setPlateColor(plateColor); } if (root.element("capture_time") == null) { parkingLPR.setPassTime(""); } else { parkingLPR.setPassTime(root.element("capture_time").getText()); } if (root.element("camera_ipaddr") == null) { parkingLPR.setDeviceIp(""); } else { parkingLPR.setDeviceIp(root.element("camera_ipaddr").getText()); } if (root.element("longitude") == null) { parkingLPR.setLongitude(""); } else { parkingLPR.setLongitude(root.element("longitude").getText()); } if (root.element("latitude") == null) { parkingLPR.setLatitude(""); } else { parkingLPR.setLatitude(root.element("latitude").getText()); } if (root.element("site_code") == null) { parkingLPR.setParkingId(""); } else { String siteCode = root.element("site_code").getText(); String substring = siteCode.substring(0, 4); parkingLPR.setParkingId(substring); } if (root.element("direction") == null) { parkingLPR.setInOut(""); } else { parkingLPR.setInOut(root.element("direction").getText()); } String date = dateFormat.format(new Date()); parkingLPR.setCreateTime(date); parkingLPR.setSource("1"); if (root.element("plate_type") == null) { parkingLPR.setPlateType(""); } else { parkingLPR.setPlateType(root.element("plate_type").getText()); } if (root.element("car_color") == null) { parkingLPR.setCarColor(""); } else { parkingLPR.setCarColor(root.element("car_color").getText()); } if (root.element("car_type") == null) { parkingLPR.setCarType(""); } else { parkingLPR.setCarType(root.element("car_type").getText()); } if (root.element("lane") == null) { parkingLPR.setLane(""); } else { parkingLPR.setLane(root.element("lane").getText()); } if (root.element("cs") == null) { parkingLPR.setCs(0); } else { parkingLPR.setCs(Integer.parseInt(root.element("cs").getText())); } //list? parkingLPRList.add(parkingLPR); //????ID????IP????ID?ID String carNo = root.element("plate_license").getText(); String parkcode = root.element("site_code").getText(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String ctStr = root.element("capture_time").getText(); String inOut = root.element("direction").getText(); String num = ""; if (inOut.equals("0")) { num = "-1"; } else { num = "1"; } long capture_time = format.parse(ctStr).getTime();//? List<NameValuePair> list = new ArrayList<NameValuePair>();//??? list.add(new BasicNameValuePair("parkcode", parkcode)); list.add(new BasicNameValuePair("qty", num)); YKPMain.elementList.add(root); YKPMain.list.add(list); List<NameValuePair> nvps = new ArrayList<NameValuePair>();//? nvps.add(new BasicNameValuePair("parkcode", parkcode)); nvps.add(new BasicNameValuePair("carnumber", carNo)); nvps.add(new BasicNameValuePair("regtime", capture_time + "")); nvps.add(new BasicNameValuePair("direction", inOut)); YKPMain.carList.add(nvps);// } catch (DocumentException e) { logger.error("?XML:" + file.getPath()); } } //beanjsonJSON list try { for (ParkingLPRBean parkingLPRs : parkingLPRList) { JSONObject parkingLPRJSON = beanUtil.convertBeanToJson(parkingLPRs); parkingLPRJSONList.add(parkingLPRJSON); } } catch (Exception e) { logger.error("?JSON" + e.getMessage()); } //keys String keys[] = { "id", "deviceId", "version", "plateNumber", "plateColor", "passTime", "deviceIp", "longitude", "latitude", "parkingId", "inOut", "source", "plateType", "carColor", "carType", "lane" }; //?sql? String sql = "insert into parking_lpr(id,device_id,version,plate_number,plate_color,pass_time,device_ip,longitude,latitude," + "parking_id,in_out,create_time,source,plate_type,car_color,car_type,lane)" + "values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?,?,?,?,?,sysdate,?,?,?,?,?)"; //???array JSONArray array = jdbcOperate.executeBatchPre(sql, parkingLPRJSONList, keys); JSONObject jsonObject = null; //???? if (array.size() == 0) { logger.info(":" + array.size() + "???"); //0oraclexml for (File file : xmlFileList) { file.delete(); } xmlFileList.clear(); } else { int begin = 0; int end = 0; for (int i = 0; i < array.size(); i++) { jsonObject = array.getJSONObject(i); System.out.println(jsonObject); Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet(); for (Map.Entry<String, Object> entry : entrySet) { String key = entry.getKey(); if (key.equals("begin")) { begin = Integer.parseInt(entry.getValue().toString()); } else if (key.equals("end")) { end = Integer.parseInt(entry.getValue().toString()); } } logger.error(":" + array.size() + "?" + "" + begin + "-" + end + "??"); //?XML getXMLForError(begin, end, xmlFileList); for (int j = 0; j < XMLListForError.size(); j++) { logger.error(begin + "---" + end + " :?" + XMLListForError.get(j).getPath()); } } //??XML(??)? for (int i = 0; i < xmlFileList.size(); i++) { for (int j = i; j < XMLListForError.size(); j++) { if (xmlFileList.get(i).getName().equals(XMLListForError.get(j).getName())) { break; } } if (i > XMLListForError.size()) { xmlFileList.get(i).delete(); } } System.out.println(XMLListForError.size()); XMLListForError.clear(); xmlFileList.clear(); } } /** * @param dateStr * @return * @throws ParseException */ public boolean isTrue(String dateStr) throws ParseException { Calendar now = Calendar.getInstance(); now.add(Calendar.DAY_OF_YEAR, -Constantz.beforeDate); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String mDateTime = formatter.format(now.getTime()); Date old = formatter.parse(dateStr); Date before = formatter.parse(mDateTime); if (old.compareTo(before) >= 0) { return true; } return false; } /** * ?? */ public String getPlateColor(String plateColor) { if (plateColor.equals("?")) { plateColor = "1"; } else if (plateColor.equals("")) { plateColor = "2"; } else if (plateColor.equals("")) { plateColor = "3"; } else if (plateColor.equals("")) { plateColor = "4"; } else if (plateColor.equals("")) { plateColor = "5"; } else { plateColor = "0"; } return plateColor; } /** * 32??ID * * @return */ public String getCode() { UUID uuid = UUID.randomUUID(); return uuid.toString().replaceAll("\\-", ""); } /** * ??XML */ public void getXMLForError(int begin, int end, List<File> XMLFileList) { for (int i = begin; i < end; i++) { XMLListForError.add(XMLFileList.get(i)); } } }