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

后端实现预览pdf,mp4,图片

PDF预览

  /*** pdf预览* @param response*/@RequestMapping(value = "/preview")public void showPdf(HttpServletResponse response) {try {//String filePath = this.getClass().getClassLoader().getResource("../../static/pdf/readme.pdf").getPath();String filePath = "E:\\歌\\2022_420300_1716900370656\\监督\\2_关于加强取的通知.pdf";File file = new File(filePath);FileInputStream fileInputStream = new FileInputStream(file);response.setHeader("Content-Type", "application/pdf");OutputStream outputStream = response.getOutputStream();IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);} catch(Exception e) {e.printStackTrace();}}

参考文章
在这里插入图片描述

视频预览

@GetMapping("/download")
public void download(HttpServletResponse response) throws IOException {File file = new File("/Users/zxk/Movies/1.2G.mp4");response.setContentType("video/mp4;charset=utf8");
//设置下载文件名response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
//中文乱码处理
//response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8") );
//网页直接播放
response.setHeader("Content-Disposition", "inline");
//下载进度response.setContentLengthLong(file.length());
try (InputStream inputStream = new FileInputStream(file);OutputStream outputStream = response.getOutputStream()) {IOUtils.copy(inputStream, outputStream);}}

参考文章

图片预览

    @ApiOperation("访问文件")@GetMapping("/download/{name}")public void getImage(HttpServletResponse response, @PathVariable("name") String name) throws IOException {//动态获取图片存放位置//        String path = getUploadPath();//获取当前系统路径String path = upload;String imagePath = path + File.separator + name;if (!new File(imagePath).exists()) {return;}if (name.endsWith("jpg") || name.endsWith("png") || name.endsWith("gif") || name.endsWith("jpeg")) {//预览时不需设置Content-Dispositionresponse.setContentType("image/jpeg;charset=utf-8");//图片}else {//下载response.setContentType("application/octet-stream");//文件response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(name, "UTF-8"));}ServletOutputStream outputStream = response.getOutputStream();outputStream.write(Files.readAllBytes(Paths.get(path).resolve(name)));outputStream.flush();outputStream.close();}

参考文章

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

相关文章:

  • 【C++】数据类型、函数、头文件、断点调试、输入输出、条件与分支、VS项目设置
  • Spring框架的原理及应用详解(六)
  • C++ | Leetcode C++题解之第151题反转字符串中的单词
  • Leetcode 415. 字符串相加-大数相加
  • IDEA集成Docker实现快捷部署
  • 五十四、openlayers官网示例LineString Arrows解析——在地图上绘制箭头
  • 内核学习——3、自旋锁的作用及其实现
  • 恒昌公益第五所“云杉校园”于湖南怀化正式揭牌
  • 番外篇 | YOLOv8算法解析和实战应用:车辆检测 + 车辆追踪 + 行驶速度计算
  • 【React】useState 的原理
  • 从二元一次方程组到二阶行列式再到克拉默法则
  • 示例:WPF中绑定枚举到ComboBox想显示成中文或自定义名称如何实现
  • 嵌入式系统软件架构设计方法
  • 【面试题】风险评估和应急响应的工作流程
  • Vue70-路由的几个注意点
  • Aidlux 1.4 部署Nextcloud 2024.6实录 没成功
  • 网络与协议安全复习 - 电子邮件安全
  • Python里的序列化是什么?
  • 自动抓取服务器功耗
  • 服务器接收苹果订阅通知
  • 2024年旅游与经济发展国际会议(ICTED 2024)
  • 【NLP练习】Transformer实战-单词预测
  • 使用Lua脚本保证原子性的Redis分布式锁实现
  • 什么是nginx到底怎么配置,什么是网关到底怎么配置?
  • 轻量级服务器内存不够编译的情况解决方案(以安装Ta-Lib库为例)
  • 学校校园考场电子钟,同步授时,助力考场公平公正-讯鹏科技
  • MySQL存储管理(一):删数据
  • 深度剖析现阶段的多模态大模型做不了医疗
  • Zabbix 监控 Kubernetes 集群
  • 网上预约就医取号系统