java 跨系统调用接口接收数据/发送数据

使用java标准类HttpURLConnection

1、定义公共接口方法

   public static JSONObject httpRequest2(String requestUrl, String requestMethod, String outputStr) {
	 		JSONObject jsonObject = null;
	 		StringBuffer buffer = new StringBuffer();
	 		HttpURLConnection httpUrlConn = null;
	 		try {
	 			// 创建SSLContext对象,并使用我们指定的信任管理器初始化
	 			URL url = new URL(requestUrl);
	 			httpUrlConn = (HttpURLConnection) url.openConnection();
	 			httpUrlConn.setDoOutput(true);
	 			httpUrlConn.setDoInput(true);
	 			httpUrlConn.setUseCaches(false);
	 			httpUrlConn.setRequestProperty("Accept", "application/json");
	 			httpUrlConn.setRequestProperty("Content-Type", "application/json");
	 			// 设置请求方式(GET/POST)
	 			httpUrlConn.setRequestMethod(requestMethod);
	 			if ("GET".equalsIgnoreCase(requestMethod))
	 				httpUrlConn.connect();

	 			// 当有数据需要提交时
	 			if (null != outputStr) {
	 				OutputStream outputStream = httpUrlConn.getOutputStream();
	 				// 注意编码格式,防止中文乱码
	 				outputStream.write(outputStr.getBytes("UTF-8"));
	 				outputStream.close();
	 			}

	 			// 将返回的输入流转换成字符串
	 			InputStream inputStream = httpUrlConn.getInputStream();
	 			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
	 			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

	 			String str = null;
	 			while ((str = bufferedReader.readLine()) != null) {
	 				buffer.append(str);
	 			}
	 			bufferedReader.close();
	 			inputStreamReader.close();
	 			// 释放资源
	 			inputStream.close();
	 			inputStream = null;
	 			httpUrlConn.disconnect();
	 			System.out.println(buffer.toString());
	 			jsonObject = JSONObject.parseObject(buffer.toString());
	 			// jsonObject = JSONObject.fromObject(buffer.toString());
	 		} catch (ConnectException ce) {
	 			LogUtil.info("Weixin server connection timed out.");
	 		} catch (Exception e) {
	 			e.printStackTrace();
	 			org.jeecgframework.core.util.LogUtil.info("https request error:{}" + e.getMessage());
	 		} finally {
	 			try {
	 				httpUrlConn.disconnect();
	 			} catch (Exception e) {
	 				e.printStackTrace();
	 				org.jeecgframework.core.util.LogUtil.info("http close error:{}" + e.getMessage());
	 			}
	 		}
	 		return jsonObject;
	 	}

2、后台发送数据

	// 延期功能
	@RequestMapping(params = "doyanqi")
	@ResponseBody
	public AjaxJson doyanqi(String contractproto,String contractStarttime,String contractEndtime,String contractReason, HttpServletRequest req) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "延期成功";
		try{

			TSUser user = ResourceUtil.getSessionUser();
			
			CommonContractEntity contract1 = new CommonContractEntity();
			CommonContractEntity contract = commonContractService.findUniqueByProperty(CommonContractEntity.class, "contractproto", contractproto);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒
			if(StringUtils.isNotEmpty(contractStarttime)&&StringUtils.isNotEmpty(contractEndtime)){
				
				Date contractStarttimeDate = sdf.parse(contractStarttime);
				Date contractEndtimeDate = sdf.parse(contractEndtime);
				int fjcount =0;
				if("1".equals(contract.getContractHtstate())) {		//正式合同:需要上传附件后才能延期
					if(StringUtil.isNotEmpty(contract.getFjcount())) {
						fjcount = contract.getFjcount();
					}
					
					List<Map<String, Object>> l = jdbcTemplate.queryForList("SELECT * FROM t_s_attachment WHERE fid='"+contract.getId()+"'");
					if(l.size() <= fjcount) {	//查询当前附件个数,如果当前附件个数不大于之前记录附件个数,表示未上传附件,不可做延期!
						j.setMsg("正式合同请上传附件个数后,再做延期!");
						j.setSuccess(false);
						return j;
					}else {
						fjcount = l.size();
					}
					
				}
				
				
				if("2".equals(contract.getContractHtstate())){
					//只有临时合同才需要 30天判断
					
					long start = contractStarttimeDate.getTime();
					long end = contractEndtimeDate.getTime();

					int betweenDays = (int) (Math.abs(end - start)/(24*3600*1000));
					
					if(betweenDays>30){
						j.setMsg("延期临时合同周期须在一个月以内");
						j.setSuccess(false);
						return j;
					}
					
				}
				
				contract1.setContractproto(contractproto);
				contract1.setContractStarttime(contractStarttimeDate); 	//开始时间
				contract1.setContractEndtime(contractEndtimeDate); 	//结束时间
				contract1.setCreateName(user.getRealName());
				contract1.setContractReason(contractReason); 	//延期原因
				contract1.setContractCompanyValue(contract.getContractCompanyValue());
				
				List<CommonContractEntity> contractList = new ArrayList<>();
				contractList.add(contract1);
				String url = com.jeecg.common.urlConfig.doyanqildserver;
				net.sf.json.JSONArray jsonarray = net.sf.json.JSONArray.fromObject(contractList);
				JSONObject json = new JSONObject();
				json.put("contractList", jsonarray.toString());
				JSONObject resp= httpRequest2(url, "POST", json.toString());
				
				String result=resp.getString("result").toString();
				if("error".equals(result)) {
					throw new RuntimeException("延期失败,该合同不在xxxxx平台!");
				}else if("success".equals(result)) {
					contract.setContractshstate("0"); 		//审核状态:未开始
					contract.setFjcount(fjcount);
					systemService.saveOrUpdate(contract);
				}
				

			}else{
				j.setMsg("开始时间和结束时间不能为空");
				j.setSuccess(false);
				return j;
			}

		}catch(Exception e){
			e.printStackTrace();
			message = "延期失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

3、系统接收数据 并做相应操作

@RequestMapping(params="doyanqildserver")
	@ResponseBody
	@ApiOperation(value="操作系统做延期传值至煦伟",produces="application/json",httpMethod="POST")
	public  JSONObject doyanqildserver(HttpServletRequest request) {
		JSONObject jsonObject =new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			StringBuilder content1 = new StringBuilder();
			InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(), "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				content1.append(str);
			}
			bufferedReader.close();
			inputStreamReader.close();
			JSONObject resp = JSONObject.parseObject(content1.toString());
			//System.out.println(resp);


			String contractListStr = resp.getString("contractList");

			//System.out.println(contractListStr);
			net.sf.json.JSONArray array = net.sf.json.JSONArray.fromObject(contractListStr);

			System.out.println("————————————————————————————————————操作系统——————————————————");
			System.out.println(array);

			RowMapper<CommonContractEntity> rowMapper = new BeanPropertyRowMapper<CommonContractEntity>(CommonContractEntity.class);



			List<CommonContractEntity> contractList = (List<CommonContractEntity>)net.sf.json.JSONArray.toList(array, CommonContractEntity.class);

			// 先判断接口传来的合同号在操作系统中能否找到,如果找到就正常执行,找不到返回error,只对销售做判断
			for (CommonContractEntity commonContract : contractList) {
				if(StringUtil.isNotEmpty(commonContract.getContractproto())) {

				
						}
					}


			jsonObject.put("result", "success");

		} catch (Exception e) {

			e.printStackTrace();
			jsonObject.put("result", "error");
		}



		return jsonObject;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值