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

java下载文件流,不生成中间文件。

java下载文件流,不生成中间文件。

    • 代码设计:
    • 代码实现

代码设计:

从前端获取的数据经过后端加工后,生成文件流,并返回前端,(不生成中间文件,注意内存,记得关闭流)

代码实现

    @ApiOperation(value = "下载文件", notes = "")@PostMapping("/getDownLoadScriptFile")public void getDownLoadScriptFile(@RequestBody ParamsObject vo, HttpServletRequest request, HttpServletResponse response) throws Exception {SysUserEntityVo uc = (SysUserEntityVo) request.getAttribute("UC");gClientScriptService.getDownLoadScriptFile(vo, uc,response);}
  @Overridepublic void getDownLoadScriptFile(ParamsObject vo, SysUserEntityVo uc, HttpServletResponse response) throws Exception {String fileContent = vo.getFileContent();String fileName = vo.getFileName();String encodeFileName = URLEncoder.encode(fileName);ServletOutputStream out = response.getOutputStream();try {//设置允许跨域的keyresponse.setHeader("Access-Control-Expose-Headers", "Content-Disposition");//文件名有“,”等特殊字符发送到前端会报错,用""括起来解决response.addHeader("Content-Disposition", "attachment;filename=\"" + encodeFileName + "\"");//设置文件大小response.addHeader("Content-Length", "" + fileContent.getBytes().length);//设置文件名,避免问题,这个也用""括起来response.setHeader("filename,", "filename=\"" + encodeFileName + "\"");//设置文件类型response.setContentType("application/octet-stream");out.write(fileContent.getBytes(StandardCharsets.UTF_8));out.flush();} catch (Exception e) {throw e;} finally {try {out.close();} catch (Exception e) {throw e;}try {out.close();} catch (Exception e) {throw e;}}}
http://www.lryc.cn/news/513488.html

相关文章:

  • 计算机专业考研 408 学科学习方法
  • mapper文件的解释
  • 常见协议的高危软件漏洞信息
  • Mediatek Android13 ROM定制
  • RedisInsight:企业级 Redis 管理与分析工具
  • c# 快捷键模块
  • 【笔记】增值税计算笔记
  • 请解释 JavaScript 中的闭包,以及它的优缺点和常见使用场景?
  • SpringBoot 集成 Caffeine 实现本地缓存
  • druid连接池参数配置
  • 【OceanBase】通过 OceanBase 的向量检索技术构建图搜图应用
  • Linux 安装运行gatk的教程
  • 什么是unit l2 norm
  • 手写顺序流程图组件
  • 适配器模式概述
  • Logo设计免费生成器:轻松设计个性化标志
  • 智能停车场车牌识别计费系统
  • 谷歌开通第三方平台OAuth登录及Java对接步骤
  • 人体:精妙绝伦的生命之躯
  • python的urllib模块和http模块
  • Java [后端] 开发日常记录(1)
  • jetbrain 安装 copilot
  • 万里数据库GreatSQL监控解析
  • OpenCV-Python实战(9)——滤波降噪
  • Pytorch | 利用DTA针对CIFAR10上的ResNet分类器进行对抗攻击
  • Linux性能测试简介
  • Kile5支持包的安装
  • 【Ubuntu 系统 之 开启远程桌面SSH登录】
  • MySQL 索引分类及区别与特点
  • 对中文乱码的理解,遇到乱码该怎么办。