Commit d420197d authored by 刘凡's avatar 刘凡

优化:【空工大】excel的文件流的解析方法

parent cf16af0b
......@@ -3,7 +3,6 @@ package com.yeejoin.amos.kgd.config;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.util.StringUtil;
import com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator;
import com.yeejoin.amos.component.robot.AmosRequestContext;
import com.yeejoin.amos.kgd.message.Constant;
import com.yeejoin.amos.kgd.message.model.TableFieldModel;
......@@ -24,13 +23,11 @@ import org.typroject.tyboot.core.foundation.context.SpringContextHelper;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -87,7 +84,6 @@ public class ClientHandler<path> implements Runnable {
// 获取输入流和输出流
InputStream ips = socket.getInputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
byte[] buffer2 = new byte[20];
int len2;
......@@ -127,12 +123,12 @@ public class ClientHandler<path> implements Runnable {
// }
// System.out.println(outputStream2.toString());
ips = fileInputStreamFilter(ips);
InputStream contentIps = fileInputStreamFilter(ips);
// 获取Excel表头内容
List<String> tableColumns = ExcelTool.readColumnsFromExcel(ips, filenameWithSuffix);
List<String> tableColumns = ExcelTool.readColumnsFromExcel(contentIps, filenameWithSuffix);
// 获取Excel表数据内容
List<List<String>> tableDatas = ExcelTool.readDataFromExcel(ips, filenameWithSuffix);
List<List<String>> tableDatas = ExcelTool.readDataFromExcel(contentIps, filenameWithSuffix);
// 新增mysql表结构和数据和数据源
if(StringUtil.isNotEmpty(tableName) && tableColumns != null && tableDatas != null){
......@@ -553,13 +549,21 @@ public class ClientHandler<path> implements Runnable {
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)) {
public 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;
// }
// }
// 将输入流包装成 BufferedReader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (StringUtils.isNotEmpty(line) && line.contains(delimiter)) {
return;
}
}
......@@ -584,38 +588,75 @@ public class ClientHandler<path> implements Runnable {
return buf;
}
public static String filenameFilter(InputStream is) throws IOException {
private InputStream getDelimiterContent(InputStream is, String start, String end) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder contentBuilder = new StringBuilder();
boolean isInsideTitleSection = false;
String line;
while ((line = reader.readLine()) != null) {
if (!isInsideTitleSection && line.contains(start)) {
isInsideTitleSection = true;
} else if (isInsideTitleSection && line.contains(end)) {
break;
} else if (isInsideTitleSection) {
contentBuilder.append(line).append("\n");
}
}
return new ByteArrayInputStream(contentBuilder.toString().getBytes("UTF-8"));
}
public String filenameFilter(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
findDelimiter(bis, "##STATITLE##");
String start = "##STATITLE##";
String end = "##ENDTITLE##";
byte[] endDelimiter = "##ENDTITLE##".getBytes();
//校验内容格式
findDelimiter(bis, start);
int byteRead;
byte[] buf = new byte[4096];
if ((byteRead = bis.read(buf)) != -1) {
byte[] newByte = checkDelimiter(buf, byteRead, endDelimiter);
return new String(newByte, "GBK");
//获取指定标记中的内容
InputStream resultContent = getDelimiterContent(is, start, end);
byte[] bytes = new byte[resultContent.available()];
int byteRead = resultContent.read(bytes);
if(byteRead != -1 ){
return new String(bytes);
}
// byte[] endDelimiter = end.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 {
public InputStream fileInputStreamFilter(InputStream is) throws IOException {
BufferedInputStream bis = new BufferedInputStream(is);
String fileTmpPath = "D:\\tmp.xlsx";
FileOutputStream fos = new FileOutputStream(fileTmpPath);
// String fileTmpPath = "D:\\tmp.xlsx";
// FileOutputStream fos = new FileOutputStream(fileTmpPath);
byte[] endDelimiter = "##ENDFILE##".getBytes();
String start = "##STATITLE##";
String end = "##ENDTITLE##";
findDelimiter(bis, "##STAFILE##");
//校验内容格式
findDelimiter(bis, start);
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);
//获取指定标记中的内容
InputStream resultContent = getDelimiterContent(is, start, end);
return resultContent;
// byte[] endDelimiter = end.getBytes();
// 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);
}
//截取文件流
......
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