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

上传及接收pdf文件,使用pdfbox读取pdf文件内容

前端上传pdf文件

html

<form class="layui-form"><div style="background-color: #ffffff" ><div style="padding: 30px"><div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label"><label style="color: red">*</label>编号:</label><div class="layui-input-inline"><input type="text" class="layui-input" name="orderNo" id="orderNo" ></div></div></div><div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label "><label style="color: red">*</label>上传文件:</label><div class="layui-inline layui-input-inline"><input type="file" name="printFile" id="printFile"></div></div></div><div class="layui-form-item"><div class="layui-input-block"><button type="button" class="layui-btn layui-btn-sm" id="save-button" onclick="save()">提交</button></div></div></div></div>
</form>

js

function save(){var formData = new FormData();formData.appen("orderNo",$("#orderNo").val())var files = document.getElementById('printFile');if(files.files && files.files.length > 1){formData.append('file',files.files[0])}else{layer.msg("文件不能为空");return false;}formData.append('file',files.files[0])console.log("formData",formData)var url = ""HSoft.postMultipartForm(url), formData,function(res){console.log(res)if ("200" == res.code) {layer.msg(res.data);setTimeout(function(){layer.closeAll('loading');},1000)} else {layer.msg("提交失败",res.msg);}});
}this.postMultipartForm = function(url,formData,fn,errorCallback){$.ajax({type: "POST",url: url,data: formData,/***必须false才会自动加上正确的Content-Type*/contentType: false,/*** 必须false才会避开jQuery对 formdata 的默认处理* XMLHttpRequest会对 formdata 进行正确的处理*/processData: false,error: function (XHR, textStatus, errorThrown) {console.log("XHR=" + XHR + "\ntextStatus=" + textStatus + "\nerrorThrown=" + errorThrown);if (errorCallback && typeof errorCallback == "function") {errorCallback({});}},success: function (data, textStatus) {if (fn && typeof fn == "function") {fn(data);}}});
};

后台接收文件

安装pdfbox依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version>
</dependency>

controller

@PostMapping("/uploadFile")
@ApiOperation("上传文件")
@ResponseBody
public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request){DataRespVO respVO = orderService.uploadFile(file,request);if(Objects.equals(respVO.getCode(), 200)){return CommonResult.success(respVO.getMessage());}return CommonResult.error(respVO.getCode(), respVO.getMessage());
}

service

@Override
public DataRespVO uploadFile(MultipartFile file, HttpServletRequest request) {MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);String orderNo = params.getParameter("orderNo ");if(HSoft.isEmpty(orderNo )){return new DataRespVO(ErrorCodeConstants.ORDER_NO_IS_EMPTY);}PDDocument document = null;try{// 读取标签文件内容String fileName = file.getOriginalFilename();document = PDDocument.load(file.getInputStream());int pageSize = document.getNumberOfPages();String text = "";// 一页一页读取for (int i = 0; i < pageSize; i++) {// 文本内容PDFTextStripper stripper = new PDFTextStripper();// 设置按顺序输出stripper.setSortByPosition(true);stripper.setStartPage(i + 1);stripper.setEndPage(i + 1);text = stripper.getText(document);System.out.println(text.trim());}}catch (Exception e){log.error(e.getMessage(),e);return new DataRespVO((new ErrorCode(10000001,"数据更新失败,"+e.getMessage())));} finally {try {if (document != null) {document.close();}} catch (IOException e) {log.error(e.getMessage(),e);return new DataRespVO((new ErrorCode(10000001,"数据更新失败,"+e.getMessage())));}}return new DataRespVO(ErrorCodeConstants.UPDATE_OK);
}
http://www.lryc.cn/news/434369.html

相关文章:

  • 第一个搭建SpringBoot项目(连接mysql)
  • docker部署rabbitMQ 单机版
  • PDF 全文多语言 AI 摘要 API 数据接口
  • 《信息系统安全》课程实验指导
  • Accelerated Soft Error Testing 介绍
  • Redis缓存常用的读写策略
  • 9月产品更新 | 超10项功能升级,快来看看你的需求上线了吗?
  • ARP协议工作原理析解 (详细抓包分析过程)
  • axure动态面板
  • [论文笔记]Making Large Language Models A Better Foundation For Dense Retrieval
  • Linux平台屏幕|摄像头采集并实现RTMP推送两种技术方案探究
  • 梧桐数据库|中秋节活动·抽奖领取大闸蟹
  • Python怎么发送邮件:基础步骤与详细教程?
  • [译] 大模型推理的极限:理论分析、数学建模与 CPU/GPU 实测(2024)
  • vue3 响应式 API:readonly() 与 shallowReadonly()
  • 迁移学习与知识蒸馏对比
  • 【Java-反射】
  • 移动UI设计要求越来越高,最为设计师应如何迎头赶上
  • 大数据-121 - Flink Time Watermark 详解 附带示例详解
  • 国行 iPhone 15 Pro 开启苹果 Apple Intelligence 教程
  • conda、anaconda、pip、torch、pytorch、tensorflow到底是什么东西?(转载自本人的知乎回答)
  • 数据库系列之GaussDB数据库中逻辑对象关系简析
  • 如何进行不同数据库的集群操作?--从部署谈起,今天来看MySQL和NoSql数据库Redis的集群
  • 第 6 章图像聚类
  • HC-SR501人体红外传感器详解(STM32)
  • 关于武汉芯景科技有限公司的IIC电平转换芯片XJ9517开发指南(兼容PCF9517)
  • C语言:scanf()、getchar()、gets()
  • 基于MATLAB的全景图像拼接系统实现
  • AI模型“减肥”风潮:量化究竟带来了什么?
  • 第四届“长城杯”网络安全大赛 暨京津冀网络安全技能竞赛(初赛) 全方向 题解WriteUp