Commit 45482b11 authored by chenzai's avatar chenzai

优化uploadMass方法

parent 5ca006a7
......@@ -10,6 +10,7 @@ import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.component.robot.AmosRequestContext;
import com.yeejoin.amos.feign.privilege.Privilege;
import com.yeejoin.amos.feign.privilege.model.AgencyUserModel;
import com.yeejoin.amos.kgd.message.Constant;
import io.github.classgraph.json.JSONUtils;
import io.micrometer.core.instrument.util.JsonUtils;
import org.slf4j.Logger;
......@@ -60,6 +61,7 @@ public class ClientHandler<path> implements Runnable {
String path = this.upload2Maas(ips, hostAndPort);
log.info("调用maas服务返回结果: {}", path);
// 关闭连接
byte[] bytes = path.getBytes();
socket.close();
} catch (IOException e) {
e.printStackTrace();
......@@ -69,14 +71,9 @@ public class ClientHandler<path> implements Runnable {
private String upload2Maas(InputStream inputStream, String hostAndPort) throws IOException {
AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class);
if (Objects.nonNull(robotAuthentication)) {
HttpHeaders headers = new HttpHeaders();
String token = robotAuthentication.getToken();
String product = robotAuthentication.getProduct();
String appKey = robotAuthentication.getAppKey();
headers.add("Token", token);
headers.add("product", product);
headers.add("appKey", appKey);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//上传maas
......@@ -96,44 +93,34 @@ public class ClientHandler<path> implements Runnable {
}
};
params.add("file", resource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, getHeader(token,product,appKey,hostAndPort,true));
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.exchange(uploadUrl, HttpMethod.POST, requestEntity, String.class);
String body = responseEntity.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
String result = jsonObject.getString("result");
String path = jsonObject.getString("path");
System.out.println("result的结果是 " + result);
System.out.println("path的结果是 " + path);
if (jsonObject.getString("status").equals("200")){
log.info("路径:"+path+"/"+result);
}else {
throw new RuntimeException("返回状态码错误");
}
//sheets
HttpHeaders newHeaders = new HttpHeaders();
newHeaders.add("Token", token);
newHeaders.add("product", product);
newHeaders.add("appKey", appKey);
newHeaders.setContentType(MediaType.APPLICATION_JSON);
String sheetsUrl = "http://" + hostAndPort + "/maas/dsm/excel/sheets";
Map<String, String> sheetsParams = new HashMap<>();
sheetsParams.put("fileName", result);
HttpEntity<Map<String, String>> sheetsRequestEntity = new HttpEntity<>(sheetsParams, newHeaders);
HttpEntity<Map<String, String>> sheetsRequestEntity = new HttpEntity<>(sheetsParams, getHeader(token,product,appKey,hostAndPort,false));
ResponseEntity<String> sheetsResponseEntity = restTemplate.exchange(sheetsUrl, HttpMethod.POST, sheetsRequestEntity, String.class);
String sheetsResponseEntityBody = sheetsResponseEntity.getBody();
JSONObject sheetJsonObject = JSONObject.parseObject(sheetsResponseEntityBody);
JSONArray sheetArray = sheetJsonObject.getJSONArray("result");
for (int i = 0; i < sheetArray.size(); i++) {
System.out.println(sheetArray.get(i));
}
//result可能有多个sheet
String columnFormatter1 = sheetJsonObject.getString("result");
String[] sheets = new String[sheetArray.size()];
for (int i = 0; i < sheetArray.size(); i++) {
sheets[i] = (String) sheetArray.getJSONObject(i).get("sheet");
}
System.out.println("sheets数组 "+sheets.toString());
String columnFormatter = columnFormatter1.replace("[", "").replace("]", "");
System.out.println("columnFormatter的结果是 " + columnFormatter);
System.out.println("sheets的结果是: " + sheetsResponseEntityBody);
log.info("sheets的结果是:" + sheetsResponseEntityBody);
//datasource(name:excel+时间戳)
......@@ -143,15 +130,12 @@ public class ClientHandler<path> implements Runnable {
String timestampStr = sdf.format(new Date(timestamp));
String datasourceUrl = "http://" + hostAndPort + "/maas/dsm/datasources";
JSONObject all = new JSONObject();
// JSONObject configJson = new JSONObject();
JSONObject oneJson = new JSONObject();
all.put("filepath",result);
for (int i = 0; i < sheetArray.size(); i++) {
oneJson.put(sheets[i],sheetArray.get(i));
System.out.println("onejson"+i+" "+oneJson.toString());
}
all.put("config",oneJson);
System.out.println("all "+all);
Map<String, String> datasourceParams = new HashMap<>();
datasourceParams.put("detail", all.toString());
......@@ -159,17 +143,28 @@ public class ClientHandler<path> implements Runnable {
datasourceParams.put("subtype", "excel");
datasourceParams.put("name", "excel"+timestampStr);
datasourceParams.put("type", "File");
HttpEntity<Map<String, String>> datasourceRequestEntity = new HttpEntity<>(datasourceParams, newHeaders);
HttpEntity<Map<String, String>> datasourceRequestEntity = new HttpEntity<>(datasourceParams, getHeader(token,product,appKey,hostAndPort,false));
ResponseEntity<String> datasourceResponseEntity = restTemplate.exchange(datasourceUrl, HttpMethod.POST, datasourceRequestEntity, String.class);
String datasourceResponseEntityBody = datasourceResponseEntity.getBody();
System.out.println("datasourceResponseEntityBody " + datasourceResponseEntityBody);
log.info("datasourceResponseEntityBody:" + datasourceResponseEntityBody);
return path + "/" + result;
}
return null;
}
private HttpHeaders getHeader(String token,String product,String appKey,String hostAndPort,Boolean isUpload){
HttpHeaders header = new HttpHeaders();
header.add(Constant.TOKEN, token);
header.add(Constant.PRODUCT, product);
header.add(Constant.APPKEY, appKey);
if (isUpload){
header.setContentType(MediaType.MULTIPART_FORM_DATA);
}else {
header.setContentType(MediaType.APPLICATION_JSON);
}
return header;
}
public static void main(String[] args) throws Exception {
requestInfoToSocketServer();
}
......@@ -179,7 +174,6 @@ public class ClientHandler<path> implements Runnable {
Socket socket = new Socket("127.0.0.1", 7777);
OutputStream ops = socket.getOutputStream();
FileInputStream fis = new FileInputStream("D:\\SamData\\RecordXLS\\測試\\第一阶段任务与考核指标.xlsx");
// FileInputStream fis = new FileInputStream("文档\\工作簿1.xlsx");
int len = 0;
byte[] bs = new byte[20480];
while ((len = fis.read(bs)) != -1) {
......
......@@ -2,4 +2,7 @@ package com.yeejoin.amos.kgd.message;
public class Constant {
public static final String REGION = "REALTIME";
public static final String TOKEN = "Token";
public static final String APPKEY = "appKey";
public static final String PRODUCT = "product";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment