1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/**
* @author VVulpes
* @CreateDate 2024/5/8
* @ProjectDetails [测点注册信息同步接口:将表格内容打包成Json数据进行发送]
*/
@RestController
public class DataController {
@PostMapping("/bridgeMonitorPointInfoSync")
public Result PostJsonData (@RequestParam("file") MultipartFile file,@RequestParam("id")Integer pro_id) throws Exception{//传入表格文件与项目ID,转为Json数据发送至url的方法
//file参数对应传入Excel表(且需要是.xlsx),pro_id是项目编号
String fileName = "Log_"+file.getOriginalFilename();//最后导出的txt文件的名称
try{
//读取Excel的.xlsx文件
InputStream inputStream = file.getInputStream();
Workbook workbook = new XSSFWorkbook(inputStream);
//读取Excel文件的第一张表
Sheet sheet = workbook.getSheetAt(0);
int rowLength = sheet.getLastRowNum()+1;//总行数
int sn = 0;//成功次数
int fn = 0;//失败次数
List<String> list = new ArrayList<>();//失败记录,记录失败后返回的响应报文及失败行
List<String> Jsonlist = new ArrayList<>();//最后要导出至txt中的内容,记录每一次的失败记录与对应的失败Json数据
for(int i=2;i<rowLength;i++){
Row row = sheet.getRow(i);//获取某行的数据
Count allcount = new Count();//count类记录每次发送后累积的成功与失败次数
allcount = postData(pro_id, row, sn, fn, i, list, Jsonlist);//调用方法,发送数据至指定url
sn = allcount.getSn();
fn = allcount.getFn();
}
int sum = sn + fn;//读取的表中检索到的总行数
System.out.println("\n---------------------------------------------");
System.out.println("检索到数据共<"+sum+">条,成功<"+ sn +">行,失败<"+fn+">行\n");
if(fn>0) {//如果有失败记录,则输出txt文件
System.out.println("失败记录:\n" + list + "\n");
exportTxtFile(Jsonlist, fileName);//将报错的信息与数据打包至txt文件中
}
}catch (IOException e){
e.printStackTrace();
}
return Result.success("数据注册结束");
}
private Count postData(Integer pro_id, Row row, int sn, int fn, int i, List<String> list, List<String> Jsonlist){//参数:项目ID、行数据、当前成功次数、当前失败次数、行、当前记录的所有失败记录、当前存储的所有最后要导出至txt的内容
//将数据发送至url的主要方法
try{
Data data = new Data();
Integer bridgeUniqueCode = getCellValue(row,0,Integer.class);
Long pointUniqueCode = getCellValue(row, 1,Long.class);
String pointCode = getCellValue(row, 2,String.class);
String pointPosition = getCellValue(row, 3,String.class);
String monitorCategory = getCellValue(row, 4,String.class);
String monitorTypeCode = getCellValue(row, 5,String.class);
Integer monitorContentCode = getCellValue(row, 6,Integer.class);
String monitorContent = getCellValue(row, 7,String.class);
Float bridgeInitValue = getCellValue(row, 8,Float.class);
Float designValue = getCellValue(row, 9,Float.class);
String sensorType = getCellValue(row, 10,String.class);
String sensorBrand = getCellValue(row, 11,String.class);
String sensorModel = getCellValue(row, 12,String.class);
String collectorCode = getCellValue(row, 13,String.class);
String collectorBrand = getCellValue(row, 14,String.class);
String collectorModel = getCellValue(row, 15,String.class);
Float samplingFrequence = getCellValue(row, 16,Float.class);
float[] levelOneUpLimit = getCellValue(row, 17,float[].class);
float[] levelOneDownLimit = getCellValue(row, 18,float[].class);
float[] levelTwoUpLimit = getCellValue(row, 19,float[].class);
float[] levelTwoDownLimit = getCellValue(row, 20,float[].class);
float[] levelThreeUpLimit = getCellValue(row, 21,float[].class);
float[] levelThreeDownLimit = getCellValue(row, 22,float[].class);
int installDate = getCellValue(row, 23,Integer.class);//获取安装年份
data.setBridgeUniqueCode(bridgeUniqueCode);
data.setPointUniqueCode(pointUniqueCode);
data.setPointCode(pointCode);
data.setPointPosition(pointPosition);
data.setMonitorCategory(monitorCategory);
data.setMonitorTypeCode(monitorTypeCode);
data.setMonitorContentCode(monitorContentCode);
data.setMonitorContent(monitorContent);
data.setBridgeInitValue(bridgeInitValue);
data.setDesignValue(designValue);
data.setSensorType(sensorType);
data.setSensorBrand(sensorBrand);
data.setSensorModel(sensorModel);
data.setCollectorCode(collectorCode);
data.setCollectorBrand(collectorBrand);
data.setCollectorModel(collectorModel);
data.setSamplingFrequence(samplingFrequence);
data.setLevelOneUpLimit(levelOneUpLimit);
data.setLevelOneDownLimit(levelOneDownLimit);
data.setLevelTwoUpLimit(levelTwoUpLimit);
data.setLevelTwoDownLimit(levelTwoDownLimit);
data.setLevelThreeUpLimit(levelThreeUpLimit);
data.setLevelThreeDownLimit(levelThreeDownLimit);
data.setInstallDate(YearToTimestamp(installDate,pro_id));//将安装年份转为13位时间戳
//根据项目编号,向对应的url发送数据
HttpPost httpPost = new HttpPost("https://xxx.gov.cn:8888/POST_JSON_HTTP/?token=");// 替换为你的网址
if(pro_id==2) {
httpPost = new HttpPost("https://xxx.gov.cn:8888/POST_JSON_HTTP/?token=");// 替换为你的网址
}else if(pro_id==3){
httpPost = new HttpPost("https://xxx.gov.cn:8888/POST_JSON_HTTP/?token=");// 替换为你的网址
}else if(pro_id==4) {
httpPost = new HttpPost("https://xxx.gov.cn:8888/POST_JSON_HTTP/?token=");// 替换为你的网址
}
String JsonStr="\n{\"data\": ["+data.ExceltoJsonStr()+"]}";//最后向网站发送的Json格式
CloseableHttpClient httpClient = HttpClients.createDefault();//创建Httpclient对象
String jsonInputString = JsonStr;//要发送的Json字符串
StringEntity entity = new StringEntity(jsonInputString, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost);//response存储服务器响应
System.out.print("服务器响应码: " + response.getStatusLine().getStatusCode());//200意味着成功连上服务器,但不代表数据成功注册了
//获取网站应答报文
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSONObject.parseObject(responseBody);
ResponseBody Message = new ResponseBody();//自定义ResponseBody类存储应答报文数据
Message.setData(jsonObject.getString("data"));
Message.setSuccess(jsonObject.getBoolean("success"));
Message.setErrorCode(jsonObject.getInteger("errorCode"));
Message.setErrorMsg(jsonObject.getString("errorMsg"));
Message.setStatus(jsonObject.getInteger("status"));
Message.setTimestamp(jsonObject.getLong("timestamp"));
Integer status = Message.getStatus();//获取应答报文中的响应码
String errorMsg = Message.getErrorMsg();//获取应答报文中的报错信息
// 检查应答报文
int n=i+1;
if(status==200){//如果应答报文返回200,才是真正的成功发送
sn++;
System.out.print(" 第"+ n +"行: "+ pointUniqueCode + " 状态码: " + status + " 注册成功!");
}else{//如果应答报文不是200,则发送失败
fn++;
String Str = "\n失败行:" + n + ",数据:" + pointUniqueCode + "|-----报错信息:<" + errorMsg + ">-----|应答报文: " + responseBody ;
list.add(Str);
Jsonlist.add(Str);
Jsonlist.add(JsonStr);
System.out.print(" 第"+ n +"行: "+ pointUniqueCode + " 状态码: " + status + " 注册失败! ");
}
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
Count count = new Count();
count.setFn(fn);//Count类存储失败记录
count.setSn(sn);//Count类存储成功记录
return count;//由于需要返回两个数据(成功、失败)才把该方法定义成Count,这样返回Count类的参数的时候,就能从外面的方法拿到两个数据
}
}
|