| | |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import com.chinaztt.mes.docx.dto.GetFileDto; |
| | | import com.opencsv.CSVReader; |
| | | import com.opencsv.CSVReaderBuilder; |
| | | import com.opencsv.exceptions.CsvValidationException; |
| | | import com.chinaztt.mes.docx.dto.ThicknessData; |
| | | import net.sourceforge.tess4j.Tesseract; |
| | | import net.sourceforge.tess4j.TesseractException; |
| | |
| | | import javax.imageio.ImageIO; |
| | | |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.sql.*; |
| | | import java.util.*; |
| | | import java.util.regex.Pattern; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class TakeWords { |
| | | |
| | | private static final String splitIdentifier = "@-@"; // 自定义唯一标识符 |
| | | |
| | | // 科学计数法正则模式 |
| | | private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile( |
| | | "^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$" |
| | | ); |
| | | |
| | | public static Object readWordFile(File file) { |
| | | String result = ""; |
| | |
| | | |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | // 创建 reader |
| | | try (BufferedReader br = Files.newBufferedReader(file.toPath())) { |
| | | // CSV文件的分隔符 |
| | | String DELIMITER = ","; |
| | | // 按行读取 |
| | | String line; |
| | | while ((line = br.readLine()) != null) { |
| | | // 分割 |
| | | String[] columns = line.split(DELIMITER); |
| | | // 打印行 |
| | | stringBuilder.append(String.join(splitIdentifier, columns)).append("\n"); |
| | | // try (BufferedReader br = Files.newBufferedReader(file.toPath())) { |
| | | // // CSV文件的分隔符 |
| | | // String DELIMITER = ","; |
| | | // // 按行读取 |
| | | // String line; |
| | | // System.out.println(br.readLine()); |
| | | // while ((line = br.readLine()) != null) { |
| | | // // 分割 |
| | | // String[] columns = line.split(DELIMITER); |
| | | // // 打印行 |
| | | // stringBuilder.append(String.join(splitIdentifier, columns)).append("\n"); |
| | | // } |
| | | // } catch (IOException ex) { |
| | | // ex.printStackTrace(); |
| | | // } |
| | | try (FileReader fileReader = new FileReader(file); |
| | | CSVReader csvReader = new CSVReaderBuilder(fileReader).build()) { |
| | | |
| | | String[] nextLine; |
| | | while ((nextLine = csvReader.readNext()) != null) { |
| | | // 处理每一行数据 |
| | | for (String cell : nextLine) { |
| | | if(StringUtils.isNotBlank(cell)){ |
| | | stringBuilder.append(scientificToNumber(cell)).append(splitIdentifier); |
| | | } |
| | | } |
| | | stringBuilder.append("\n"); |
| | | } |
| | | } catch (IOException ex) { |
| | | ex.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (CsvValidationException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return stringBuilder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 将科学计数法转换为数字 |
| | | * @param cell |
| | | * @return |
| | | */ |
| | | public static String scientificToNumber(String cell){ |
| | | if(SCIENTIFIC_PATTERN.matcher(cell).matches()){ |
| | | return String.valueOf(Double.parseDouble(cell)); |
| | | } |
| | | return cell; |
| | | } |
| | | |
| | | public static Object readMdbFile(File file, GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
| | |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | public static Object getmysqlFile(GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
| | | public static Object getMysqlFile(GetFileDto getFileDto){ |
| | | Map<String, Object> tableMap = new HashMap<>(16); |
| | | // 数据库连接信息 |
| | | String url = "jdbc:mysql://localhost:3306/"+getFileDto.getDbFileName()+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; |
| | | // 从 GetFileDto 获取数据库名,对应【文件名称】字段 |
| | | String dbName = getFileDto.getDbFileName(); |
| | | String user = getFileDto.getDbUserName(); |
| | | String password = getFileDto.getDbPassword(); |
| | | List<ThicknessData> dataList = new ArrayList<>(); |
| | | // 从 GetFileDto 获取数据表名,对应【数据库表名】字段 |
| | | String table = getFileDto.getDbTable(); |
| | | // 检查数据库名和表名是否为空 |
| | | if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) { |
| | | return R.failed("数据库名或表名不能为空"); |
| | | } |
| | | // 数据库连接信息 |
| | | String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; |
| | | Connection connection = null; |
| | | PreparedStatement preparedStatement = null; |
| | | ResultSet resultSet = null; |
| | | List<Map<String, Object>> dataList = new ArrayList<>(); |
| | | |
| | | try ( |
| | | // 建立连接 |
| | | Connection connection = DriverManager.getConnection(url, user, password); |
| | | // 创建 Statement 对象执行 SQL |
| | | Statement statement = connection.createStatement() |
| | | ) { |
| | | String sql = "SELECT ThinnestPoint, AverageThickness FROM model1records"; |
| | | ResultSet resultSet = statement.executeQuery(sql); |
| | | try { |
| | | // 建立连接 |
| | | connection = DriverManager.getConnection(url, user, password); |
| | | // 构建基础 SQL |
| | | |
| | | StringBuilder sql = new StringBuilder("SELECT * FROM ").append(table); |
| | | // 创建 PreparedStatement 对象执行 SQL |
| | | preparedStatement = connection.prepareStatement(sql.toString()); |
| | | resultSet = preparedStatement.executeQuery(); |
| | | ResultSetMetaData metaData = resultSet.getMetaData(); |
| | | int columnCount = metaData.getColumnCount(); |
| | | // 遍历结果集获取数据 |
| | | while (resultSet.next()) { |
| | | double thinnestPoint = resultSet.getDouble("ThinnestPoint"); |
| | | double averageThickness = resultSet.getDouble("AverageThickness"); |
| | | dataList.add(new ThicknessData(thinnestPoint, averageThickness)); |
| | | Map<String, Object> rowData = new HashMap<>(); |
| | | for (int i = 1; i <= columnCount; i++) { |
| | | String columnName = metaData.getColumnName(i); |
| | | rowData.put(columnName, resultSet.getObject(i)); |
| | | } |
| | | dataList.add(rowData); |
| | | } |
| | | // while (resultSet.next()) { |
| | | // double thinnestPoint = resultSet.getDouble("ThinnestPoint"); |
| | | // double averageThickness = resultSet.getDouble("AverageThickness"); |
| | | // dataList.add(new ThicknessData(thinnestPoint, averageThickness)); |
| | | // } |
| | | tableMap.put("data", dataList); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 假设 R 类有 failed 方法,若没有需补充实现 |
| | | return R.failed("数据库查询出错: " + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | if (resultSet != null) { |
| | | resultSet.close(); |
| | | } |
| | | if (preparedStatement != null) { |
| | | preparedStatement.close(); |
| | | } |
| | | if (connection != null) { |
| | | connection.close(); |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return tableMap; |
| | | } |