开发手账(一)

一、 关于设计

(一)数据库

  1. 确定外键标识,需判断该外键是否有可能被修改。如菜单id,菜单code,菜单名,前两者都可做外键,后面一个则不应做外键。

二、关于组件

(一)POI

1. 文档页数统计

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.ofdrw.reader.OFDReader;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
@Slf4j
public class LvDocPageCounter {
    public static final String DOCUMENT_PAGE_TEMP = "DOCUMENT_PAGE_TEMP";
    public static int getPageCount(String filePath) {
        String fileType = getFileType(filePath);
        try {
            switch (fileType) {
                case "pdf":
                    return getPdfPageCount(filePath);
                case "docx":
                    return getDocxPageCount(filePath);
                case "doc":
                    return getDocPageCount(filePath);
                case "ofd":
                    return getOfdPageCount(filePath);
                // Add more cases for other document types as needed
                default:
                    log.warn("不支持的文件类型:{}", filePath);
                    return 1;
//                throw new IllegalArgumentException("Unsupported file type");
            }
        } catch (Exception e) {
            log.warn("读取文件异常:{},{}", filePath,e);
            return 0;
        }
    }

    /**
     * 文件类型
     * @param filePath
     * @return
     */
    private static String getFileType(String filePath) {
        int dotIndex = filePath.lastIndexOf('.');
        if (dotIndex == -1 || dotIndex == filePath.length() - 1) {
            log.warn("文件名中没有找到扩展名:{}", filePath);
            return "";
        }
        return filePath.substring(dotIndex + 1).toLowerCase();
    }
    /**
     * 获取PDF文档页数
     * @param filePath
     * @return
     * @throws IOException
     */
    private static int getPdfPageCount(String filePath) throws IOException {
        try (PDDocument document = Loader.loadPDF(new File(filePath))) {
//            PDDocument document = new PDDocument();
            int numberOfPages = document.getNumberOfPages();
            document.close();
            return numberOfPages;
        }
    }

