HTTPClient實現跨服務器傳遞數據


摘要:(實現步驟很簡單,內容之所以很長,主要是我粘貼的原代碼需要處理的數據的有點多(關聯的表數據太多了),哈哈哈哈~)

本文參考自:https://blog.csdn.net/weixin_33755557/article/details/89621201

首先:
引入工具類: 可以自行添加/修改

package com.shd.biz.bureauDetect.uploadData.util;//工具包路徑

public class Constants {
    
    public static final String TEST = "http://192.168.4.143:8089/sms";
    public static final String PRODUCT_MANAGEMENT_HOST = "http://192.168.4.143:8085/sms";
    
}

引入HttpClient支持:

@Autowired
private RestTemplate restTemplate;

 

1.查詢數據 :getForEntity(url,Object.class)

參數一(URL): 需要調用服務的地址(可以攜帶數據)

參數二(Object.class):String.class表示我希望返回的body類型是String

返回值類型: ResponseEntity*<*T*>*

@GetMapping("/findProductByPage")
    public ResponseEntity<String> findProductByPage(Integer page, Integer rows,Product product){
        HttpStatus statusCode = null;
        try {
            String url = Constants.PRODUCT_MANAGEMENT_HOST+ "/product/findProductByPage?page="+page+"&rows="+rows+"&product="+product;
            ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);//跨服務器訪問
            statusCode = entity.getStatusCode();//獲取狀態碼
            String body = entity.getBody(); //獲取返回數據
            return new ResponseEntity<>(body,statusCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(statusCode);
        }
}

2.若需要修改訪問端服務器的數據(保存數據): postForEntity(url,pojo,Object.class)
參數一:需要調用服務的地址(可以攜帶數據)
參數二:傳遞的數據
參數三:Object.class表示我希望返回的body類型,例如String.class是希望返回的body類型String類型
*注意:該方法傳遞給跨服務器是 方法參數上需要加上@RequestBody 否則無法接受到數據

@PostMapping("/saveProduct")
   public ResponseEntity<String> saveProduct(Product product){
       HttpStatus statusCode =null;
       try {
           JSONObject proJson = JSONObject.fromObject(product);
           String prodct = proJson.toString();
           String url = Constants.PRODUCT_MANAGEMENT_HOST+"/product/saveProduct";
           ResponseEntity<String> entity = restTemplate.postForEntity(url, prodct, String.class);
           String body = entity.getBody();
           statusCode = entity.getStatusCode();
           return new ResponseEntity<>(HttpStatus.CREATED);
       } catch (Exception e) {
           e.printStackTrace();
           return new ResponseEntity<>(statusCode);
       }
}

下面是我使用的完整例子,或許有所差異,謹供參考:

第一步:添加工具類(設置對方服務器的訪問地址,提供於實現層方法里的url參數)

第二步:uploadDateService定義接口:

/**
     * @function 發送端.
     * @author Liangjw  
     * @date 2019年10月10日 上午11:06:17
     * @param testId 局放檢測任務id
     * @return
     */
    public String uploadData(String testId);

注:我這里是前端傳數據的id,然后后台接收id,查詢獲取相關表的數據,然后將數據封裝到JSONObject中,傳遞的時候轉換成json字符串再傳遞數據,這樣的好處是在服務端無需建立參數模型,直接接收String,便於后期維護。

 

第三步:uploadDateserviceImp實現層實現發送數據接口:

注:我獲取的數據很多,你自需要理解步驟思路就可以了
1.獲取自己需要上傳的數據,封裝到一個JSONObject對象中,然后把它轉成Json字符串傳遞過去。
例如:StringEntity entity = new StringEntity(params.toJSONString(), "UTF-8");
2.然后獲取httpClient,並創建請求方式(get只能獲取數據,post可修改數據);
3.post請求是將參數放在請求體里面傳過去的;

4.創建響應模型(獲取請求的響應結果response);

