| | |
| | | package com.smtaiserver.smtaiserver.core;
|
| | |
|
| | | import java.io.ByteArrayOutputStream;
|
| | | import java.io.InputStream;
|
| | | import java.io.*;
|
| | | import java.sql.Connection;
|
| | | import java.sql.DriverManager;
|
| | | import java.util.ArrayList;
|
| | |
| | |
|
| | | import org.apache.logging.log4j.LogManager;
|
| | | import org.apache.logging.log4j.Logger;
|
| | | import org.apache.pdfbox.pdmodel.PDDocument;
|
| | | import org.apache.pdfbox.text.PDFTextStripper;
|
| | | import org.apache.poi.hwpf.HWPFDocument;
|
| | | import org.apache.poi.hwpf.extractor.WordExtractor;
|
| | | import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
|
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
| | | import org.dom4j.Document;
|
| | | import org.dom4j.io.OutputFormat;
|
| | | import org.dom4j.io.XMLWriter;
|
| | |
| | | import org.springframework.web.context.WebApplicationContext;
|
| | |
|
| | | import com.alibaba.druid.pool.DruidDataSource;
|
| | | import com.smtaiserver.smtaiserver.attach.SMTAIAttachMetricDef;
|
| | | import com.smtaiserver.smtaiserver.attach.SMTAIAttachTableDef;
|
| | | import com.smtaiserver.smtaiserver.database.SMTDatabase;
|
| | | import com.smtaiserver.smtaiserver.gismap.SMTGisMapLayerDef;
|
| | |
| | | import net.sf.ehcache.CacheManager;
|
| | |
|
| | | import com.smtservlet.core.SMTApp.SMTEhCacheManagerInitialize;
|
| | | import org.springframework.web.multipart.MultipartFile;
|
| | |
|
| | |
|
| | | public class SMTAIServerApp extends SMTApp implements SMTEhCacheManagerInitialize,SMTApp.SMTSessionListenerEvent
|
| | |
| | | "getMapVPropDefMap",
|
| | | "getMapThemeTableDefMap",
|
| | | "getMapThemeDefMap",
|
| | | "getAttachTableDefMap"
|
| | | "getAttachTableDefMap",
|
| | | "getAttachMetricDefMap"
|
| | | };
|
| | |
|
| | | public static SMTAIServerApp getApp()
|
| | |
| | | Map<String, SMTMetricsDef> mapResult = mapMapMetrics.get(groupId);
|
| | |
|
| | | if(mapResult == null)
|
| | | throw new Exception("can't find metrics group : " + groupId);
|
| | | return new HashMap<String, SMTMetricsDef>();
|
| | |
|
| | | return mapResult;
|
| | | }
|
| | |
| | | _serverEncache.getMapThemeTableDefMap();
|
| | | _serverEncache.getMapThemeDefMap();
|
| | | _serverEncache.getAttachTableDefMap();
|
| | | _serverEncache.getAttachMetricDefMap();
|
| | | _serverEncache.getQwenAgentManager();
|
| | | }
|
| | |
|
| | | public SMTMapTableDef getMapTableDef(String tableId) throws Exception
|
| | |
| | |
|
| | | return attachTableDef;
|
| | | }
|
| | | |
| | | public Map<String, SMTAIAttachMetricDef> getAttachMetricDefMap() throws Exception
|
| | | {
|
| | | return _serverEncache.getAttachMetricDefMap();
|
| | | }
|
| | | |
| | | public SMTAIAttachMetricDef getAttachMetricDef(String id) throws Exception
|
| | | {
|
| | | SMTAIAttachMetricDef attachMetricDef = _serverEncache.getAttachMetricDefMap().get(id);
|
| | | if(attachMetricDef == null)
|
| | | throw new Exception("can't find attach metric def : " + id);
|
| | | |
| | | return attachMetricDef;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 文件(word、pdf)转txt
|
| | | */
|
| | | public static String fileTranslTxt(MultipartFile file) throws Exception {
|
| | | InputStream inputStream = file.getInputStream();
|
| | | try (BufferedInputStream bis = new BufferedInputStream(file.getInputStream())) {
|
| | | String mimeType = file.getContentType();
|
| | | if (mimeType != null) {
|
| | | switch (mimeType) {
|
| | | case "application/pdf":
|
| | | _logger.info("文件类型是 PDF");
|
| | | PDDocument document = PDDocument.load(inputStream);
|
| | | PDFTextStripper stripper = new PDFTextStripper();
|
| | | return stripper.getText(document);
|
| | | case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| | | _logger.info("文件类型是 Word (DOCX)");
|
| | | try (XWPFDocument docx = new XWPFDocument(bis)) {
|
| | | XWPFWordExtractor docxExtractor = new XWPFWordExtractor(docx);
|
| | | return docxExtractor.getText();
|
| | | }
|
| | | case "application/msword":
|
| | | _logger.info("文件类型是 Word (DOC)");
|
| | | try (HWPFDocument doc = new HWPFDocument(bis)) {
|
| | | WordExtractor wordExtractor = new WordExtractor(doc);
|
| | | return wordExtractor.getText();
|
| | | }
|
| | | case "text/plain":
|
| | | _logger.info("文件类型是 TXT");
|
| | | byte[] fileData = readAttachFile(file);
|
| | | return new String(fileData, "UTF-8");
|
| | | default:
|
| | | _logger.info("未知文件类型: " + mimeType);
|
| | | break;
|
| | | }
|
| | | } else {
|
| | | _logger.info("无法获取文件类型");
|
| | | }
|
| | | } catch (Exception e) {
|
| | | _logger.error("文件处理失败: " + e.getMessage());
|
| | | throw new Exception("文件处理失败: " + e.getMessage());
|
| | | }
|
| | | return "";
|
| | | }
|
| | | public static byte[] readAttachFile(MultipartFile file) throws Exception
|
| | | {
|
| | | InputStream is = file.getInputStream();
|
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
| | | byte[] data = new byte[1024 * 1024];
|
| | | while(true)
|
| | | {
|
| | | int size = is.read(data);
|
| | | if(size <= 0)
|
| | | return bos.toByteArray();
|
| | |
|
| | | bos.write(data, 0, size);
|
| | | }
|
| | | }
|
| | |
|
| | | }
|