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

企业级-PDF文件下载

作者:fyupeng
技术专栏:☞ https://github.com/fyupeng
项目地址:☞ https://github.com/fyupeng/rpc-netty-framework


留给读者

一、介绍

文件下载在浏览器可以根据响应头设置纯下载和直接打开两种方式。

二、代码


@RequestMapping("/downloadPdf")@ResponseBodypublic void downloadSignPdf(HttpServletRequest request, HttpServletResponse response) {OutputStream out = null;try {byte[] bytes = docService.downLoadPdf(gid, type, isHs);if (bytes == null) {// 构造错误信息的 JSON 格式字符串String errorMsg = "{\"msg\": \"文件不存在\", \"gid\": \""+gid+"\", \"type\": \""+type+"\", \"isHs\": \""+isHs+"\" }";// 设置响应的 Content-Type 和字符编码response.setContentType("application/json;charset=UTF-8");// 将错误信息写入响应输出流PrintWriter pw = response.getWriter();pw.print(errorMsg);pw.flush();return;}response.reset(); // 非常重要// 纯下载方式//response.setContentType("text/html; charset=UTF-8");//response.setHeader("Content-Disposition", "attachment;fileName=" + gid + ".pdf");// 直接打开response.setContentType("application/pdf"); // 设置返回的文件类型response.addHeader("Content-Length", String.valueOf(bytes.length));  //文件大小out = response.getOutputStream();out.write(bytes);out.flush();} catch (Exception e) {e.printStackTrace();logger.error("下载电子签名后的pdf失败,gid={}, type={}, isHs={}", gid, type, isHs);} finally {if (out != null) {try {out.close();} catch (Exception e) {e.printStackTrace();}}}}

三、总结

简洁、高效、易用!

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

相关文章:

  • 00 - React 基础
  • 基于WPF技术的换热站智能监控系统17--项目总结
  • CI /CD学习
  • 基于matlab的高斯滤波与图像去噪
  • 解决 uniapp h5 页面在私有企微iOS平台 间歇性调用uni api不成功问题(uni.previewImage为例)。
  • Jenkins nginx自动化构建前端vue项目
  • Devicetree - 删除某个节点或属性
  • Xcode will continue when the operation completes
  • Python爬虫-贝壳新房
  • Canvas绘制图片和区域
  • Day10—Spark SQL基础
  • 开源技术:在线教育系统源码及教育培训APP开发指南
  • [C++][设计模式][观察者模式]详细讲解
  • Adobe Acrobat 编辑器软件下载安装,Acrobat 轻松编辑和管理各种PDF文件
  • eVTOL飞机:技术挑战、应用机遇和运动的作用
  • 【python】flask中如何向https服务器传输信息
  • 计算机网络 —— 应用层(FTP)
  • zookeeper + kafka消息队列
  • Python高级编程:深度学习基础
  • 如何从magento1迁移到magento2
  • 【Nginx】Nginx安装及简单使用
  • 【Linux系列】find命令使用与用法详解
  • Apple - DNS Service Discovery Programming Guide
  • 如何高效地为pip换源:详细操作指南
  • 免费ddns工具,快解析DNS解析使用教程
  • 【Vite】控制打包结构
  • Debian Linux安装minikubekubectl
  • Discuz动漫二次元风格网站模板
  • RIP、OSPF、IS-IS学习
  • 移植案例与原理 - build lite源码分析 之 hb命令__main__.py