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

java springboot2.7 写一个本地 pdf 预览的接口

依赖方面 创建的是 接口web项目就好了

然后包管理工具打开需要这些

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

然后 例如这里我要预览 本地 E盘下的 ydzxmgf下的git分支管理规范.pdf文件 就可以这样写

@GetMapping("/pdf")public ResponseEntity<Resource> previewPdfFile() throws IOException {// 指定文件路径String filePath = "E:/ydzxmgf/git分支管理规范.pdf";// 创建文件资源File file = new File(filePath);Path path = Paths.get(file.getAbsolutePath());Resource resource = new FileSystemResource(file);// 设置响应头HttpHeaders headers = new HttpHeaders();headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + file.getName());headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PDF_VALUE);// 返回响应实体return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.APPLICATION_PDF).body(resource);}

然后在浏览器上访问接口
在这里插入图片描述
就达到这个预览的效果了

http://www.lryc.cn/news/206449.html

相关文章:

  • RustDay06------Exercise[81-90]
  • 【Docker从入门到入土 6】Consul详解+Docker https安全认证(附证书申请方式)
  • PCIe架构的处理器系统介绍
  • 国产内存强势崛起,光威龙武挑战D5内存24×2新标杆
  • 矩阵的运算
  • Android 获取SIM卡号码权限申请
  • Linux CentOS 本地yum配置
  • 【c++速通】入门级攻略:什么是内联函数?函数重载又是什么?
  • ubuntu 安装串口工具和添加虚拟串口
  • 【数据结构】数组和字符串(四):特殊矩阵的压缩存储:稀疏矩阵——三元组表
  • 为什么POST请求经常发送两次?
  • 打破总分行数据协作壁垒,DataOps在头部股份制银行的实践|案例研究
  • 测试用例的设计方法(全):边界值分析方法
  • 酷开科技 | 酷开系统沉浸式大屏游戏更解压!
  • 读高性能MySQL(第4版)笔记20_Performance Schema和其他
  • spring cloud Eureka集群模式搭建(IDEA中运行)《二》
  • 大模型(LLM)在电商推荐系统的探索与实践
  • C语言之指针详解
  • 【Java笔记+踩坑】设计模式——原型模式
  • Flutter GetX使用详解
  • 【ARM Coresight 系列文章 3.3 - ARM Coresight SWD 协议详细介绍】
  • 作为开发者,可视化开发工具了解一下
  • Python:实现日历功能
  • 2.9.C++项目:网络版五子棋对战之业务处理模块的设计
  • springboot actuator 常用接口
  • 知识点滴 - Email地址不区分大小写
  • 同一个页面同一区域两个el-table在v-if下样式重叠问题
  • ExoPlayer架构详解与源码分析(6)——MediaPeriod
  • 【开题报告】基于Spring Boot的课程在线预约系统的设计与实现
  • React Hooks还有哪些常用的用法?