From af1889b5c2b7cbe0bc49dd1698fcfe3f38967832 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 3 Apr 2023 23:53:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../idc/service/impl/IdcServiceImpl.java | 72 ++++++++++++++++--- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/api-admin/src/main/java/com/glxp/sale/admin/idc/service/impl/IdcServiceImpl.java b/api-admin/src/main/java/com/glxp/sale/admin/idc/service/impl/IdcServiceImpl.java index 123d96e..48c6ecb 100644 --- a/api-admin/src/main/java/com/glxp/sale/admin/idc/service/impl/IdcServiceImpl.java +++ b/api-admin/src/main/java/com/glxp/sale/admin/idc/service/impl/IdcServiceImpl.java @@ -3,6 +3,7 @@ package com.glxp.sale.admin.idc.service.impl; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; @@ -275,9 +276,9 @@ public class IdcServiceImpl implements IdcService { if(isLastLevel()) { if(json.get("code")!=null) { - success = analyToDB(json.getJSONObject("data"),files,false); + success = analyToDB(host,json.getJSONObject("data"),files,false); } else { - success = analyToDB(json,files,false); + success = analyToDB(host,json,files,false); } if(!success) logger.info("fetchData-->解析失败"); @@ -376,7 +377,7 @@ public class IdcServiceImpl implements IdcService { syncAddTaskStatus(json,true,true); /*解析入库*/ if(isLastLevel()) { - if(!analyToDB(json,files,true)) + if(!analyToDB("",json,files,true)) return ResultVOUtils.error(9000, "解析失败"); } else { //需要转发 @@ -628,7 +629,7 @@ public class IdcServiceImpl implements IdcService { /*解析到数据库*/ - private boolean analyToDB(JSONObject jsonObject,MultipartFile[] files,boolean isUpload) { + private boolean analyToDB(String host,JSONObject jsonObject,MultipartFile[] files,boolean isUpload) { if(jsonObject.get("data")!=null) { String tableName = ""; @@ -649,7 +650,7 @@ public class IdcServiceImpl implements IdcService { filePathColumn = obj.getString("filePathColumn"); } if(!StringUtils.isEmpty(tableName)) { - return analyData(tableName,uniqueColumn,filePathColumn,list,isUpload); + return analyData(host,tableName,uniqueColumn,filePathColumn,list,isUpload); } else { logger.error("数据格式错误:无数据标记"); } @@ -659,7 +660,7 @@ public class IdcServiceImpl implements IdcService { } /*按表名解析数据到数据库,子表暂未处理*/ - private boolean analyData(String tableName,String uniqueColumn,String filePathColumn,List> list,boolean isUpload) { + private boolean analyData(String host,String tableName,String uniqueColumn,String filePathColumn,List> list,boolean isUpload) { String tName = DBAUtils.tableRealName(tableName); String sql="replace "+tName +"("; @@ -770,7 +771,7 @@ public class IdcServiceImpl implements IdcService { for (Object o : (List) obj) { chList.add((Map)o); } - analyData(list.get(i).get("tableName"+m).toString(),"","",chList,isUpload); + analyData(host,list.get(i).get("tableName"+m).toString(),"","",chList,isUpload); } else { break; } @@ -782,6 +783,9 @@ public class IdcServiceImpl implements IdcService { if(!result) logger.error(tableName+"-->fetchSave Fail"); } + if(!isUpload&&!StringUtils.isEmpty(filePathColumn)) { + batchDownloadFile(host,files); + } } return result; @@ -842,18 +846,64 @@ public class IdcServiceImpl implements IdcService { } /*下载文件*/ - private boolean batchDownloadFile(String[] files) { + private boolean batchDownloadFile(String syncIp,String[] files) { boolean success = true; for(String fileName:files) { if(!StringUtils.isEmpty(fileName)) { - if(!signleDownloadFile(fileName)) + if(!signleDownloadFile(syncIp,fileName)) success = false; } } return success; } - private boolean signleDownloadFile(String fileName) { - + private boolean signleDownloadFile(String syncIp,String fileName) { + OkHttpClient client = new OkHttpClient(); + RequestBody requestBody = new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("fileName", fileName) + .build(); + + + Request request = new Request.Builder() + .url(syncIp+"/spssync/common/downloadFile") + .post(requestBody) + .build(); + try { + Response result = client.newCall(request).execute(); + + + + if (MediaType.parse("application/force-download").equals(result.body().contentType())) { + try (InputStream inputStream = result.body().byteStream()) { + String filePathSlash = filePath.substring(filePath.length() -1).equals("/") ? "" : "/"; + + FileOutputStream outputStream =new FileOutputStream(filePath +filePathSlash+fileName); + + byte b[]=new byte[1024]; + + int len=0; + + while ((len=inputStream.read(b))!=-1){ + + outputStream.write(b,0,len); + + } + + outputStream.flush(); + + + + } catch (Exception e) { + + + } + } + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return true; }