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

PDF预览

java 复制代码
  /**
     * 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();
        }
    }

参考文章

视频预览

java 复制代码
@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);
        }
    }

参考文章

图片预览

java 复制代码
    @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-Disposition
            response.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();
    }

参考文章

相关推荐
CodeCaptain3 分钟前
【一】dify的知识库上传过相关的文件作为待引用的文档,这样已经与[原始语料 → 按“一文档一份 PDF”存 ObjectStore]同样的概念吗
人工智能·pdf·dify
清风~徐~来5 分钟前
【视频点播系统】Etcd-SDK 介绍及使用
数据库·etcd
计算机毕设VX:Fegn08956 分钟前
计算机毕业设计|基于springboot + vue球鞋购物系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
仍然.9 分钟前
MYSQL--- 表的设计
数据库·mysql
刘一说13 分钟前
Java中基于属性的访问控制(ABAC):实现动态、上下文感知的权限管理
java·网络·python
一晌小贪欢14 分钟前
Python 操作 Excel 高阶技巧:用 openpyxl 玩转循环与 Decimal 精度控制
开发语言·python·excel·openpyxl·python办公·python读取excel
铁蛋AI编程实战17 分钟前
Falcon-H1-Tiny 微型 LLM 部署指南:100M 参数也能做复杂推理,树莓派 / 手机都能跑
java·人工智能·python·智能手机
数据知道21 分钟前
PostgreSQL的连接方式有哪些?有哪些连接工具?
数据库·postgresql
柚子科技22 分钟前
毕业设计不用愁:一个免费的 SQL 转 ER 图在线工具,真香!
数据库·sql·毕业设计·课程设计·毕设
xuefuhe23 分钟前
postgresql获取真正的execution plan
数据库·postgresql