欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 接口对接json

接口对接json

2024/10/24 5:19:49 来源:https://blog.csdn.net/weixin_45386898/article/details/140214407  浏览:    关键词:接口对接json
public AjaxResult zhihuiya(@RequestBody ZlRecord zlRecord) {//查看用户或者部门有没有下载次数SysUser user = SecurityUtils.getLoginUser().getUser();ZlCount zlCount = newLabelMapper.selectUserCount(user.getUserId());if (ObjectUtils.isEmpty(zlCount)){ZlCount zlCount2 = newLabelMapper.selectDeptCount(user.getDeptId());if (ObjectUtils.isEmpty(zlCount2))throw new RuntimeException("该部门或用户没有下载次数");}//获取tokenString token="";String jobid="";String filePath="";String patentNumber="CN106561027B";String clientId="w6nf8ZI4AKA0oYgjJb";String clientSecret="Gy2geNN56TX1h79";String cacheObject = redisCache.getCacheObject("zha");if (StringUtils.isNotEmpty(cacheObject)){token = cacheObject;}else {String tokenhost = "https://"+clientId+":"+clientSecret+"@connect.ziya.com";String tokenpath = "/oauth/token";Map headers = new HashMap();headers.put("Content-Type", "application/x-www-form-urlencoded");Map requestbody = new HashMap();requestbody.put("grant_type", "client_credentials");try {HttpResponse response = HttpUtils.doPost(tokenhost, tokenpath, null, headers, null, requestbody);String result = EntityUtils.toString(response.getEntity());System.out.println(result);org.json.JSONObject jsonResponse = new org.json.JSONObject(result);org.json.JSONObject data = jsonResponse.getJSONObject("data");if (data.has("token")&&StringUtils.isNotEmpty(data.getString("token"))){token = "Bearer "+data.getString("token");redisCache.setCacheObject("zhihuiya" , token, 25, TimeUnit.MINUTES);}} catch (Exception e) {e.printStackTrace();throw new ServiceException("获取token失败", HttpStatus.ERROR);}}//获取专利jobidString host = "https://connect.zya.com";String path = "/patent-valuation-data/patent-report/task/create";Map headers2 = new HashMap();headers2.put("Content-Type", "application/json");headers2.put("authorization", token);Map querys = new HashMap();querys.put("apikey", clientId);//JSONObject dataBody = (JSONObject) JSON.parse("{\"report_type\":\"pdf\",\"patent_number\":\"CN108977442B\"}");JSONObject dataBody = new JSONObject();dataBody.put("report_type", "pdf");dataBody.put("patent_number", zlRecord.getPatentNumber());try {HttpResponse response = HttpUtils.doPost(host, path, null, headers2, querys, dataBody.toJSONString());String result = EntityUtils.toString(response.getEntity());System.out.println(result);org.json.JSONObject jsonResponse = new org.json.JSONObject(result);org.json.JSONObject data = jsonResponse.getJSONObject("data");jobid = data.getString("job_id");} catch (Exception e) {e.printStackTrace();throw new ServiceException("获取报告导出任务失败", HttpStatus.ERROR);}//获取导出链接String host3 = "https://connect.zhya.com";String path3 = "/patent-valuation-data/patent-report/job_id/status";Map headers3 = new HashMap();headers3.put("Content-Type", "application/json");headers3.put("authorization", token);Map querys3 = new HashMap();querys3.put("job_id", jobid);querys3.put("apikey", clientId);try {HttpResponse response = HttpUtils.doGet(host3, path3, null, headers3, querys3);String result = EntityUtils.toString(response.getEntity());System.out.println(result);org.json.JSONObject jsonResponse = new org.json.JSONObject(result);org.json.JSONObject data = jsonResponse.getJSONObject("data");if (jsonResponse.has("error_code")&&jsonResponse.getInt("error_code")!=0){throw new ServiceException("下载报告失败"+jsonResponse.getInt("error_code"), HttpStatus.ERROR);}if (data.has("job_status")&&jsonResponse.getInt("error_code")==0){while (data.getInt("job_status")==1){System.out.println("导出中");Thread.sleep(5000); // 延迟1秒response = HttpUtils.doGet(host3, path3, null, headers3, querys3);result = EntityUtils.toString(response.getEntity());jsonResponse = new org.json.JSONObject(result);data = jsonResponse.getJSONObject("data");}if (data.getInt("job_status")==2){System.out.println("导出完成");filePath = data.getString("download_url");}else {throw new ServiceException("导出失败"+data.getInt("job_status")+"errorcode"+jsonResponse.getString("error_code"), HttpStatus.ERROR);}}} catch (Exception e) {e.printStackTrace();throw new ServiceException("导出报告失败", HttpStatus.ERROR);}String fileName1="";try {// 创建URL对象指向文件下载地址URL url = new URL(filePath);// 打开连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 设置请求方法为GETconnection.setRequestMethod("GET");// 准备读取响应int responseCode = connection.getResponseCode();if (responseCode == HttpURLConnection.HTTP_OK) {String fileNameWithQuery = filePath.substring(filePath.lastIndexOf("/") + 1);int queryIndex = fileNameWithQuery.indexOf("?");String fileName = (queryIndex != -1) ? fileNameWithQuery.substring(0, queryIndex) : fileNameWithQuery;long currentTimeMillis = System.currentTimeMillis();fileName1 = user.getUserId()+"_"+currentTimeMillis+"_"+fileName;// 指定本地保存的路径和文件名Path saveFilePath = Paths.get("/home/www/zlbg/", fileName1);// 创建文件输出流用于写入文件Files.copy(connection.getInputStream(), saveFilePath, StandardCopyOption.REPLACE_EXISTING);System.out.println("文件下载成功:" + saveFilePath.toAbsolutePath());System.out.println("/home/www/zlbg/"+ fileName1);zlRecord.setId(UUID.randomUUID().toString().replaceAll("-", ""));zlRecord.setFilePath("/home/www/zlbg/"+ fileName1);zlRecord.setUserId(user.getUserId());zlRecord.setDeptId(user.getDeptId());newLabelMapper.insertZlRecord(zlRecord);try {if (ObjectUtils.isEmpty(zlCount)){newLabelMapper.updateDeptCount(user.getDeptId());}else {newLabelMapper.updateUserCount(user.getUserId());}} catch (Exception e) {throw new ServiceException("文件下载次数超限", HttpStatus.ERROR);}} else {System.out.println("文件下载失败,响应码:" + responseCode);throw new ServiceException("文件下载失败", HttpStatus.ERROR);}// 关闭连接connection.disconnect();} catch (IOException e) {e.printStackTrace();throw new ServiceException("文件下载失败", HttpStatus.ERROR);}return AjaxResult.success("/home/www/zlbg/"+ fileName1);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com