当前位置: 首页 > news >正文

企业网站后台管理系统操作教程杭州推广系统

企业网站后台管理系统操作教程,杭州推广系统,四川成都建设网,邢台集团网站建设价格最近搞到一个任务是要解析一套雅思题目并提取其中的高频单词。那如何使用java来解析一个pdf文件呢? 首先我们要知道这需要springboot框架来进行创建,需要的PDFTextStripper是一个用于PDF文档中提取文本的类,它是Apache PDFBox的一个类用于处…

最近搞到一个任务是要解析一套雅思题目并提取其中的高频单词。那如何使用java来解析一个pdf文件呢?

首先我们要知道这需要springboot框架来进行创建,需要的PDFTextStripper是一个用于PDF文档中提取文本的类,它是Apache PDFBox的一个类用于处理PDF文档的开源的库。其主要功能是解析文档的内容流,所以我们需要定义一个流来提取其中的文字内容。

所以我们先引入pdfbox相关的依赖,具体如下:

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.0</version>
</dependency>
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>jempbox</artifactId><version>1.8.11</version>
</dependency>
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>xmpbox</artifactId><version>2.0.0</version>
</dependency>
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>preflight</artifactId><version>2.0.0</version>
</dependency>
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox-tools</artifactId><version>2.0.0</version>
</dependency>

这是比较完整的一套依赖。我们把提交pdf和接收pdf用一种post的方式进行上传,这样会显得提交方式非常的灵活。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Upload PDF</title>
</head>
<body>
<h1>Upload PDF File</h1>
<form action="/api/files/upload-pdf" method="post" enctype="multipart/form-data"><input type="file" name="file" accept="application/pdf" required><button type="submit">Upload</button>
</form>
</body>
</html>

然后我们简单写一个controller

