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

SpringBoot接口内部从sftp服务器获取文件流实现文件下载

SpringBoot接口内部从sftp服务器获取文件流实现文件下载

    @GetMapping("/voice/file/download")@ApiOperation("语音文件--按id下载录音文件")@RequestLog(businessType = BusinessType.SELECT)public void downloadFile(@RequestParam("recfilename") String recfilename, HttpServletResponse response) throws IOException {ChannelSftp channel = (ChannelSftp) SftpUtil.initialChannel(null, sftpCtiConfiguration.getAccountSftp(), sftpCtiConfiguration.getEntranceTicketSftp(), sftpCtiConfiguration.getServerIp(), sftpCtiConfiguration.getPortSftp(), "180000");if (channel == null) {log.error(this.getClass().getSimpleName() + "#callVoiceFileList, step0:初始化连接sftp服务器失败!");emailUtil.sendMail("获取语音文件失败,初始化连接sftp服务器失败!请联系相关人员进行人工处理,谢谢!", "Connect sftp server failed");throw new BusinessException("连接cti服务器失败,请排查配置信息!");}InputStream inputStream;try {inputStream = channel.get(recfilename);} catch (SftpException e) {log.error("获取文件失败", e);emailUtil.sendMail("获取语音文件失败,请联系相关人员进行人工处理,谢谢!", "Get file failed");throw new BusinessException("获取文件失败");}String filename = recfilename.substring(recfilename.lastIndexOf("/") + 1);response.setCharacterEncoding("utf-8");response.setContentType("application/octet-stream");// 对真实文件名进行百分号编码String percentEncodedFileName = URLEncoder.encode(filename, "utf-8").replaceAll("\\+", "%20");// 组装contentDisposition的值StringBuilder contentDispositionValue = new StringBuilder();contentDispositionValue.append("attachment; filename=").append(percentEncodedFileName).append(";").append("filename*=").append("utf-8''").append(percentEncodedFileName);response.setHeader("Content-disposition",contentDispositionValue.toString());response.setHeader("filename", java.net.URLEncoder.encode(filename, "UTF-8"));// 将文件流写到response中try (OutputStream outputStream = response.getOutputStream()) {IOUtils.copy(inputStream, outputStream);} catch (IOException e) {log.error("文件下载失败!", e);throw new BusinessException("文件下载失败!");}log.info(this.getClass().getSimpleName() + "#downloadFile(),文件下载完毕!");}


http://www.mrgr.cn/news/7933.html

相关文章:

  • 什么是持续集成(持续交付、部署)
  • 组合数dfs
  • Ruby宝石光芒:探索SEO优化的瑰宝工具与库
  • 【Python】家庭用电数据分析Prophet预测
  • 【C++ Primer Plus习题】4.10
  • (贪心) LeetCode 45. 跳跃游戏 II
  • PV、UV、IP:网站流量分析的关键指标
  • viscode 自定义片段,快速生成自己的开发模板
  • java 字符串判断非空工具类 不用依赖
  • vue+uniapp
  • 树莓派与STM32(RT1064)的串口通信实现
  • Android 12中读写SD卡,提示Operation not permitted问题处理
  • 职场人士必备!2024年流行思维导图软件
  • 二叉搜索树
  • 【C语言】常见文件操作
  • 后端Web之登录校验(下篇)
  • Jupyter Notebook 使用多个Kernel
  • 日常开发规范
  • C# 使用OpenCV 类VideoCapture 和 Mat的正确方法
  • 【五】阿伟开始学Kafka