| | |
| | | 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 org.apache.commons.lang3.ObjectUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.POIXMLDocument; |
| | | import org.apache.poi.POIXMLTextExtractor; |
| | | import org.apache.poi.hssf.usermodel.HSSFPicture; |
| | | import org.apache.poi.hssf.usermodel.HSSFPictureData; |
| | | import org.apache.poi.hssf.usermodel.HSSFSheet; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.hwpf.extractor.WordExtractor; |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | | import org.apache.poi.openxml4j.opc.OPCPackage; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.apache.poi.xwpf.extractor.XWPFWordExtractor; |
| | | import java.awt.Color; |
| | | import java.awt.image.BufferedImage; |
| | | 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 { |
| | |
| | | return result; |
| | | } |
| | | |
| | | public static Object readExcelFile(File file) throws FileNotFoundException, IOException { |
| | | public static Object readExcelFile(File file) throws IOException { |
| | | StringBuilder result = new StringBuilder(); |
| | | //å建工ä½ç°¿å¯¹è±¡ |
| | | XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file)); |
| | | XSSFWorkbook xssfWorkbook = new XSSFWorkbook(Files.newInputStream(file.toPath())); |
| | | //è·åå·¥ä½ç°¿ä¸sheetçä¸ªæ° åªè¯»å第ä¸ä¸ªsheet |
| | | // int sheetNum = xssfWorkbook.getNumberOfSheets(); |
| | | //éåå·¥ä½ç°¿ä¸çæææ°æ® |
| | | for (int i = 0; i < 1; i++) { |
| | | XSSFSheet sheet = xssfWorkbook.getSheetAt(i); |
| | | XSSFSheet sheet = xssfWorkbook.getSheetAt(0); |
| | | //è·åæåä¸è¡çnumï¼å³æ»è¡æ°ãæ¤å¤ä»0å¼å§ |
| | | int maxRow = sheet.getLastRowNum(); |
| | | for (int row = 0; row <= maxRow; row++) { |
| | | for (int row = 1; row <= maxRow; row++) { |
| | | //è·åæååå
æ ¼numï¼å³æ»åå
æ ¼æ° ***注æï¼æ¤å¤ä»1å¼å§è®¡æ°*** |
| | | int maxRol = sheet.getRow(row).getLastCellNum(); |
| | | StringBuilder aLine = new StringBuilder(); |
| | |
| | | } |
| | | result.append(aLine).append("\n"); |
| | | } |
| | | } |
| | | return result.toString(); |
| | | } |
| | | |
| | | public static Object readExcelxlsFile(File file) throws IOException { |
| | | StringBuilder result = new StringBuilder(); |
| | | String result = ""; |
| | | try (FileInputStream fis = new FileInputStream(file); |
| | | Workbook workbook = new HSSFWorkbook(fis)) { |
| | | // è·å第ä¸ä¸ªå·¥ä½è¡¨ |
| | |
| | | if (workbook instanceof HSSFWorkbook) { |
| | | HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; |
| | | List<HSSFPictureData> pictures = hssfWorkbook.getAllPictures(); |
| | | for (HSSFPictureData picture : pictures) { |
| | | //å¤çæåä¸å¼ å¾çæ°æ® |
| | | HSSFPictureData lastPicture = pictures.get(pictures.size()-1); |
| | | // è·åå¾çç±»å |
| | | String pictureType = picture.suggestFileExtension(); |
| | | String pictureType = lastPicture.suggestFileExtension(); |
| | | // è·åå¾çæ°æ® |
| | | byte[] pictureData = picture.getData(); |
| | | byte[] pictureData = lastPicture.getData(); |
| | | // åå»ºä¸´æ¶æä»¶ |
| | | File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType); |
| | | try (FileOutputStream fos = new FileOutputStream(tempFile)) { |
| | | fos.write(pictureData); |
| | | } |
| | | // å¾çé¢å¤ç |
| | | // File processedFile = preprocessImage(tempFile, pictureType); |
| | | // è°ç¨ readPngFile1 æ¹æ³è¯»åå¾çæåä¿¡æ¯ |
| | | String ocrResult = ""; |
| | | try { |
| | | ocrResult = (String) readPngFile1(tempFile); |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | // ocrResult = (String) readPngFile(processedFile); |
| | | ocrResult = (String) readPngFile(tempFile); |
| | | } catch (TesseractException e) { |
| | | ocrResult = "OCRè¯å«å¤±è´¥: " + e.getMessage(); |
| | | } finally { |
| | | // å é¤ä¸´æ¶æä»¶ |
| | | tempFile.delete(); |
| | | // processedFile.delete(); |
| | | } |
| | | result = ocrResult; |
| | | // String ocrText = fixOcrText(ocrResult); |
| | | // result.append("OCR Result:").append(ocrText).append(","); |
| | | |
| | | // å°å¾çä¿¡æ¯æ·»å å°ç»æä¸ |
| | | // result.append("Picture Type: ").append(pictureType) |
| | | // .append(", Picture Size: ").append(pictureData.length) |
| | | // .append(" bytes") |
| | | // .append(", OCR Result: ").append(ocrResult) |
| | | // .append(","); |
| | | String ocrText = fixOcrText(ocrResult); |
| | | result.append("OCR Result:").append(ocrText).append(","); |
| | | } |
| | | |
| | | // for (HSSFPictureData picture : pictures) { |
| | | // // è·åå¾çç±»å |
| | | // String pictureType = picture.suggestFileExtension(); |
| | | // // è·åå¾çæ°æ® |
| | | // byte[] pictureData = picture.getData(); |
| | | // // åå»ºä¸´æ¶æä»¶ |
| | | // File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType); |
| | | // try (FileOutputStream fos = new FileOutputStream(tempFile)) { |
| | | // fos.write(pictureData); |
| | | // } |
| | | // // å¾çé¢å¤ç |
| | | //// File processedFile = preprocessImage(tempFile, pictureType); |
| | | // // è°ç¨ readPngFile1 æ¹æ³è¯»åå¾çæåä¿¡æ¯ |
| | | // String ocrResult = ""; |
| | | // try { |
| | | // ocrResult = (String) readPngFile(tempFile); |
| | | //// ocrResult = (String) readPngFile(tempFile); |
| | | //// ocrResult = (String) readPngFile(processedFile); |
| | | // } catch (TesseractException e) { |
| | | // ocrResult = "OCRè¯å«å¤±è´¥: " + e.getMessage(); |
| | | // } finally { |
| | | // // å é¤ä¸´æ¶æä»¶ |
| | | //// tempFile.delete(); |
| | | //// processedFile.delete(); |
| | | // } |
| | | // |
| | | // // å°å¾çä¿¡æ¯æ·»å å°ç»æä¸ |
| | | //// result.append("Picture Type: ").append(pictureType) |
| | | //// .append(", Picture Size: ").append(pictureData.length) |
| | | //// .append(" bytes") |
| | | //// .append(", OCR Result: ").append(ocrResult) |
| | | //// .append(","); |
| | | // String ocrText = fixOcrText(ocrResult); |
| | | // result.append("OCR Result:").append(ocrText).append(","); |
| | | // } |
| | | } |
| | | // |
| | | // // é忝ä¸è¡ |
| | |
| | | return tesseract.doOCR(file); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public static Object readTxtFile(File file) throws IOException { |
| | | FileInputStream fin = new FileInputStream(file); |
| | | InputStreamReader reader = new InputStreamReader(fin); |
| | |
| | | |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | // å建 reader |
| | | // 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()) { |
| | | |
| | |
| | | } |
| | | tableMap.put("data", list); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | closeA1l(conn, preparedStatement, rs); |
| | | } |
| | |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | |
| | | public static Object getMysqlFile(GetFileDto getFileDto){ |
| | | Map<String, Object> tableMap = new HashMap<>(16); |
| | | // ä» GetFileDto è·åæ°æ®åºåï¼å¯¹åºãæä»¶åç§°ãåæ®µ |
| | |
| | | return R.failed("æ°æ®åºåæè¡¨åä¸è½ä¸ºç©º"); |
| | | } |
| | | // æ°æ®åºè¿æ¥ä¿¡æ¯ |
| | | String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; |
| | | String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&characterEncoding=utf8"; |
| | | Connection connection = null; |
| | | PreparedStatement preparedStatement = null; |
| | | ResultSet resultSet = null; |
| | |
| | | connection = DriverManager.getConnection(url, user, password); |
| | | // æå»ºåºç¡ SQL |
| | | |
| | | StringBuilder sql = new StringBuilder("SELECT * FROM ").append(table); |
| | | String sql = "SELECT * FROM "+table+" WHERE 1=1"; |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode())){ |
| | | sql+=" AND " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode()+ "'"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){ |
| | | sql+=" AND " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'"; |
| | | } |
| | | // å建 PreparedStatement 对象æ§è¡ SQL |
| | | preparedStatement = connection.prepareStatement(sql.toString()); |
| | | preparedStatement = connection.prepareStatement(sql); |
| | | resultSet = preparedStatement.executeQuery(); |
| | | ResultSetMetaData metaData = resultSet.getMetaData(); |
| | | int columnCount = metaData.getColumnCount(); |
| | |
| | | } |
| | | 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(); |