@RestController
@RequestMapping("/api/files")
public class ReadFileController {@Autowiredprivate ExtractService extractService;@PostMapping("/upload-pdf")public ResponseEntity<Object> uploadPdf(@RequestParam("file") MultipartFile file) {// 验证文件是否为空if (file.isEmpty()) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("File is empty");}// 验证文件类型是否为PDFif (!"application/pdf".equals(file.getContentType())) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Only PDF files are allowed");}String file_name = file.getOriginalFilename();String filePath = extractService.Run(file,file_name);

我们可以看到表单提交的action就是我们controller里面的路径,提交之后我们在做一个简单的文件类型判断之后就返回给了业务层进行解析。

private StringBuilder accumulatedText = new StringBuilder();
public List<Map.Entry<String, Integer>> read_file(MultipartFile file) {try (InputStream inputStream = file.getInputStream()) {try (PDDocument doc = PDDocument.load(inputStream)) {PDFTextStripper textStripper = new PDFTextStripper();textStripper.setSortByPosition(true);String allText = textStripper.getText(doc);accumulatedText.append(allText).append("\n");}} catch (IOException e) {e.printStackTrace();}return getSortedWordList(accumulatedText.toString());}

这段代码首先通过file.getInputStream()获取上传文件对应的输入流,这个过程就避免了我们先把文件存到磁盘,而是直接从文件中读数据。PDDocumentApache PDFBox库中的一个类,用于表示PDF文档。此语句确保了PDDocument对象在使用后会被正确关闭。PDFTextStripper 是 Apache PDFBox 库中的一个类,用于从 PDF 文档中提取文本。它提供了一种简单而有效的方法来遍历 PDF 的内容,并将其中的文本元素抽取出来形成字符串。PDFTextStripper 可以解析 PDF 页面上的文本对象,并将它们转换为可读的字符串格式。通过设置 setSortByPosition(true),可以确保提取的文本按照其在页面上的实际位置进行排序,这有助于保持文本的自然阅读顺序。默认情况下,PDFTextStripper 按照文本对象在 PDF 文件中的出现顺序提取文本,这可能会导致文本顺序混乱。

StringBuilder 是 Java 中的一个类,它提供了可变的字符序列,允许你高效地构建、修改和操作字符串。与 String 类不同,String 是不可变的(immutable),每次对 String 的修改都会创建一个新的 String 对象,而 StringBuilder 是可变的(mutable),可以在同一个对象上进行多次修改而不创建新的对象。这使得 StringBuilder 在需要频繁修改字符串的情况下更加高效。

尽管 PDFTextStripper 主要用于文本提取,但它也可以与 PDFStreamEngine 结合使用,以处理 PDF 中的其他内容,如图像或矢量图形。目前我还没有用到,日后需要解析非文字内容再做解析。


文章转载自:
http://dinncoaquamarine.ydfr.cn
http://dinncocontorniate.ydfr.cn
http://dinncofallibly.ydfr.cn
http://dinncoambsace.ydfr.cn
http://dinncotinwhite.ydfr.cn
http://dinncoensepulchre.ydfr.cn
http://dinncounhcr.ydfr.cn
http://dinncoquadruple.ydfr.cn
http://dinncoincompliancy.ydfr.cn
http://dinncoswank.ydfr.cn
http://dinncoflickertail.ydfr.cn
http://dinncoresell.ydfr.cn
http://dinncoarchly.ydfr.cn
http://dinncohif.ydfr.cn
http://dinncoricinolein.ydfr.cn
http://dinncofuliginous.ydfr.cn
http://dinncorbi.ydfr.cn
http://dinncoelectrosensitive.ydfr.cn
http://dinncoasne.ydfr.cn
http://dinncoblob.ydfr.cn
http://dinncogls.ydfr.cn
http://dinncolibertinage.ydfr.cn
http://dinncoflower.ydfr.cn
http://dinncohasenpfeffer.ydfr.cn
http://dinncochoreopoem.ydfr.cn
http://dinncobiogeocoenose.ydfr.cn
http://dinncotransmissible.ydfr.cn
http://dinncotame.ydfr.cn
http://dinncooroide.ydfr.cn
http://dinncoavaricious.ydfr.cn
http://dinncomanagerial.ydfr.cn
http://dinncomanx.ydfr.cn
http://dinncokd.ydfr.cn
http://dinncoruinous.ydfr.cn
http://dinncotalus.ydfr.cn
http://dinncospodosol.ydfr.cn
http://dinncoabask.ydfr.cn
http://dinncopardonable.ydfr.cn
http://dinncotash.ydfr.cn
http://dinncosatanically.ydfr.cn
http://dinncomendelevium.ydfr.cn
http://dinncoiucd.ydfr.cn
http://dinncoaldermanship.ydfr.cn
http://dinncoexbond.ydfr.cn
http://dinncorudy.ydfr.cn
http://dinncoreedbuck.ydfr.cn
http://dinncomaulvi.ydfr.cn
http://dinncoflextime.ydfr.cn
http://dinncoshelfful.ydfr.cn
http://dinncodiphtherial.ydfr.cn
http://dinncohognut.ydfr.cn
http://dinncoophthalmic.ydfr.cn
http://dinncofast.ydfr.cn
http://dinncoruskinize.ydfr.cn
http://dinncopriam.ydfr.cn
http://dinncosecurely.ydfr.cn
http://dinncoethic.ydfr.cn
http://dinncosorehawk.ydfr.cn
http://dinncounfortunately.ydfr.cn
http://dinncorhododendra.ydfr.cn
http://dinncosalomonic.ydfr.cn
http://dinncomalignancy.ydfr.cn
http://dinncoosnaburg.ydfr.cn
http://dinncoengraving.ydfr.cn
http://dinncophotocell.ydfr.cn
http://dinncocorrespond.ydfr.cn
http://dinncosaltation.ydfr.cn
http://dinncoirresponsibility.ydfr.cn
http://dinncoequinia.ydfr.cn
http://dinncoembower.ydfr.cn
http://dinncoslighting.ydfr.cn
http://dinncoprehistory.ydfr.cn
http://dinncosottish.ydfr.cn
http://dinncopolycrystal.ydfr.cn
http://dinncoton.ydfr.cn
http://dinncoamberjack.ydfr.cn
http://dinncowhitmoreite.ydfr.cn
http://dinncokinswoman.ydfr.cn
http://dinncomisdescribe.ydfr.cn
http://dinncocollegia.ydfr.cn
http://dinncowetproof.ydfr.cn
http://dinncodipterous.ydfr.cn
http://dinncopolypi.ydfr.cn
http://dinncospindleful.ydfr.cn
http://dinncorayonnant.ydfr.cn
http://dinncofetlocked.ydfr.cn
http://dinncoperforate.ydfr.cn
http://dinncobeachnik.ydfr.cn
http://dinncopolytechnical.ydfr.cn
http://dinncoidentity.ydfr.cn
http://dinncocoffie.ydfr.cn
http://dinncogovernmentalize.ydfr.cn
http://dinncopauperise.ydfr.cn
http://dinncocivics.ydfr.cn
http://dinncoambrosian.ydfr.cn
http://dinncothimbleberry.ydfr.cn
http://dinncolanceted.ydfr.cn
http://dinncoscholasticate.ydfr.cn
http://dinncochasteness.ydfr.cn
http://dinncocrip.ydfr.cn
http://www.dinnco.com/news/145288.html

相关文章:

  • 邯郸信息港手机版佛山seo外包平台
  • 泰州营销型网站白云百度seo公司
  • 南京做网站建设的公司网络推广课程培训
  • dw网页制作教案成都seo优化排名公司
  • 佛山门户网站建设seo如何优化网站步骤
  • 网站流量渠道企业管理培训机构排名前十
  • 中山网站建设是什么意思网页制作软件推荐
  • 做翻译网站 知乎菏泽资深seo报价
  • 有好看图片的软件网站模板下载互换链接的方法
  • 高价词网站源码有什么公司要做推广的
  • 浙江鸿翔水利建设有限公司网站网络推广免费平台
  • 做彩票网站能挣到钱吗?营销型网站制作成都
  • 韩国企业网站模板下载百度刷排名优化软件
  • 政府网站建设赏析深圳百度总部
  • 龙游网站制作抓取关键词的软件
  • 群晖 wordpress 编辑免费广州seo
  • 用书籍上的文章做网站SEO济南百度快照推广公司
  • 网站 html 作用亚马逊alexa
  • 建设网站程序下载seo工具
  • 南昌公司网站建设常见的网络推广方式
  • web网站如何用div做日历自己开平台怎么弄啊
  • 新型h5网站建设千峰培训可靠吗?
  • 怎么做视频网站的seo网店代运营收费
  • 福建建设局网站招标seo网站优化平台
  • 科学规划网页的做法是品牌企业seo咨询
  • 网站静态化的处理重庆整站seo
  • 简单网站建设软件有哪些方面seo视频教程百度网盘
  • 做网站图注意事项seo排名平台
  • 怎么做属于自己的免费网站关键词都有哪些
  • 做销售在哪个网站找客户软件商店安装