Commit 2e4f3c50 authored by chenzai's avatar chenzai

修改上传maas功能

parent 8dad2129
package com.yeejoin.amos.kgd.config; package com.yeejoin.amos.kgd.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yeejoin.amos.boot.core.utils.RestTemplateUtil;
import com.yeejoin.amos.component.feign.model.FeignClientResult;
import com.yeejoin.amos.component.robot.AmosRequestContext; 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 com.yeejoin.amos.kgd.message.Constant;
import io.github.classgraph.json.JSONUtils;
import io.micrometer.core.instrument.util.JsonUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.typroject.tyboot.core.foundation.context.RequestContext;
import org.typroject.tyboot.core.foundation.context.SpringContextHelper; import org.typroject.tyboot.core.foundation.context.SpringContextHelper;
import java.io.*; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
...@@ -49,7 +36,7 @@ public class ClientHandler<path> implements Runnable { ...@@ -49,7 +36,7 @@ public class ClientHandler<path> implements Runnable {
public ClientHandler(Socket socket, String hostAndPort) { public ClientHandler(Socket socket, String hostAndPort) {
this.socket = socket; this.socket = socket;
this.hostAndPort = hostAndPort; this.hostAndPort = hostAndPort; //127.0.0.1:30009
} }
@Override @Override
...@@ -57,9 +44,15 @@ public class ClientHandler<path> implements Runnable { ...@@ -57,9 +44,15 @@ public class ClientHandler<path> implements Runnable {
try { try {
// 获取输入流和输出流 // 获取输入流和输出流
InputStream ips = socket.getInputStream(); InputStream ips = socket.getInputStream();
// ips中,不是单纯的文件流,还有一些其他字段,所以要将单纯文件的部分过滤出来
String filePath = filenameFilter(ips);
String filenameWithSuffix = FileSystems.getDefault().getPath(filePath).getFileName().toString();
String filename = filenameWithSuffix.substring(0, filenameWithSuffix.lastIndexOf("."));
ips = fileInputStreamFilter(ips);
// 处理客户端请求 // 处理客户端请求
log.info("收到客户端数据: "); log.info("收到客户端数据: ");
String path = this.upload2Maas(ips, hostAndPort); String path = this.upload2Maas(ips, hostAndPort, filename);
log.info("调用maas服务返回结果: {}", path); log.info("调用maas服务返回结果: {}", path);
// 关闭连接 // 关闭连接
byte[] bytes = path.getBytes(); byte[] bytes = path.getBytes();
...@@ -69,19 +62,19 @@ public class ClientHandler<path> implements Runnable { ...@@ -69,19 +62,19 @@ public class ClientHandler<path> implements Runnable {
} }
} }
private String upload2Maas(InputStream inputStream, String hostAndPort) throws IOException { private String upload2Maas(InputStream inputStream, String hostAndPort, String filename) throws IOException {
InputStream intercept = intercept(inputStream);
AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class); AmosRequestContext robotAuthentication = SpringContextHelper.getBean(AmosRequestContext.class);
if (Objects.nonNull(robotAuthentication)) { if (Objects.nonNull(robotAuthentication)) {
String token = robotAuthentication.getToken(); // String token = robotAuthentication.getToken();
String product = robotAuthentication.getProduct(); // String product = robotAuthentication.getProduct();
String appKey = robotAuthentication.getAppKey(); // String appKey = robotAuthentication.getAppKey();
//上传maas
String token = "1121d8d2-915f-453e-8e20-1558566cefbb"; // 如果token、失效,就去浏览器复制最新的token、
String product = "AMOS_STUDIO_WEB";
String appKey = "AMOS_STUDIO";
//upload //1, 上传到maas
String uploadUrl = "http://" + hostAndPort + "/maas/dsm/excel/upload"; String uploadUrl = "http://" + hostAndPort + "/maas/dsm/excel/upload";
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
Resource resource = new InputStreamResource(inputStream) { Resource resource = new InputStreamResource(inputStream) {
...@@ -110,7 +103,25 @@ public class ClientHandler<path> implements Runnable { ...@@ -110,7 +103,25 @@ public class ClientHandler<path> implements Runnable {
throw new RuntimeException("返回状态码错误"); throw new RuntimeException("返回状态码错误");
} }
//sheets // 2, 调用conn接口
String connsUrl = "http://" + hostAndPort + "/maas/maas/desktop/conns";
Map<String, String> params2 = new HashMap<>();
params2.put("connType", "excel");
params2.put("detail", "{\"location\":{\"type\":\"excel\",\"path\":\"" + result + "\"}}");
params2.put("subjectid", "534de50c-bb8c-4450-87a0-557aac745a94");
HttpEntity<Map<String, String>> requestEntity2 = new HttpEntity<>(params2, getHeader(token, product, appKey, hostAndPort, false));
ResponseEntity<String> response2 = restTemplate.exchange(connsUrl, HttpMethod.POST, requestEntity2, String.class);
String body2 = response2.getBody();
JSONObject jsonObject2 = JSONObject.parseObject(body2);
String connId = jsonObject2.getString("result");
String pathConns = jsonObject2.getString("path");
// String queryUrl = "http://" + hostAndPort + "/maas/maas/desktop/data/query?page=0&size=50";
// Map<String, String> params3 = new HashMap<>();
// params3.put("connId", connId);
// params3.put("query", "{\"columnNameLineNumber\":1,\"skipEmptyLines\":true,\"skipEmptyColumns\":false,\"columnFormatter\":{},\"sheet\":\"Sheet1\",\"configed\":true}");
//3, sheets
String sheetsUrl = "http://" + hostAndPort + "/maas/dsm/excel/sheets"; String sheetsUrl = "http://" + hostAndPort + "/maas/dsm/excel/sheets";
Map<String, String> sheetsParams = new HashMap<>(); Map<String, String> sheetsParams = new HashMap<>();
sheetsParams.put("fileName", result); sheetsParams.put("fileName", result);
...@@ -127,35 +138,53 @@ public class ClientHandler<path> implements Runnable { ...@@ -127,35 +138,53 @@ public class ClientHandler<path> implements Runnable {
log.info("sheets的结果是:" + sheetsResponseEntityBody); log.info("sheets的结果是:" + sheetsResponseEntityBody);
//datasource(name:excel+时间戳) // 4,
// 获取当前时间的时间戳 String tablesUrl = "http://" + hostAndPort + "/maas/maas/desktop/tables";
long timestamp = System.currentTimeMillis(); Map<String, String> params3 = new HashMap<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); params3.put("name", filename);
String timestampStr = sdf.format(new Date(timestamp)); params3.put("pid", "534de50c-bb8c-4450-87a0-557aac745a94");
String datasourceUrl = "http://" + hostAndPort + "/maas/dsm/datasources"; params3.put("connId", connId);
JSONObject all = new JSONObject(); // params3.add("config", sheetArray.get(0).toString());
JSONObject oneJson = new JSONObject(); params3.put("config", "{\"columnNameLineNumber\":1,\"skipEmptyLines\":true,\"skipEmptyColumns\":false,\"columnFormatter\":{\"zztest\":{\"name\":\"zztest\",\"alias\":\"zztest\",\"type\":\"VARCHAR\"},\"zz\":{\"name\":\"zz\",\"alias\":\"zz\",\"type\":\"VARCHAR\"}},\"sheet\":\"Sheet1\"}");
all.put("filepath",result); HttpEntity<Map<String, String>> requestEntity3 = new HttpEntity<>(params3, getHeader(token, product, appKey, hostAndPort, false));
for (int i = 0; i < sheetArray.size(); i++) { ResponseEntity<String> response3 = restTemplate.exchange(tablesUrl, HttpMethod.POST, requestEntity3, String.class);
oneJson.put(sheets[i],sheetArray.get(i)); String body3 = response3.getBody();
} JSONObject jsonObject3 = JSONObject.parseObject(body3);
all.put("config",oneJson); String result2 = jsonObject3.getString("result");
String path2 = jsonObject3.getString("path");
Map<String, String> datasourceParams = new HashMap<>();
datasourceParams.put("detail", all.toString());
datasourceParams.put("groupid", "72684d79-5d28-4086-9f21-5b091b6675db"); // //datasource(name:excel+时间戳)
datasourceParams.put("subtype", "excel"); // // 获取当前时间的时间戳
datasourceParams.put("name", "excel"+timestampStr); // long timestamp = System.currentTimeMillis();
datasourceParams.put("type", "File"); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
HttpEntity<Map<String, String>> datasourceRequestEntity = new HttpEntity<>(datasourceParams, getHeader(token,product,appKey,hostAndPort,false)); // String timestampStr = sdf.format(new Date(timestamp));
ResponseEntity<String> datasourceResponseEntity = restTemplate.exchange(datasourceUrl, HttpMethod.POST, datasourceRequestEntity, String.class); // String datasourceUrl = "http://" + hostAndPort + "/maas/dsm/datasources";
String datasourceResponseEntityBody = datasourceResponseEntity.getBody(); // JSONObject all = new JSONObject();
log.info("datasourceResponseEntityBody:" + datasourceResponseEntityBody); // JSONObject oneJson = new JSONObject();
return path + "/" + result; // all.put("filepath",result);
// for (int i = 0; i < sheetArray.size(); i++) {
// oneJson.put(sheets[i],sheetArray.get(i));
// }
// all.put("config",oneJson);
// Map<String, String> datasourceParams = new HashMap<>();
// datasourceParams.put("detail", all.toString());
// datasourceParams.put("groupid", "72684d79-5d28-4086-9f21-5b091b6675db");
// datasourceParams.put("subtype", "excel");
// datasourceParams.put("name", "excel"+timestampStr);
// datasourceParams.put("type", "File");
// 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();
// log.info("datasourceResponseEntityBody:" + datasourceResponseEntityBody);
// return path + "/" + result;
} }
return null; return null;
} }
//根据机器人生成的校验参数将其放入请求头
private HttpHeaders getHeader(String token, String product, String appKey, String hostAndPort, Boolean isUpload) { private HttpHeaders getHeader(String token, String product, String appKey, String hostAndPort, Boolean isUpload) {
HttpHeaders header = new HttpHeaders(); HttpHeaders header = new HttpHeaders();
header.add(Constant.TOKEN, token); header.add(Constant.TOKEN, token);
...@@ -173,8 +202,78 @@ public class ClientHandler<path> implements Runnable { ...@@ -173,8 +202,78 @@ public class ClientHandler<path> implements Runnable {
requestInfoToSocketServer(); requestInfoToSocketServer();
} }
public static void findDelimiter(InputStream is, String delimiter) throws IOException {
StringBuilder sb = new StringBuilder();
int nextByte;
while ((nextByte = is.read()) != -1) {
char c = (char) nextByte;
sb.append(c);
if (sb.toString().contains(delimiter)) {
return;
}
}
throw new IOException("没有开始分隔符");
}
public static byte[] checkDelimiter(byte[] buf, int byteRead, byte[] delimiter) {
if (delimiter.length > buf.length) return buf;
for (int i = 0; i <= buf.length - delimiter.length; i ++ ) {
boolean isMatch = true;
for (int j = 0; j < delimiter.length; j ++ ) {
if (buf[i + j] != delimiter[j]) {
isMatch = false;
break;
}
}
if (isMatch) {
return Arrays.copyOf(buf, i);
}
}
return buf;
}
public static String filenameFilter(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedInputStream bis = new BufferedInputStream(is);
findDelimiter(bis, "##STATITLE##");
byte[] endDelimiter = "##ENDTITLE##".getBytes();
int byteRead;
byte[] buf = new byte[4096];
if ((byteRead = bis.read(buf)) != -1) {
byte[] newByte = checkDelimiter(buf, byteRead, endDelimiter);
return new String(newByte, "GBK");
}
throw new IOException("没有输入流内容或者文件名太长");
}
public static InputStream fileInputStreamFilter(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
String fileTmpPath = "D:\\tmp.xlsx";
FileOutputStream fos = new FileOutputStream(fileTmpPath);
byte[] endDelimiter = "##ENDFILE##".getBytes();
findDelimiter(bis, "##STAFILE##");
int byteRead;
byte[] buf = new byte[4000];
while ((byteRead = bis.read(buf)) != -1) {
byte[] newByte = checkDelimiter(buf, byteRead, endDelimiter);
fos.write(newByte, 0, Math.min(byteRead, newByte.length));
}
return new FileInputStream(fileTmpPath);
}
//截取文件流 //截取文件流
public static InputStream intercept(InputStream is) { public static InputStream intercept(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String line = null; String line = null;
...@@ -192,6 +291,15 @@ public class ClientHandler<path> implements Runnable { ...@@ -192,6 +291,15 @@ public class ClientHandler<path> implements Runnable {
} }
} }
String string1 = sb.toString(); String string1 = sb.toString();
// int fileStart = string1.indexOf(".xlsx") + 5;
//// int fileStart = string1.indexOf(".txt") + 4;
// String file = string1.substring(fileStart);
//
// InputStream fileInputStream = new ByteArrayInputStream(file.getBytes());
// return fileInputStream;
//
int startFilePath = string1.indexOf("##STATITLE##"); int startFilePath = string1.indexOf("##STATITLE##");
int endFilePath = string1.indexOf("##ENDTITLE##"); int endFilePath = string1.indexOf("##ENDTITLE##");
int startFile = string1.indexOf("##STAFILE##"); int startFile = string1.indexOf("##STAFILE##");
...@@ -218,6 +326,11 @@ public class ClientHandler<path> implements Runnable { ...@@ -218,6 +326,11 @@ public class ClientHandler<path> implements Runnable {
log.info("文件名:" + filePath); log.info("文件名:" + filePath);
String file = string1.substring(startFile, endFile).substring("##STAFILE##".length()); String file = string1.substring(startFile, endFile).substring("##STAFILE##".length());
log.info("文件:" + file); log.info("文件:" + file);
System.out.println("file size " + file.length() );
// FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\kgdcz\\data\\maas\\test.xlsx"));
// fileOutputStream.write(file.getBytes());
// fileOutputStream.close();
InputStream fileInputStream = new ByteArrayInputStream(file.getBytes()); InputStream fileInputStream = new ByteArrayInputStream(file.getBytes());
return fileInputStream; return fileInputStream;
...@@ -228,8 +341,9 @@ public class ClientHandler<path> implements Runnable { ...@@ -228,8 +341,9 @@ public class ClientHandler<path> implements Runnable {
private static void requestInfoToSocketServer() { private static void requestInfoToSocketServer() {
try { try {
Socket socket = new Socket("127.0.0.1", 7777); Socket socket = new Socket("127.0.0.1", 7777);
// Socket socket = new Socket("localhost", 7777);
OutputStream ops = socket.getOutputStream(); OutputStream ops = socket.getOutputStream();
FileInputStream fis = new FileInputStream("D:\\SamData\\RecordXLS\\測試\\第一阶段任务与考核指标.xlsx"); FileInputStream fis = new FileInputStream("D:\\SamData\\RecordXLS\\測試\\1.xlsx");
int len = 0; int len = 0;
byte[] bs = new byte[20480]; byte[] bs = new byte[20480];
while ((len = fis.read(bs)) != -1) { while ((len = fis.read(bs)) != -1) {
......
## DB properties: ## DB properties:
spring.datasource.url=jdbc:mysql://172.16.3.221:3306/amos_idx_biz?allowMultiQueries=true&serverTimezone=GMT%2B8&characterEncoding=utf8 spring.datasource.url=jdbc:mysql://172.16.3.101:3306/jd_bearing?allowMultiQueries=true&serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=Yeejoin@2020 spring.datasource.password=Yeejoin@2020
## eureka properties: ## eureka properties:
eureka.instance.hostname=172.16.3.221 eureka.instance.hostname=127.0.0.1
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:10001/eureka/ eureka.client.serviceUrl.defaultZone=http://admin:a1234560@localhost:10001/eureka/
## redis properties: ## redis properties:
spring.redis.database=1 spring.redis.database=1
spring.redis.host=172.16.3.221 spring.redis.host=localhost
spring.redis.port=6379 spring.redis.port=6379
spring.redis.password=yeejoin@2020 spring.redis.password=yeejoin@2020
...@@ -43,13 +43,13 @@ lettuce.timeout=10000 ...@@ -43,13 +43,13 @@ lettuce.timeout=10000
emqx.clean-session=true emqx.clean-session=true
emqx.client-id=${spring.application.name}-${random.int[1024,65536]} emqx.client-id=${spring.application.name}-${random.int[1024,65536]}
emqx.broker=tcp://172.16.3.221:2883 emqx.broker=tcp://localhost:2883
emqx.client-user-name=super emqx.client-user-name=super
emqx.client-password=a123456 emqx.client-password=a123456
emqx.max-inflight=1000 emqx.max-inflight=1000
spring.influx.url=http://172.16.3.221:8086 spring.influx.url=http://localhost:8086
spring.influx.password=Yeejoin@2020 spring.influx.password=Yeejoin@2020
spring.influx.user=root spring.influx.user=root
spring.influx.database=iot_platform spring.influx.database=iot_platform
......
...@@ -8,7 +8,7 @@ spring.jackson.date-format=yyyy-MM-dd HH:mm:ss ...@@ -8,7 +8,7 @@ spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
logging.config=classpath:logback-${spring.profiles.active}.xml logging.config=classpath:logback-${spring.profiles.active}.xml
## mybatis-plus配置控制台打印完整带参数SQL语句 ## mybatis-plus\u914D\u7F6E\u63A7\u5236\u53F0\u6253\u5370\u5B8C\u6574\u5E26\u53C2\u6570SQL\u8BED\u53E5
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
## DB properties: ## DB properties:
...@@ -43,7 +43,7 @@ spring.redis.lettuce.pool.max-active=200 ...@@ -43,7 +43,7 @@ spring.redis.lettuce.pool.max-active=200
spring.redis.lettuce.pool.max-wait=-1 spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=10 spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=0 spring.redis.lettuce.pool.min-idle=0
## redis失效时间 ## redis\u5931\u6548\u65F6\u95F4
redis.cache.failure.time=10800 redis.cache.failure.time=10800
spring.servlet.multipart.maxFileSize=100MB spring.servlet.multipart.maxFileSize=100MB
...@@ -57,7 +57,8 @@ spring.http.encoding.force=true ...@@ -57,7 +57,8 @@ spring.http.encoding.force=true
amos.system.socket.port=7777 amos.system.socket.port=7777
amos.system.maas.url=172.16.3.221:10005 #amos.system.maas.url=172.16.3.221:10005
amos.system.maas.url=127.0.0.1:30009
amos.system.user.user-name=kgd_gdd amos.system.user.user-name=kgd_gdd
amos.system.user.password=a1234560 amos.system.user.password=a1234560
amos.system.user.app-key=AMOS_STUDIO amos.system.user.app-key=AMOS_STUDIO
......
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