    /**
     * 获取doc文档页数
     * @param filePath
     * @return
     * @throws IOException
     */
    private static int getDocPageCount(String filePath) throws IOException {
//        try (InputStream inputStream = new FileInputStream(filePath);
//             HWPFDocument document = new HWPFDocument(inputStream)) {
//            int pageCount = document.getSummaryInformation().getPageCount();
//            document.close();
//            return pageCount;
//        }
        try (InputStream inputStream = new FileInputStream(filePath)) {
            com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
            int num = doc.getPageCount();
            doc.cleanup();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 获取docx页数
     * @param filePath
     * @return
     * @throws IOException
     */
    private static int getDocxPageCount(String filePath) throws IOException {
//        try (InputStream inputStream = new FileInputStream(filePath);
//             XWPFDocument document = new XWPFDocument(inputStream)) {
//            int pages = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
//            document.close();
//            return pages;
//        }
        try (InputStream inputStream = new FileInputStream(filePath)) {
            com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
            int num = doc.getPageCount();
            doc.cleanup();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * pdf页数
     * @param filePath
     * @return
     * @throws IOException
     */
    private static int getOfdPageCount(String filePath) throws IOException {
        Path ofdFile = Paths.get(filePath);
        OFDReader ofdReader = new OFDReader(ofdFile);
        int numberOfPages = ofdReader.getNumberOfPages();
        ofdReader.close();
        return numberOfPages;
    }


    /**
     * 获取缓存文件页数
     * @param inputStream
     * @param originalFilename
     * @return
     */
    public static Integer getPageCount(MultipartFile inputStream, String originalFilename) {
        try (InputStream inputStream1 = inputStream.getInputStream()) {
            return getPageCount(inputStream1,originalFilename);
        } catch (IOException e) {
            log.warn("读取文件异常:{},{}", originalFilename,e);
            return 0;
        }
    }

// Add methods for other document types as needed
}

2. 文本提取

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
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.ofdrw.converter.export.TextExporter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author yilv
 * @version 1.0
 * @description: TODO
 * @date 2023/11/16 16:12
 */
@Slf4j
public class LvDocTxTHunter {
    private static AtomicInteger  UPPER_LIMIT=new AtomicInteger(50);
    /**
     * 读取文档内容
     * @param filePath
     * @return
     */
    public static String readText(String filePath) {

        int pageCount = LvDocPageCounter.getPageCount(filePath);
        if (pageCount >UPPER_LIMIT.get()) {
            log.warn("文件过大:{},{}", filePath,pageCount);
            return "";
        }
        String fileType = getFileType(filePath);
        try {
            switch (fileType) {
                case "pdf":
                    return readPdfText(filePath);
                case "doc":
                    return readDocText(filePath);
                case "docx":
                    return readDocxText(filePath);
                case "ofd":
                    return readOfdText(filePath);
                // Add more cases for other document types as needed
                default:
                    log.warn("不支持的文件类型:{}", filePath);
                    return "";
            }
        } catch (IOException e) {
            log.warn("读取文件异常:{},{}", filePath,e);
            return "";
        }

    }

    /**
     * 获取文件类型
     * @param filePath
     * @return
     */
    private static String getFileType(String filePath) {
        int dotIndex = filePath.lastIndexOf('.');
        if (dotIndex == -1 || dotIndex == filePath.length() - 1) {
            log.warn("文件名中没有找到扩展名:{}", filePath);
            return "";
        }
        return filePath.substring(dotIndex + 1).toLowerCase();
    }

    /**
     * 获取pdf文本
     * @param filePath
     * @return
     * @throws IOException
     */
    private static String readPdfText(String filePath) throws IOException {
        try (PDDocument document = Loader.loadPDF(filePath)) {
            String text = new PDFTextStripper().getText(document);
            document.close();
            return text;
        }
    }

    /**
     * 获取doc文本
     * @param filePath
     * @return
     * @throws IOException
     */
    private static String readDocText(String filePath) throws IOException {
        try (InputStream inputStream = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(inputStream)) {
            WordExtractor extractor = new WordExtractor(document);
            String text = extractor.getText();
            document.close();
            return text;
        }
    }

    /**
     * 获取docx文本
     * @param filePath
     * @return
     * @throws IOException
     */
    private static String readDocxText(String filePath) throws IOException {
        try (InputStream inputStream = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(inputStream)) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            String text = extractor.getText();
            document.close();
            return text;
        }
    }
    /**
     * pdf页数
     * @param filePath
     * @return
     * @throws IOException
     */
    private static String readOfdText(String filePath) throws IOException {
        Path txtPath = Paths.get("DOCUMENT_PAGE_TEMP", FilenameUtils.getBaseName(filePath) + ".txt");
        TextExporter textExporter = new TextExporter(Paths.get(filePath), txtPath);
        textExporter.export();
        String s = FileUtil.readUtf8String(txtPath.toFile());
        textExporter.close();
        return s;
    }

    /**
     * 获取文件文本
     * @param tempFile
     * @return
     */
    public static String readText(File tempFile) {
        return readText(tempFile.getPath());
    }
    // Add methods for other document types as needed
}

3. 文案转换

  • ofd转换
    • ①启动加载字体
    /**
     * 前置系统数据加载
     */
    private static void systemInit() {
        FontLoader preload = FontLoader.Preload();
        preload.scanFontDir(Paths.get(FileUtil.local, "font"));
        Field namePathMapping = ReflectUtil.getField(FontLoader.class, "fontNamePathMapping");
        Map<String, String> fontNamePathMapping = (Map<String, String>) ReflectUtil.getFieldValue(preload,namePathMapping);
        System.out.println("加载字体:" + JSONUtil.toJsonStr(fontNamePathMapping.keySet()));
    }
    • ②使用ofdrw进行pdf转换
    /**
     * 将OFD转换为PDF
     *
     * @param ofdPath OFD路径
     * @param distPath 输出路径
     * @param pdfPath 输出PDF路径
     * @throws IOException
     */
    public static void convertOfdToPDFByBridge(String ofdPath, String distPath, String pdfPath) throws IOException {

        log.debug("解析文件:{}",ofdPath);
        Path ofdFilePath = Paths.get(ofdPath);
        Path dir = Paths.get(distPath);
        PDFExporterIText exporter = new PDFExporterIText(ofdFilePath, Paths.get(pdfPath));
        exporter.export();
        exporter.close();
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/137367.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

为什么STM32在中国这么出名?

为什么STM32在中国这么出名&#xff1f; 在STM32之前&#xff0c;都是老大头51&#xff0c;带着它的“小弟们” MSP430、AVR、PIC在单片机界呼风唤雨。那个时候&#xff0c;市场上遍布8位机&#xff0c;大学教材用51入门&#xff0c;个人、企业学单片机基本上也都在用51。 最近…

深入了解Java 8 新特性:Stream流的实践应用(二)

阅读建议 嗨&#xff0c;伙计&#xff01;刷到这篇文章咱们就是有缘人&#xff0c;在阅读这篇文章前我有一些建议&#xff1a; 本篇文章大概8000多字&#xff0c;预计阅读时间长需要10分钟&#xff08;不要害怕字数过多&#xff0c;其中有一大部分是示例代码&#xff0c;读起…

FFmpeg常用命令行讲解及实战一

文章目录 前言一、学习资料参考二、FFmpeg 选项1、主要选项①、主要命令选项②、举例 2、视频选项①、主要命令选项②、举例1&#xff09;提取固定帧2&#xff09;禁止输出视频3&#xff09;指定视频的纵横比 3、音频选项①、主要命令选项②、举例 4、字幕选项①、主要命令选项…

redis的高可用

1.redis的高可用 在集群当中有一个非常重要的指标&#xff0c;提供正常服务的时间的百分比&#xff08;365天&#xff09;99% redis的高可用含义更宽泛&#xff0c;正常服务是指标之一&#xff0c;数据容量的扩展&#xff0c;数据的安全性。 在redis中实现高可用技术&#x…

循环链表3

插入函数——插入数据&#xff0c;在链表plsit的pos位置插入val数据元素 位置pos&#xff08;在无特别说明的情况下&#xff09;是从0开始计数的 要改变链表结构&#xff0c;就要依赖前驱&#xff0c;每个前驱的next存储着下一个数据结点的地址&#xff0c;也就是依靠前驱的ne…

数据类型扩展02

1、字符串拓展 所有的字符本质还是数字。 char c1 a;char c2 中;System.out.println("c1:"c1);System.out.println("c1转换:"(int)c1);System.out.println("c2:"c2);System.out.println("c2转换:"(int)c2); 执行结果 c1:a c1转换:…

Java修仙记之记录一次与前端女修士论道的经历

文章开始之前&#xff0c;想跟我念一句&#xff1a;福生无量天尊&#xff0c;无量寿佛&#xff0c;阿弥陀佛 第一场论道&#xff1a;id更新之争 一个天气明朗的下午&#xff0c;前端的小美女长发姐告诉我&#xff1a;嘿&#xff0c;小后端&#xff0c;你的代码报错了 我答道&am…

3.6 Windows驱动开发:内核进程汇编与反汇编

在笔者上一篇文章《内核MDL读写进程内存》简单介绍了如何通过MDL映射的方式实现进程读写操作&#xff0c;本章将通过如上案例实现远程进程反汇编功能&#xff0c;此类功能也是ARK工具中最常见的功能之一&#xff0c;通常此类功能的实现分为两部分&#xff0c;内核部分只负责读写…

通过AppLink把拼多多热门榜单商品同步至小红书

上篇说到AppLink当中定时调度方式如何配置&#xff0c;这次来演示一下&#xff0c;如何把热门榜单信息同步至小红书 1.拉取一个定时器作为触发动作&#xff0c;通过配置定时器调度时间将定时策略配置为每天执行一次 2.触发动作完成后通过好单库获取拼多多每日热门榜单&#xf…

单元测试实战(四)MyBatis-Plus 的测试

为鼓励单元测试&#xff0c;特分门别类示例各种组件的测试代码并进行解说&#xff0c;供开发人员参考。 本文中的测试均基于JUnit5。 单元测试实战&#xff08;一&#xff09;Controller 的测试 单元测试实战&#xff08;二&#xff09;Service 的测试 单元测试实战&am…

人机交互复习专题

第一章概述 1.1人机交互的概念与理解 人机交互的概念与理解 人机交互是人与机器进行交互的操作方式&#xff0c;即用户与机器互相传递信息的媒介。好的人机交互界面美观且通俗易懂、操作简单有引导功能&#xff0c;使用户感受到愉快、有兴趣&#xff0c;从而提升使用效率。 美…

【阿里云】图像识别

一、阿里云官网资料及配置本地 二、配置环境变量 三、C语言调用阿里云Python接口 一、阿里云官网资料及配置本地 阿里云官网 垃圾识别分类 sudo apt install python3-pip pip3 install alibabacloud_imagerecog20190930可能出现的网络问题 二、配置环境变量 配置环境变量A…

猫罐头牌子哪个好一点?精选5款口碑好的猫罐头推荐!

猫罐头牌子哪个好一点&#xff1f;选择猫罐头是十分重要的事情&#xff0c;千万不能将就。因为&#xff0c;好的猫罐头不仅可以营养丰富&#xff0c;水分充足&#xff0c;适口性好&#xff0c;还能易吸收。而一旦选择错误&#xff0c;不仅无法达到上述效果&#xff0c;还可能产…

在Python中调用imageJ开发

文章目录 一、在ImageJ中进行Python开发二、在Python中调用imageJ开发2.1、简介2.2、环境配置2.3、测试一2.4、测试二 Python imageJ 解决方案&#xff0c;采坑记录 一、在ImageJ中进行Python开发 原生ImageJ仅支持JS脚本&#xff08;JAVAScript&#xff09;&#xff0c;而Im…

虾皮网同行数据丨虾皮数据工具-知虾:监控竞争对手数据的利器

在如今的电商竞争激烈的市场中&#xff0c;了解竞争对手的销售情况和策略对于制定自己的营销策略至关重要。虾皮网作为一家知名的电商平台&#xff0c;提供了一款强大的同行数据工具-知虾&#xff0c;可以帮助卖家监控竞争对手的数据&#xff0c;为自己的业务发展提供有力支持。…

请收藏!2023年全栈开发人员实战进阶指南终极版

全栈工程师在过去十年中越来越受到欢迎&#xff0c;而且在国内的就业环境下&#xff0c;它是更适合从技术转管理的职位。 但究竟什么是全栈工程师&#xff1f;他需要哪些技术能力&#xff1f;如何才能成为一名优秀的全栈工程师&#xff1f;今天这篇文章就给大家全方位分享一下…

去除IDEA中代码的波浪线(黄色警示线)

去除IDEA中代码的波浪线 首先是点击File—>Settings 操作如下图所示: 然后点击Editor—>Inspections—>General—>Duplicated code fragment(去掉勾选)—>Apply—>OK 即可,详情请看下图所示:

构建和应用卡尔曼滤波器 (KF)--扩展卡尔曼滤波器 (EKF)

作为一名数据科学家&#xff0c;我们偶尔会遇到需要对趋势进行建模以预测未来值的情况。虽然人们倾向于关注基于统计或机器学习的算法&#xff0c;但我在这里提出一个不同的选择&#xff1a;卡尔曼滤波器&#xff08;KF&#xff09;。 1960 年代初期&#xff0c;Rudolf E. Kal…

华为云cce中环境变量的使用

如上图&#xff0c;cce中的环境变量可配置。 配置后的这些参数怎么用呢&#xff1f; 我们可以在docker打包前在springboot的配置文件中配置&#xff0c;cce在启动的时候会调用环境变量中的设置。 如上图&#xff0c;配置的东西以key值标记&#xff0c;冒号后面的是默认配置项…

jenkins-2.426.1-1.1.noarch.rpm 的公钥没有安装

执行命令 yum install jenkins 报错 jenkins-2.426.1-1.1.noarch.rpm 的公钥没有安装 下载的软件包保存在缓存中&#xff0c;直到下次成功执行事务。 您可以通过执行 yum clean packages 删除软件包缓存。 错误&#xff1a;GPG 检查失败 解决办法&#xff1a; 1、安装新的公…
最新文章