/**
     * 
     * @function 上傳數據發送端.
     * @author Liangjw
     * @date 2019-10-18 上午10:10:30  
     * @param testId 局放檢測任務id
     */
    @Override
    public String uploadData(String testId) {      
        if(testId != null && !"".equals(testId)){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("TEST_ID", testId);
            //電纜局放檢測任務
            DataRecord pdTestDr = this.getDao().queryForDataRecord(PATH + "getPDTestByTestId", map);  //查詢車載系統庫中的檢測任務表
            //監測點
            List<DataRecord> test_pointList = this.getDao().queryForDataSet(PATH + "getTestPointByTestId", map).getResults();  //查詢車載系統庫中的監測點表
            //電纜線路(位置)
            Map<String, Object> placeIdMap = new HashMap<String, Object>();
            placeIdMap.put("PLACE_ID", pdTestDr.getLong("PLACE_ID"));
            DataRecord placeCableDr = this.getDao().queryForDataRecord(PATH + "getPlaceCableByPlaceId", placeIdMap);  //查詢車載系統庫中的電纜線路(位置)表
            //檢測電纜(檢測任務與電纜設備的中間表)
            List<DataRecord> test_cableList = this.getDao().queryForDataSet(PATH + "getTestCableByPlaceId", map).getResults();  //查詢車載系統庫中的檢測電纜表
            //電纜局放檢測分析結果
            List<DataRecord> test_analysis_resultList = this.getDao().queryForDataSet(PATH + "getTestAnalysisResultByTestId", map).getResults();  //查詢車載系統庫中的電纜局放檢測分析結果
            //電纜(設備)
            Map<String, Object> deviceIdsMap = new HashMap<String, Object>();
            String ids = "";
            if(test_analysis_resultList.size() > 0){
                for(DataRecord arDr : test_analysis_resultList){
                    String tempId = arDr.getLong("DEV_ID").toString()+",";
                    ids += tempId;                    
                }
            }
            ids=(ids.substring(0,ids.length()-1)).trim();    
            deviceIdsMap.put("ids", ids);
            List<DataRecord> device_cableList = this.getDao().queryForDataSet(PATH + "getDeviceCableByDeviceId", deviceIdsMap).getResults();  //查詢車載系統庫中的電纜設備
            //局放檢測裝置
            Map<String, Object> equipmentIdsMap = new HashMap<String, Object>();
            String equipmentIds = "";
            if(test_pointList != null && test_pointList.size() > 0){
                for(DataRecord test_PointDr : test_pointList){
                    String tempId2 = test_PointDr.getLong("EQUIPMENT_ID").toString()+",";
                    equipmentIds += tempId2;                    
                }
            }
            equipmentIds=(equipmentIds.substring(0,equipmentIds.length()-1)).trim();    
            equipmentIdsMap.put("equipmentIds", equipmentIds);
            List<DataRecord> detection_deviceList = this.getDao().queryForDataSet(PATH + "getDetectionDeviceByEquipmentIds", equipmentIdsMap).getResults();  //查詢車載系統庫中的局放檢測裝置
            
            JSONObject params = new JSONObject();
            
            List<DataRecord> QPhiList = new ArrayList<DataRecord>();
            List<DataRecord> PRPDList = new ArrayList<DataRecord>();
            List<DataRecord> PRPSList = new ArrayList<DataRecord>();
            DataRecord sysLogQPhi = new DataRecord(), sysLogPRPD = new DataRecord(), sysLogTimeField = new DataRecord();
            
            //獲取三個表上一次分別上傳的截止時間
            String jsonMap = isExitUploadTime();
            if (jsonMap.startsWith("{")) {
                jsonMap = jsonMap.substring(1, jsonMap.length());
            }
            if (jsonMap.endsWith("}")) {
                jsonMap = jsonMap.substring(0, jsonMap.length() - 1);
            }
            Map<String, Object> paramMap = new HashMap<String, Object>();
            String[] out = jsonMap.split(",");
            for (String anOut : out) {
                String[] inn = anOut.split("=");
                paramMap.put(inn[0].trim(), inn[1].trim());
            }
            JSONObject jsonObject = new JSONObject(paramMap);
            String isExitQPhiDate = jsonObject.getString("isExitQPhiDate");
            String idQPhi = null;
            if(jsonObject.containsKey("idQPhi") == true){
                idQPhi = jsonObject.getString("idQPhi");
            }
            String isExitPRPDDate = jsonObject.getString("isExitPRPDDate");
            String idPRPD = null;
            if(jsonObject.containsKey("idPRPD") == true){
                idPRPD = jsonObject.getString("idPRPD");
            }
            String isExitTimeFieldDate = jsonObject.getString("isExitTimeFieldDate");
            String idTimeField = null;
            if(jsonObject.containsKey("idTimeField") == true){
                idTimeField = jsonObject.getString("idTimeField");
            }
            
            Map<String, Object> typeNameMap = new HashMap<String, Object>();
            typeNameMap.put("TYPE_NAME", "檢測車");
            //查詢檢測車
            DataRecord mechanicsType = this.getDao().queryForDataRecord(PATH + "getMechanicsTypeByTypeName", typeNameMap);  //查詢車載系統庫中的機具類型表
            Map<String, Object> typeIdMap = new HashMap<String, Object>();
            typeIdMap.put("TYPE_ID", mechanicsType.getString("TYPE_ID"));
            List<DataRecord> mechanics = this.getDao().queryForDataSet(PATH + "getMechanicsByTypeId", typeIdMap).getResults();  //查詢車載系統庫中的機具表
            String DATA_TYPE_KEY_QPhi = null, DATA_TYPE_KEY_PRPD = null, DATA_TYPE_KEY_TimeField = null;
            if(mechanics != null && mechanics.size() > 0){
                DATA_TYPE_KEY_QPhi = mechanics.get(0).getString("MECHANICS_ID")+"tb_QPhiData";
                DATA_TYPE_KEY_PRPD = mechanics.get(0).getString("MECHANICS_ID")+"tb_PRPDData";
                DATA_TYPE_KEY_TimeField = mechanics.get(0).getString("MECHANICS_ID")+"tb_TimeFieldData";
            }
            
            //獲取PRPD圖譜圖片
            String file_original_name =  testId;
            String PRPDImages_folderPath = SbpConfig.getProperty("sbp.phase.PRPD.images") + File.separator +file_original_name;
            String  uploadImagesResult = uploadImages(file_original_name, PRPDImages_folderPath);        
            System.out.println("PRPD圖譜圖片上傳狀態:"+uploadImagesResult);
            
            //獲取QPhi、PRPD、TimeField數據
            if(test_pointList != null && test_pointList.size() > 0){
                String sqlQPhi = null, sqlPRPD = null, sqlTimeField = null;
                String beginTimeQPhi = null, beginTimePRPDD = null, beginTimeTimeField = null;
                //QPhi
                if(isExitQPhiDate.equals("是")){
                    beginTimeQPhi = jsonObject.getString("newQPhiUploadTime");
                    sqlQPhi = "select GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PDDataA, PhaseDataB, PDDataB, PhaseDataC, PDDataC, PhaseFreq " +
                    "from tb_QPhiData where ReceiveTime > ? order by ReceiveTime";
                }else{
                    sqlQPhi = "select GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PDDataA, PhaseDataB, PDDataB, PhaseDataC, PDDataC, PhaseFreq " +
                    "from tb_QPhiData order by ReceiveTime";
                }
                
                //PRPD
                if(isExitPRPDDate.equals("是")){
                    beginTimePRPDD = jsonObject.getString("newPRPDUploadTime");
                    sqlPRPD = "select GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PRPDDataA, PhaseDataB, PRPDDataB, PhaseDataC, PRPDDataC " +
                    "from tb_PRPDData where ReceiveTime > ? order by ReceiveTime";
                }else{
                    sqlPRPD = "select GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PRPDDataA, PhaseDataB, PRPDDataB, PhaseDataC, PRPDDataC " +
                    "from tb_PRPDData order by ReceiveTime";
                }
                
                //TimeField
                if(isExitTimeFieldDate.equals("是")){
                    beginTimeTimeField = jsonObject.getString("newTimeFieldUploadTime");
                    sqlTimeField = "select GUID, DeviceID, ReceiveTime, TimeStamp, TimeFieldDataA, TimeFieldDataB, TimeFieldDataC, GUID_QPhi " +
                    "from tb_TimeFieldData where ReceiveTime > ? order by ReceiveTime";
                }else{
                    sqlTimeField = "select GUID, DeviceID, ReceiveTime, TimeStamp, TimeFieldDataA, TimeFieldDataB, TimeFieldDataC, GUID_QPhi " +
                    "from tb_TimeFieldData order by ReceiveTime";
                }
                            
                Connection con = DBConnectUtil.getConnection();
                PreparedStatement pstmt = null;
                ResultSet resultSet = null;
                
                try {
                    //QPhi
                    pstmt = con.prepareStatement(sqlQPhi);
                    if(isExitQPhiDate.equals("是")){
                        pstmt.setString(1, beginTimeQPhi);
                    }
                    resultSet = pstmt.executeQuery();
                    
                    while(resultSet.next()){
                        //如果該監測點存在QPhi數據                            
                        DataRecord QPhiDr = new DataRecord();
                        QPhiDr.put("GUID", resultSet.getString(1));
                        QPhiDr.put("DeviceID", resultSet.getString(2));
                        QPhiDr.put("ReceiveTime", resultSet.getString(3));
                        QPhiDr.put("TimeStamp", resultSet.getString(4));
                        QPhiDr.put("PhaseDataA", resultSet.getBytes(5));
                        QPhiDr.put("PDDataA", resultSet.getBytes(6));
                        QPhiDr.put("PhaseDataB", resultSet.getBytes(7));
                        QPhiDr.put("PDDataB", resultSet.getBytes(8));
                        QPhiDr.put("PhaseDataC", resultSet.getBytes(9));
                        QPhiDr.put("PDDataC", resultSet.getBytes(10));
                        QPhiDr.put("PhaseFreq", resultSet.getString(11));
                        QPhiList.add(QPhiDr);        
                    }
                    
                    //若QPhiList集合存在值,則(添加/修改同步記錄進度--上傳最大截止時間)發送數據同步記錄到接收端                
                    if(QPhiList != null && QPhiList.size() > 0){
                        sysLogQPhi.put("isExitQPhiDate", isExitQPhiDate);
                        if(idQPhi != null ){
                            sysLogQPhi.put("ID", idQPhi);
                        }
                        sysLogQPhi.put("DATA_TYPE_KEY", DATA_TYPE_KEY_QPhi);
                        sysLogQPhi.put("SYN_PROGRESS", (QPhiList.get(QPhiList.size()-1)).getString("ReceiveTime"));                        
                    }    
                    
                    //PRPD
                    pstmt = con.prepareStatement(sqlPRPD);
                    
                    if(isExitPRPDDate.equals("是")){
                        pstmt.setString(1, beginTimePRPDD);
                    }
                    resultSet = pstmt.executeQuery();
                    
                    while(resultSet.next()){
                        //如果該監測點存在PRPD數據                            
                        DataRecord PRPDListDr = new DataRecord();
                        PRPDListDr.put("GUID", resultSet.getString(1));
                        PRPDListDr.put("DeviceID", resultSet.getString(2));
                        PRPDListDr.put("ReceiveTime", resultSet.getString(3));
                        PRPDListDr.put("TimeStamp", resultSet.getString(4));
                        PRPDListDr.put("PhaseDataA", resultSet.getBytes(5));
                        PRPDListDr.put("PRPDDataA", resultSet.getBytes(6));
                        PRPDListDr.put("PhaseDataB", resultSet.getBytes(7));
                        PRPDListDr.put("PRPDDataB", resultSet.getBytes(8));
                        PRPDListDr.put("PhaseDataC", resultSet.getBytes(9));
                        PRPDListDr.put("PRPDDataC", resultSet.getBytes(10));
                        PRPDList.add(PRPDListDr);        
                    }
                    
                    if(PRPDList != null && PRPDList.size() > 0){
                        sysLogPRPD.put("isExitPRPDDate", isExitPRPDDate);
                        if(idPRPD != null ){
                            sysLogPRPD.put("ID", idPRPD);
                        }
                        sysLogPRPD.put("DATA_TYPE_KEY", DATA_TYPE_KEY_PRPD);
                        sysLogPRPD.put("SYN_PROGRESS", (PRPDList.get(PRPDList.size()-1)).getString("ReceiveTime"));
                    }                
                    
                    //TimeField
                    pstmt = con.prepareStatement(sqlTimeField);
                    
                    if(isExitTimeFieldDate.equals("是")){
                        pstmt.setString(1, beginTimeTimeField);
                    }
                    resultSet = pstmt.executeQuery();
                    
                    while(resultSet.next()){
                        //如果該監測點存在PRPS數據                            
                        DataRecord PRPSListDr = new DataRecord();
                        PRPSListDr.put("GUID", resultSet.getString(1));
                        PRPSListDr.put("DeviceID", resultSet.getString(2));
                        PRPSListDr.put("ReceiveTime", resultSet.getString(3));
                        PRPSListDr.put("TimeStamp", resultSet.getString(4));
                        PRPSListDr.put("TimeFieldDataA", resultSet.getBytes(5));
                        PRPSListDr.put("TimeFieldDataB", resultSet.getBytes(6));
                        PRPSListDr.put("TimeFieldDataC", resultSet.getBytes(7));
                        PRPSListDr.put("GUID_QPhi", resultSet.getString(8));
                        PRPSList.add(PRPSListDr);        
                    }
                    
                    if(PRPSList != null && PRPSList.size() > 0){
                        sysLogTimeField.put("isExitTimeFieldDate", isExitTimeFieldDate);
                        if(idTimeField != null ){
                            sysLogTimeField.put("ID", idTimeField);
                        }
                        sysLogTimeField.put("DATA_TYPE_KEY", DATA_TYPE_KEY_TimeField);
                        sysLogTimeField.put("SYN_PROGRESS", (PRPSList.get(PRPSList.size()-1)).getString("ReceiveTime"));
                    }
                                
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if(resultSet != null){
                            resultSet.close();
                        }
                        if(pstmt != null){
                            pstmt.close();
                        }
                        if(con != null){
                            con.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                
            }
            
            // 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
            CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
            
            // 創建Post請求
            /*HttpPost httpPost = new HttpPost(Constants.TEST+"/biz/bureauDetect/uploadData.do?action=doPostReceiveData");    */           
            String port = SbpConfig.getProperty("sbp.jiangmen.visualWarningSystem.url");
            HttpPost httpPost = new HttpPost(port+"/biz/bureauDetect/uploadData.do?action=doPostReceiveData");    
            
            params.put("testId", testId);
            if(pdTestDr != null){
                params.put("pdTestDr", pdTestDr);
            }
            if(test_pointList.size() > 0){
                params.put("test_pointList", test_pointList);
            }
            if(placeCableDr != null){
                 params.put("placeCableDr", placeCableDr);
            }     
            if(test_cableList.size() > 0){
                params.put("test_cableList", test_cableList);
            }
            if(test_analysis_resultList.size() > 0){
                params.put("test_analysis_resultList", test_analysis_resultList);
            }
            if(device_cableList.size() > 0){
                params.put("device_cableList", device_cableList);
            }
            if(detection_deviceList.size() > 0){
                params.put("detection_deviceList", detection_deviceList);
            }
           
            if(QPhiList != null && QPhiList.size() > 0){
                params.put("QPhiList", QPhiList);
            }
            if(PRPDList != null && PRPDList.size() > 0){
                params.put("PRPDList", PRPDList);
            }
            if(PRPSList != null && PRPSList.size() > 0){
                params.put("PRPSList", PRPSList);
            }
            if(sysLogQPhi != null){
                params.put("sysLogQPhi", sysLogQPhi);
            }
            if(sysLogPRPD != null){
                params.put("sysLogPRPD", sysLogPRPD);
            }
            if(sysLogTimeField != null){
                params.put("sysLogTimeField", sysLogTimeField);
            }

            // 傳值時傳遞的是json字符串,c
            StringEntity entity = new StringEntity(params.toJSONString(), "UTF-8");
           // post請求是將參數放在請求體里面傳過去的;這里將entity放入post請求體中
            httpPost.setEntity(entity);              
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
             
            // 響應模型
            CloseableHttpResponse response = null;
            try {
                // 由客戶端執行(發送)Post請求
                response = httpClient.execute(httpPost);
                // 從響應模型中獲取響應實體
                HttpEntity responseEntity = response.getEntity();         
                
                System.out.println("響應狀態為:" + response.getStatusLine());

                if (responseEntity != null) {
                    System.out.println("響應內容長度為:" + responseEntity.getContentLength());
                    /*InputStream in =  responseEntity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                    String line = null;
                    StringBuilder sb = new StringBuilder();
                    while((line = br.readLine())!=null){
                        sb.append(line);
                    }       */
                    //將資料解碼
//                    System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
                    return EntityUtils.toString(responseEntity);
                }
           
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 釋放資源
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }                
    }
        return null;    
}

第四步:數據發送到對方服務器,對方服務器的controller:

    /**
     * 
     * @function 數據接收.
     * @author Liangjw  
     * @date 2019-10-11 上午11:01:13
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public void doPostReceiveData(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");        
        //創建一個鍵值對,返回前台:存放上傳后返回的結果
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        System.out.println("數據發送過來了!");
        //讀取請求內容................獲取POST請求:將請求的數據轉換成數據流
        //從request域里面讀取數據流,InputStreamReader把數據流用指定的字符集讀取字節,並解碼為字符;
        //通過BufferedReader 流的形式進行流緩存,之后通過readLine方法獲取到緩存的內容。    
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }       
        //將資料解碼
        String reqData = sb.toString();
        String result = null;
        if(reqData != null && !"".equals(reqData)) {
            JSONObject jsonObject = JSON.parseObject(reqData);            
            if(!jsonObject.isEmpty()) {
                result = service.receiveData(jsonObject);
            }     
        }
        if(result != null && !"".equals(result)) {            
            PrintWriter writer = response.getWriter();
            writer.write(result);
        }
    }

第五步:對方服務器中接收發送過來的數據,進行插入/更新等業務邏輯的操作,然后返回數據。

/**
     * 
     * @function 上傳數據接收端.
     * @author Liangjw
     * @date 2019-10-20 上午10:01:01  
     * @param json 上傳數據的json格式
     * @return 返回上傳結果:“上傳成功”或“上傳失敗”
     */
    @Override
    public String receiveData(JSONObject json) {
        if(json != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("TEST_ID", json.getString("testId"));    
            
            //.....插入電纜線路(位置)
            JSONObject pcDr = json.getJSONObject("placeCableDr");
            if(pcDr != null){
                DataRecord placeCableDr = new DataRecord();
                if(pcDr.getString("PLACE_ID") != null && !pcDr.getString("PLACE_ID").equals("")){
                    placeCableDr.put("PLACE_ID", pcDr.getString("PLACE_ID"));
                }
                if(pcDr.getString("PLACE_NAME") != null && !pcDr.getString("PLACE_NAME").equals("")){
                    placeCableDr.put("PLACE_NAME", pcDr.getString("PLACE_NAME"));
                }
                if(pcDr.getString("LAYING_START") != null && !pcDr.getString("LAYING_START").equals("")){
                    placeCableDr.put("LAYING_START", pcDr.getString("LAYING_START"));
                }
                if(pcDr.getString("LAYING_FINISH") != null && !pcDr.getString("LAYING_FINISH").equals("")){
                    placeCableDr.put("LAYING_FINISH", pcDr.getString("LAYING_FINISH"));
                }
                
                Map<String, Object> placeIdMap = new HashMap<String, Object>();
                placeIdMap.put("PLACE_ID", pcDr.getString("PLACE_ID"));
                DataRecord placeCable2Dr = this.getDao().queryForDataRecord(PATH + "getPlaceCableByPlaceId", placeIdMap);  //查詢可視化系統庫的電纜線路(位置)表
                if(placeCable2Dr != null){   //第一步:插入或修改電纜線路數據
                    this.getDao().update("DVC_PLACE_CABLE", placeCableDr, placeIdMap);                     
                }else{
                    this.getDao().insert("DVC_PLACE_CABLE", placeCableDr);              
                }        
            }
            
            //.....插入電纜局放檢測任務
            JSONObject testDr = json.getJSONObject("pdTestDr");
            if(testDr != null){
                DataRecord pdTestDr = new DataRecord();
                if(testDr.getString("TEST_ID") != null && !testDr.getString("TEST_ID").equals("")){
                    pdTestDr.put("TEST_ID", testDr.getString("TEST_ID"));
                }
                if(testDr.getString("PLACE_ID") != null && !testDr.getString("PLACE_ID").equals("")){
                    pdTestDr.put("PLACE_ID", testDr.getString("PLACE_ID"));
                }
                if(testDr.getString("PLACE_NAME") != null && !testDr.getString("PLACE_NAME").equals("")){
                    pdTestDr.put("PLACE_NAME", testDr.getString("PLACE_NAME"));
                }
                if(testDr.getString("WEATHER") != null && !testDr.getString("WEATHER").equals("")){
                    pdTestDr.put("WEATHER", testDr.getString("WEATHER"));
                }
                if(testDr.getString("TEMPERATURE") != null && !testDr.getString("TEMPERATURE").equals("")){
                    pdTestDr.put("TEMPERATURE", testDr.getString("TEMPERATURE"));
                }
                if(testDr.getString("HUMIDITY") != null && !testDr.getString("HUMIDITY").equals("")){
                    pdTestDr.put("HUMIDITY", testDr.getString("HUMIDITY"));
                }
                if(testDr.getString("CABLE_LENGTH") != null && !testDr.getString("CABLE_LENGTH").equals("")){
                    pdTestDr.put("CABLE_LENGTH", testDr.getString("CABLE_LENGTH"));
                }
                if(testDr.getDate("BEGIN_TIME") != null && !testDr.getDate("BEGIN_TIME").equals("")){
                    pdTestDr.put("BEGIN_TIME", testDr.getDate("BEGIN_TIME"));
                }
                if(testDr.getDate("END_TIME") != null && !testDr.getDate("END_TIME").equals("")){
                    pdTestDr.put("END_TIME", testDr.getDate("END_TIME"));
                }
                if(testDr.getString("CAR_NUMBER") != null && !testDr.getString("CAR_NUMBER").equals("")){
                    pdTestDr.put("CAR_NUMBER", testDr.getString("CAR_NUMBER"));
                }
                if(testDr.getString("LONGITUDE") != null && !testDr.getString("LONGITUDE").equals("")){
                    pdTestDr.put("LONGITUDE", testDr.getString("LONGITUDE"));
                }
                if(testDr.getString("LATITUDE") != null && !testDr.getString("LATITUDE").equals("")){
                    pdTestDr.put("LATITUDE", testDr.getString("LATITUDE"));
                }
                if(testDr.getString("TESTERS") != null && !testDr.getString("TESTERS").equals("")){
                    pdTestDr.put("TESTERS", testDr.getString("TESTERS"));
                }
                if(testDr.getString("TEST_STATE") != null && !testDr.getString("TEST_STATE").equals("")){
                    pdTestDr.put("TEST_STATE", testDr.getString("TEST_STATE"));
                }
                if(testDr.getString("UPLOAD_STATE") != null && !testDr.getString("UPLOAD_STATE").equals("")){
                    pdTestDr.put("UPLOAD_STATE", testDr.getString("UPLOAD_STATE"));
                }
                if(testDr.getString("DISCHARGE") != null && !testDr.getString("DISCHARGE").equals("")){
                    pdTestDr.put("DISCHARGE", testDr.getString("DISCHARGE"));
                }
                                                
                DataRecord pdTest2Dr = this.getDao().queryForDataRecord(PATH + "getPDTestByTestId", map);  //查詢可視化系統庫的檢測任務表
                if(pdTest2Dr != null){                        
                    this.getDao().update("PD_TEST", pdTestDr, map);  //修改檢測任務數據,第一個人參數:表名,第二個參數:修改的DataRecord類型的數據,第三個參數:表的主鍵id    
                }else{
                    this.getDao().insert("PD_TEST", pdTestDr);  //插入新的檢測任務數據,第一個人參數:表名,第二個參數:插入表的DataRecord類型的數據        
                }
            }
            
            //局放檢測裝置map
            Map<String, Object> equipmentIdsMap = new HashMap<String, Object>();
            String equipmentIds = "";
            
            //.....插入監測點
            List<JSONObject> test_pointList = json.getJSONArray("test_pointList").toJavaList(JSONObject.class);
            if(test_pointList != null && test_pointList.size() > 0){
                List<DataRecord> test_pointList2 = new ArrayList<DataRecord>();
                List<DataRecord> test_point2List = this.getDao().queryForDataSet(PATH + "getTestPointByTestId", map).getResults();  //查詢可視化系統庫的監測點表
                for(JSONObject testPointDr2 : test_pointList){
                    DataRecord testPointDr = new DataRecord();
                    if(testPointDr2.getString("TEST_POING_ID") != null && !testPointDr2.getString("TEST_POING_ID").equals("")){
                        testPointDr.put("TEST_POING_ID", testPointDr2.getString("TEST_POING_ID"));
                    }
                    if(testPointDr2.getString("TEST_ID") != null && !testPointDr2.getString("TEST_ID").equals("")){
                        testPointDr.put("TEST_ID", testPointDr2.getString("TEST_ID"));
                    }
                    if(testPointDr2.getString("EQUIPMENT_ID") != null && !testPointDr2.getString("EQUIPMENT_ID").equals("")){
                        testPointDr.put("EQUIPMENT_ID", testPointDr2.getString("EQUIPMENT_ID"));
                    }
                    if(testPointDr2.getString("EQUIPMENT_NAME") != null && !testPointDr2.getString("EQUIPMENT_NAME").equals("")){
                        testPointDr.put("EQUIPMENT_NAME", testPointDr2.getString("EQUIPMENT_NAME"));
                    }
                    if(testPointDr2.getString("CMD_ID") != null && !testPointDr2.getString("CMD_ID").equals("")){
                        testPointDr.put("CMD_ID", testPointDr2.getString("CMD_ID"));
                    }
                    if(testPointDr2.getString("PLACE_ID") != null && !testPointDr2.getString("PLACE_ID").equals("")){
                        testPointDr.put("PLACE_ID", testPointDr2.getString("PLACE_ID"));
                    }
                    if(testPointDr2.getString("PLACE_NAME") != null && !testPointDr2.getString("PLACE_NAME").equals("")){
                        testPointDr.put("PLACE_NAME", testPointDr2.getString("PLACE_NAME"));
                    }
                    if(testPointDr2.getString("TEST_LOCATION") != null && !testPointDr2.getString("TEST_LOCATION").equals("")){
                        testPointDr.put("TEST_LOCATION", testPointDr2.getString("TEST_LOCATION"));
                    }
                    if(testPointDr2.getString("ORDER_NUMBER") != null && !testPointDr2.getString("ORDER_NUMBER").equals("")){
                        testPointDr.put("ORDER_NUMBER", testPointDr2.getString("ORDER_NUMBER"));
                    }
                    
                    test_pointList2.add(testPointDr);
                }
                
                if(test_pointList2.size() > 0){
                    for(DataRecord test_PointDr : test_pointList2){
                        String tempId2 = test_PointDr.getLong("EQUIPMENT_ID").toString()+",";
                        equipmentIds += tempId2;                    
                    }
                }
                equipmentIds=(equipmentIds.substring(0,equipmentIds.length()-1)).trim();    
                equipmentIdsMap.put("equipmentIds", equipmentIds);
                
                if(test_point2List != null && test_point2List.size() > 0){
                    for(DataRecord testPointDr : test_pointList2){
                        Map<String, Object> testPointMap = new HashMap<String, Object>();
                        testPointMap.put("TEST_POING_ID", testPointDr.getLong("TEST_POING_ID"));
                        this.getDao().update("PD_TEST_POINT", testPointDr, testPointMap);  //修改監測點數據,第一個人參數:表名,第二個參數:修改的DataRecord類型的數據,第三個參數:表的主鍵id
                    }        
                }else{
                    this.getDao().insertBatch(test_pointList2, "PD_TEST_POINT");//插入新的監測點數據,第一個人參數:插入表的List<DataRecord>類型的數據,第二個參數:表名                
                }
            }
            
            //.....插入檢測電纜數據
            List<JSONObject> test_cableList = json.getJSONArray("test_cableList").toJavaList(JSONObject.class);
            if(test_cableList != null && test_cableList.size() > 0){
                List<DataRecord> test_cable2List = this.getDao().queryForDataSet(PATH + "getTestCableByPlaceId", map).getResults();  //查詢可視化系統庫的檢測電纜表
                if(test_cable2List != null && test_cable2List.size() > 0){
                    for(JSONObject testCableDr2 : test_cableList){
                        DataRecord testCableDr = new DataRecord();
                        if(testCableDr2.getString("TEST_CABLE_ID") != null && !testCableDr2.getString("TEST_CABLE_ID").equals("")){
                            testCableDr.put("TEST_CABLE_ID", testCableDr2.getString("TEST_CABLE_ID"));
                        }
                        if(testCableDr2.getString("TEST_ID") != null && !testCableDr2.getString("TEST_ID").equals("")){
                            testCableDr.put("TEST_ID", testCableDr2.getString("TEST_ID"));
                        }
                        if(testCableDr2.getString("DEV_ID") != null && !testCableDr2.getString("DEV_ID").equals("")){
                            testCableDr.put("DEV_ID", testCableDr2.getString("DEV_ID"));
                        }
                        
                        Map<String, Object> testCableMap = new HashMap<String, Object>();
                        testCableMap.put("TEST_CABLE_ID", testCableDr2.getString("TEST_CABLE_ID"));
                        this.getDao().update("PD_TEST_CABLE", testCableDr, testCableMap);  //修改檢測電纜數據,第一個人參數:表名,第二個參數:修改的DataRecord類型的數據,第三個參數:表的主鍵id
                    }        
                }else{
                    List<DataRecord> test_cableList2 = new ArrayList<DataRecord>();
                    for(JSONObject testCableDr2 : test_cableList){
                        DataRecord testCableDr = new DataRecord();
                        if(testCableDr2.getString("TEST_CABLE_ID") != null && !testCableDr2.getString("TEST_CABLE_ID").equals("")){
                            testCableDr.put("TEST_CABLE_ID", testCableDr2.getString("TEST_CABLE_ID"));
                        }
                        if(testCableDr2.getString("TEST_ID") != null && !testCableDr2.getString("TEST_ID").equals("")){
                            testCableDr.put("TEST_ID", testCableDr2.getString("TEST_ID"));
                        }
                        if(testCableDr2.getString("DEV_ID") != null && !testCableDr2.getString("DEV_ID").equals("")){
                            testCableDr.put("DEV_ID", testCableDr2.getString("DEV_ID"));
                        }
                        /*testCableDr.put("TEST_CABLE_ID", testCableDr2.getString("TEST_CABLE_ID"));
                        testCableDr.put("TEST_ID", testCableDr2.getString("TEST_ID"));
                        testCableDr.put("DEV_ID", testCableDr2.getString("DEV_ID"));*/
                        
                        test_cableList2.add(testCableDr);
                    }
                    this.getDao().insertBatch(test_cableList2, "PD_TEST_CABLE");//插入新的監測點數據,第一個人參數:插入表的List<DataRecord>類型的數據,第二個參數:表名                
                }
            }
            
            //....電纜設備id集map
            Map<String, Object> deviceIdsMap = new HashMap<String, Object>();
            String ids = "";
            
            //.....插入電纜局放檢測分析結果
            List<JSONObject> test_analysis_resultList = json.getJSONArray("test_analysis_resultList").toJavaList(JSONObject.class);
            if(test_analysis_resultList != null && test_analysis_resultList.size() > 0){
                List<DataRecord> test_analysis_result2List = this.getDao().queryForDataSet(PATH + "getTestAnalysisResultByTestId", map).getResults();  //查詢可視化系統庫的電纜局放檢測分析結果
                List<DataRecord> test_resultList2 = new ArrayList<DataRecord>();
                for(JSONObject testResultDr2 : test_analysis_resultList){
                    DataRecord testResultDr = new DataRecord();
                    if(testResultDr2.getString("ID") != null && !testResultDr2.getString("ID").equals("")){
                        testResultDr.put("ID", testResultDr2.getString("ID"));
                    }
                    if(testResultDr2.getString("TEST_ID") != null && !testResultDr2.getString("TEST_ID").equals("")){
                        testResultDr.put("TEST_ID", testResultDr2.getString("TEST_ID"));
                    }
                    if(testResultDr2.getString("DEV_ID") != null && !testResultDr2.getString("DEV_ID").equals("")){
                        testResultDr.put("DEV_ID", testResultDr2.getString("DEV_ID"));
                    }
                    if(testResultDr2.getString("DEV_NAME") != null && !testResultDr2.getString("DEV_NAME").equals("")){
                        testResultDr.put("DEV_NAME", testResultDr2.getString("DEV_NAME"));
                    }
                    if(testResultDr2.getString("PHASE") != null && !testResultDr2.getString("PHASE").equals("")){
                        testResultDr.put("PHASE", testResultDr2.getString("PHASE"));
                    }
                    if(testResultDr2.getString("PLACE_ID") != null && !testResultDr2.getString("PLACE_ID").equals("")){
                        testResultDr.put("PLACE_ID", testResultDr2.getString("PLACE_ID"));
                    }
                    if(testResultDr2.getString("PLACE_NAME") != null && !testResultDr2.getString("PLACE_NAME").equals("")){
                        testResultDr.put("PLACE_NAME", testResultDr2.getString("PLACE_NAME"));
                    }
                    if(testResultDr2.getString("DISCHARGE") != null && !testResultDr2.getString("DISCHARGE").equals("")){
                        testResultDr.put("DISCHARGE", testResultDr2.getString("DISCHARGE"));
                    }
                    if(testResultDr2.getString("MAX_PD") != null && !testResultDr2.getString("MAX_PD").equals("")){
                        testResultDr.put("MAX_PD", testResultDr2.getString("MAX_PD"));
                    }
                    if(testResultDr2.getString("DISCHARGE_LOCATION") != null && !testResultDr2.getString("DISCHARGE_LOCATION").equals("")){
                        testResultDr.put("DISCHARGE_LOCATION", testResultDr2.getString("DISCHARGE_LOCATION"));
                    }
                    if(testResultDr2.getString("REASON") != null && !testResultDr2.getString("REASON").equals("")){
                        testResultDr.put("REASON", testResultDr2.getString("REASON"));
                    }
                    if(testResultDr2.getString("POSITION") != null && !testResultDr2.getString("POSITION").equals("")){
                        testResultDr.put("POSITION", testResultDr2.getString("POSITION"));
                    }
                    if(testResultDr2.getDate("TEST_TIME") != null && !testResultDr2.getDate("TEST_TIME").equals("")){
                        testResultDr.put("TEST_TIME", testResultDr2.getDate("TEST_TIME"));
                    }
                    if(testResultDr2.getString("PD_STATE") != null && !testResultDr2.getString("PD_STATE").equals("")){
                        testResultDr.put("PD_STATE", testResultDr2.getString("PD_STATE"));
                    }
                    
                    test_resultList2.add(testResultDr);
                    
                }
                
                if(test_resultList2.size() > 0){
                    for(DataRecord arDr : test_resultList2){
                        String tempId = arDr.getLong("DEV_ID").toString()+",";
                        ids += tempId;                    
                    }
                }
                ids=(ids.substring(0,ids.length()-1)).trim();    
                deviceIdsMap.put("ids", ids);
                
                if(test_analysis_result2List != null && test_analysis_result2List.size() > 0){  //可視化已存在數據:修改
                    for(DataRecord testResultDr : test_resultList2){
                        Map<String, Object> testResultMap = new HashMap<String, Object>();
                        testResultMap.put("ID", testResultDr.getString("ID"));
                        this.getDao().update("PD_TEST_ANALYSIS_RESULT", testResultDr, testResultMap);  
                    }        
                }else{   //可視化不存在數據:新增
                    for(DataRecord testResultDr : test_resultList2){
                        this.getDao().insert("PD_TEST_ANALYSIS_RESULT", testResultDr);
                    }
                    
                /*    this.getDao().insertBatch(test_resultList2, "PD_TEST_ANALYSIS_RESULT2");        */
                }
            }
            
            //插入電纜設備
            List<JSONObject> device_cableList = json.getJSONArray("device_cableList").toJavaList(JSONObject.class);
            if(device_cableList != null && device_cableList.size() > 0){
                List<DataRecord> dev_cableList2 = new ArrayList<DataRecord>();
                for(JSONObject dev_cable : device_cableList){
                    DataRecord dev_cableDr = new DataRecord();
                    if(dev_cable.getString("DEV_ID") != null && !dev_cable.getString("DEV_ID").equals("")){
                        dev_cableDr.put("DEV_ID", dev_cable.getString("DEV_ID"));
                    }
                    if(dev_cable.getString("DEV_NAME") != null && !dev_cable.getString("DEV_NAME").equals("")){
                        dev_cableDr.put("DEV_NAME", dev_cable.getString("DEV_NAME"));
                    }
                    if(dev_cable.getString("PHASE") != null && !dev_cable.getString("PHASE").equals("")){
                        dev_cableDr.put("PHASE", dev_cable.getString("PHASE"));
                    }
                    if(dev_cable.getString("NOMINAL_AREA") != null && !dev_cable.getString("NOMINAL_AREA").equals("")){
                        dev_cableDr.put("NOMINAL_AREA", dev_cable.getString("NOMINAL_AREA"));
                    }
                    if(dev_cable.getString("POWER_LEVEL") != null && !dev_cable.getString("POWER_LEVEL").equals("")){
                        dev_cableDr.put("POWER_LEVEL", dev_cable.getString("POWER_LEVEL"));
                    }
                    if(dev_cable.getString("STRUCTURE_TYPE") != null && !dev_cable.getString("STRUCTURE_TYPE").equals("")){
                        dev_cableDr.put("STRUCTURE_TYPE", dev_cable.getString("STRUCTURE_TYPE"));
                    }
                    if(dev_cable.getString("INSULATION") != null && !dev_cable.getString("INSULATION").equals("")){
                        dev_cableDr.put("INSULATION", dev_cable.getString("INSULATION"));
                    }
                    if(dev_cable.getString("WIRE_CORE_NUM") != null && !dev_cable.getString("WIRE_CORE_NUM").equals("")){
                        dev_cableDr.put("WIRE_CORE_NUM", dev_cable.getString("WIRE_CORE_NUM"));
                    }
                    if(dev_cable.getString("LAYING_ENVIRONMENT") != null && !dev_cable.getString("LAYING_ENVIRONMENT").equals("")){
                        dev_cableDr.put("LAYING_ENVIRONMENT", dev_cable.getString("LAYING_ENVIRONMENT"));
                    }
                    if(dev_cable.getString("LENGTH") != null && !dev_cable.getString("LENGTH").equals("")){
                        dev_cableDr.put("LENGTH", dev_cable.getString("LENGTH"));
                    }
                    if(dev_cable.getString("PLACE_ID") != null && !dev_cable.getString("PLACE_ID").equals("")){
                        dev_cableDr.put("PLACE_ID", dev_cable.getString("PLACE_ID"));
                    }
                    
                    dev_cableList2.add(dev_cableDr);
                }
                List<DataRecord> device_cable2List = this.getDao().queryForDataSet(PATH + "getDeviceCableByDeviceId", deviceIdsMap).getResults();  //查詢可視化系統庫的電纜設備
                if(device_cable2List != null && device_cable2List.size() > 0){
                    for(DataRecord deviceCableDr : dev_cableList2){
                        Map<String, Object> deviceIdMap = new HashMap<String, Object>();
                        deviceIdMap.put("DEV_ID", deviceCableDr.getLong("DEV_ID"));
                        this.getDao().update("DVC_DEVICE_CABLE", deviceCableDr, deviceIdMap);  
                    }    
                }else{
                    this.getDao().insertBatch(dev_cableList2, "DVC_DEVICE_CABLE");
                }
            }
            
            //插入局放檢測裝置
            List<JSONObject> detection_deviceList = json.getJSONArray("detection_deviceList").toJavaList(JSONObject.class);
            if(detection_deviceList != null && detection_deviceList.size() > 0){
                List<DataRecord> detection_deviceList2 = new ArrayList<DataRecord>();
                for(JSONObject detection_device : detection_deviceList){
                    DataRecord detection_deviceDr = new DataRecord();
                    if(detection_device.getString("EQUIPMENT_ID") != null && !detection_device.getString("EQUIPMENT_ID").equals("")){
                        detection_deviceDr.put("EQUIPMENT_ID", detection_device.getString("EQUIPMENT_ID"));
                    }
                    if(detection_device.getString("EQUIPMENT_NAME") != null && !detection_device.getString("EQUIPMENT_NAME").equals("")){
                        detection_deviceDr.put("EQUIPMENT_NAME", detection_device.getString("EQUIPMENT_NAME"));
                    }
                    if(detection_device.getString("CMD_ID") != null && !detection_device.getString("CMD_ID").equals("")){
                        detection_deviceDr.put("CMD_ID", detection_device.getString("CMD_ID"));
                    }
                    if(detection_device.getString("USE_STATE") != null && !detection_device.getString("USE_STATE").equals("")){
                        detection_deviceDr.put("USE_STATE", detection_device.getString("USE_STATE"));
                    }
                    if(detection_device.getString("CONNECT_STATE") != null && !detection_device.getString("CONNECT_STATE").equals("")){
                        detection_deviceDr.put("CONNECT_STATE", detection_device.getString("CONNECT_STATE"));
                    }
                    
                    detection_deviceList2.add(detection_deviceDr);
                }
                List<DataRecord> detection_device2List = this.getDao().queryForDataSet(PATH + "getDetectionDeviceByEquipmentIds", equipmentIdsMap).getResults();  //查詢可視化系統庫的局放檢測裝置
                if(detection_device2List.size() > 0){
                    for(DataRecord detectionDeviceDr : detection_deviceList2){
                        Map<String, Object> detectionDeviceIdMap = new HashMap<String, Object>();
                        detectionDeviceIdMap.put("EQUIPMENT_ID", detectionDeviceDr.getLong("EQUIPMENT_ID"));
                        this.getDao().update("PD_DETECTION_DEVICE", detectionDeviceDr, detectionDeviceIdMap);  
                    }    
                }else{
                    this.getDao().insertBatch(detection_deviceList2, "PD_DETECTION_DEVICE");
                }
            }
            
            //接收QPhi集
            List<JSONObject> QPhiList = new ArrayList<JSONObject>();
            if(json.containsKey("QPhiList") == true){
                QPhiList = json.getJSONArray("QPhiList").toJavaList(JSONObject.class);
            }
            
            //接收PRPD集
            List<JSONObject> PRPDList = new ArrayList<JSONObject>();
            if(json.containsKey("PRPDList") == true){
                PRPDList = json.getJSONArray("PRPDList").toJavaList(JSONObject.class);
            }
            
            //接收TimeField集
            List<JSONObject> PRPSList = new ArrayList<JSONObject>();
            if(json.containsKey("PRPSList") == true){
                PRPSList = json.getJSONArray("PRPSList").toJavaList(JSONObject.class);
            }            
            
            Connection con = DBConnectUtil.getConnection();
            PreparedStatement pstmt = null;
            ResultSet resultSet = null;
            
            JSONObject sysLogQPhi = json.getJSONObject("sysLogQPhi");
            Boolean flagQPhi = false;
            if(sysLogQPhi != null){
                if("是".equals(sysLogQPhi.getString("ISEXITQPHIDATE"))){
                    flagQPhi = true;
                }
            }
            DataRecord systemLogQPhi = new DataRecord();
            Map<String, Object> mapQPhi = new HashMap<String, Object>();
            
            JSONObject sysLogPRPD = json.getJSONObject("sysLogPRPD");
            Boolean flagPRPD = false;
            if(sysLogPRPD != null){
                if("是".equals(sysLogPRPD.getString("ISEXITPRPDDATE"))){
                    flagPRPD = true;
                }
            }
            DataRecord systemLogPRPD = new DataRecord();
            Map<String, Object> mapPRPD = new HashMap<String, Object>();
            
            
            JSONObject sysLogTimeField = json.getJSONObject("sysLogTimeField");
            Boolean flagTimeField = false;
            if(sysLogTimeField != null){
                if("是".equals(sysLogTimeField.getString("ISEXITTIMEFIELDDATE"))){
                    flagTimeField = true;
                }
            }
            DataRecord systemLogTimeField = new DataRecord();
            Map<String, Object> mapTimeField = new HashMap<String, Object>();
            
            try {
                //QPhi
                if(QPhiList != null && QPhiList.size() > 0){
                    String insertSqlQPhi = "insert into tb_QPhiData(GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PDDataA, PhaseDataB, PDDataB, PhaseDataC, PDDataC, PhaseFreq) values(?, ?, ? , ? , ? , ? , ? , ? , ? , ?, ?)";
                    pstmt = con.prepareStatement(insertSqlQPhi);
                    con.setAutoCommit(false);
                    for(JSONObject QPhi : QPhiList){
                        pstmt.setString(1, QPhi.getString("GUID"));
                        pstmt.setString(2, QPhi.getString("DEVICEID"));
                        pstmt.setString(3, QPhi.getString("RECEIVETIME"));
                        pstmt.setString(4, QPhi.getString("TIMESTAMP"));
                        pstmt.setBytes(5, QPhi.getBytes("PHASEDATAA"));
                        pstmt.setBytes(6, QPhi.getBytes("PDDATAA"));
                        pstmt.setBytes(7, QPhi.getBytes("PHASEDATAB"));
                        pstmt.setBytes(8, QPhi.getBytes("PDDATAB"));
                        pstmt.setBytes(9, QPhi.getBytes("PHASEDATAC"));
                        pstmt.setBytes(10, QPhi.getBytes("PDDATAC"));
                        pstmt.setString(11, QPhi.getString("PHASEFREQ"));
                        /*pstmt.executeUpdate();    */  //一條一條的插入
                        pstmt.addBatch();                      //批量插入
                    }
                    pstmt.executeBatch();
                    con.commit();
                    if(flagQPhi){
                        mapQPhi.put("ID", sysLogQPhi.getString("ID"));
                        mapQPhi.put("DATA_TYPE_KEY", sysLogQPhi.getString("DATA_TYPE_KEY"));
                        mapQPhi.put("SYN_PROGRESS", sysLogQPhi.getString("SYN_PROGRESS"));
                        this.getDao().updateByStatement(PATH + "editSysSynLOG", mapQPhi);
                    }else{
                        systemLogQPhi.put("ID", this.getDao().generateId());
                        systemLogQPhi.put("DATA_TYPE_KEY", sysLogQPhi.getString("DATA_TYPE_KEY"));
                        systemLogQPhi.put("SYN_PROGRESS", sysLogQPhi.getString("SYN_PROGRESS"));
                        this.getDao().insert("SYS_SYN_LOG", systemLogQPhi);
                    }
                }
                
                //PRPD
                if(PRPDList != null && PRPDList.size() > 0){
                    String insertSqldeleteSqlPRPD = "insert into tb_PRPDData(GUID, DeviceID, ReceiveTime, TimeStamp, PhaseDataA, PRPDDataA, PhaseDataB, PRPDDataB, PhaseDataC, PRPDDataC) values(?, ?, ? , ? , ? , ? , ? , ? , ? , ?)";
                    pstmt = con.prepareStatement(insertSqldeleteSqlPRPD);
                    con.setAutoCommit(false);
                    for(JSONObject prpd : PRPDList){
                        pstmt.setString(1, prpd.getString("GUID"));
                        pstmt.setString(2, prpd.getString("DEVICEID"));
                        pstmt.setString(3, prpd.getString("RECEIVETIME"));
                        pstmt.setString(4, prpd.getString("TIMESTAMP"));
                        pstmt.setBytes(5, prpd.getBytes("PHASEDATAA"));
                        pstmt.setBytes(6, prpd.getBytes("PRPDDATAA"));
                        pstmt.setBytes(7, prpd.getBytes("PHASEDATAB"));
                        pstmt.setBytes(8, prpd.getBytes("PRPDDATAB"));
                        pstmt.setBytes(9, prpd.getBytes("PHASEDATAC"));
                        pstmt.setBytes(10, prpd.getBytes("PRPDDATAC"));
                        /*pstmt.executeUpdate();    */
                        pstmt.addBatch();        
                    }
                    pstmt.executeBatch();
                    con.commit();
                    if(flagPRPD){
                        mapPRPD.put("ID", sysLogPRPD.getString("ID"));
                        mapPRPD.put("DATA_TYPE_KEY", sysLogPRPD.getString("DATA_TYPE_KEY"));
                        mapPRPD.put("SYN_PROGRESS", sysLogPRPD.getString("SYN_PROGRESS"));
                        this.getDao().updateByStatement(PATH + "editSysSynLOG", mapPRPD);
                    }else{
                        systemLogPRPD.put("ID", this.getDao().generateId());
                        systemLogPRPD.put("DATA_TYPE_KEY", sysLogPRPD.getString("DATA_TYPE_KEY"));
                        systemLogPRPD.put("SYN_PROGRESS", sysLogPRPD.getString("SYN_PROGRESS"));
                        this.getDao().insert("SYS_SYN_LOG", systemLogPRPD);
                    }
                }
                
                //TimeField
                if(PRPSList != null && PRPSList.size() > 0){
                    String insertSqldeleteSqlPRPS = "insert into tb_TimeFieldData(GUID, DeviceID, ReceiveTime, TimeStamp, TimeFieldDataA, TimeFieldDataB, TimeFieldDataC, GUID_QPhi) values(?, ?, ? , ? , ? , ? , ? , ? )";
                    pstmt = con.prepareStatement(insertSqldeleteSqlPRPS);
                    con.setAutoCommit(false);
                    for(JSONObject prprs : PRPSList){
                        pstmt.setString(1, prprs.getString("GUID"));
                        pstmt.setString(2, prprs.getString("DEVICEID"));
                        pstmt.setString(3, prprs.getString("RECEIVETIME"));
                        pstmt.setString(4, prprs.getString("TIMESTAMP"));
                        pstmt.setBytes(5, prprs.getBytes("TIMEFIELDDATAA"));
                        pstmt.setBytes(6, prprs.getBytes("TIMEFIELDDATAB"));
                        pstmt.setBytes(7, prprs.getBytes("TIMEFIELDDATAC"));
                        pstmt.setString(8, prprs.getString("GUID_QPHI"));
                        /*pstmt.executeUpdate();        */
                        pstmt.addBatch();        
                    }
                    pstmt.executeBatch();
                    con.commit();
                    if(flagTimeField){
                        mapTimeField.put("ID", sysLogTimeField.getString("ID"));
                        mapTimeField.put("DATA_TYPE_KEY", sysLogTimeField.getString("DATA_TYPE_KEY"));
                        mapTimeField.put("SYN_PROGRESS", sysLogTimeField.getString("SYN_PROGRESS"));
                        this.getDao().updateByStatement(PATH + "editSysSynLOG", mapTimeField);
                    }else{
                        systemLogTimeField.put("ID", this.getDao().generateId());
                        systemLogTimeField.put("DATA_TYPE_KEY", sysLogTimeField.getString("DATA_TYPE_KEY"));
                        systemLogTimeField.put("SYN_PROGRESS", sysLogTimeField.getString("SYN_PROGRESS"));
                        this.getDao().insert("SYS_SYN_LOG", systemLogTimeField);
                    }
                }
                
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(resultSet != null){
                        resultSet.close();
                    }
                    if(pstmt != null){
                        pstmt.close();
                    }
                    if(con != null){
                        con.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            return "上傳成功";
        }
        return "上傳失敗";
    }

第六步:還需要你在雙方后台配置文件配置不攔截對方的請求路徑(可以只配置這個包下的這個接口的請求路徑不攔截!)

注:雙方都要配置不設防該請求的攔截。

 

前端調用代碼:

<a href="javascript: uploadData();" class="xs-btn btn_a" style="margin-left: 10px; ">上傳數據</a>
//JavaScirpt部分:
     //
上傳數據 function uploadData(){ var testId = $("#TEST_ID").val(); $.ajax({ type: "post", dataType: 'text', url: "<c:url value='/ajax'/>", data: { service: 'com.shd.biz.bureauDetect.uploadData.service.uploadDataService', method: 'uploadData', params: [testId] }, beforeSend: function(){ // 請求后的等待界面 $("#ajaxProgressDiv").css("display", "block"); }, success: function(data){ $("#ajaxProgressDiv").css("display", "none"); alert(data.substring(16,20)); } , error: function(XMLHttpRequest, textStatus, errorThrown){ //查看錯誤信息 alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); } }) }

 

好了,完事,其實實現步驟不難,理解后就發現挺簡單的。(上面的內容之所以很長,主要是我的原代碼需要處理的數據的有點多(關聯的表數據太多了),哈哈哈哈~)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM