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

后台接口返回void有设置response相关内容,前端通过open打开接口下载excel文件

1、引入依赖,用来生成excel

<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.1.2</version></dependency>

2、接口类代码如下:

/*** 企业列表--导出*/@ApiOperation("企业列表--导出")@GetMapping(value = "/downloadTenantList")public void downloadTenantList(HttpServletRequest request,HttpServletResponse response) throws IOException {log.info("downloadTenantList begin.");String tenantIds = request.getParameter("tenantIds");Map<String,String> map = tenantService.exportTenantListByIds(tenantIds);String filePath = map.get("filePath");String fileName = map.get("fileName");InputStream is = new FileInputStream(new File(filePath));// 设置response参数,可以打开下载页面response.reset();response.setContentType("application/octet-stream;charset=utf-8");response.setHeader("Access-Control-Expose-Headers", "content-disposition");response.setHeader("Content-Disposition", "attachment;filename="+fileName);ServletOutputStream out = response.getOutputStream();BufferedInputStream bis = null;BufferedOutputStream bos = null;try {bis = new BufferedInputStream(is);bos = new BufferedOutputStream(out);byte[] buff = new byte[2048];int bytesRead;while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {bos.write(buff, 0, bytesRead);}log.info("downloadTenantList end.");} catch (final IOException e) {log.error("downloadTenantList errors, reason:{}", e.getMessage());} finally {if (bis != null){bis.close();}if (is != null){is.close();}if (bos != null){bos.close();}if (out != null){out.close();}}// 用后删除临时用途的文件File fileTempZip = new File(filePath);if(fileTempZip.exists()){fileTempZip.delete();}}

3、服务层代码如下:

public Map<String,String> exportTenantListByIds(String ids) {String[] idStrs = ids.split(",");List<TenantMaintainDTO> list = tenantMapper.listTenantsByIds(Arrays.asList(idStrs));List<TenantInfoExcelDTO> listExport = new ArrayList<>();if(list!=null && !list.isEmpty()) {// 处理企业相关信息,管理员、人数、企业行业、企业类型、企业规模handleTenantRelatedInfo(list);listExport = handleTenantListForExport(list);}String fileName = UuidUtil.getUuid() + ".xlsx";String filePath = filePathTemp + fileName;EasyExcel.write(filePath, TenantInfoExcelDTO.class).sheet("企业列表").doWrite(listExport);Map<String,String> map = new HashMap<String,String>();map.put("fileName",fileName);map.put("filePath",filePath);return map;}

4、TenantInfoExcelDTO类代码如下:

package cn.iiot.myth.platform.dto.tenant;import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;/*** <pre>* 企业列表* </pre>** @author heming@iiot.cn* @date 2024-05-06*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class TenantInfoExcelDTO {@ExcelProperty(value = "编号")private String tenantId;@ExcelProperty(value = "企业/组织机构名称")private String companyName;@ExcelProperty(value = "所属行业")private String companyIndustry;@ExcelProperty(value = "规模")private String companyScale;@ExcelProperty(value = "联系人")private String contacts;@ExcelProperty(value = "管理员")private String managerNames;@ExcelProperty(value = "平台成员数量")private int userCount;@ExcelProperty(value = "联系号码")private String mobile;@ExcelProperty(value = "联系邮箱")private String email;@ExcelProperty(value = "认证状态")private String auditStatus;@ExcelProperty(value = "入驻时间")private Date createTime;@ExcelProperty(value = "认证时间")private Date joinDate;
}

5、前端使用window.open("接口路径");下载该excel文件。

功能正常使用,但是这种方式,前端加不了认证头信息,该后台接口不安全。

解决方案参考:后台接口返回void但是response有设置合适的相关信息,前端调用接口解析Blob数据下载excel文件-CSDN博客

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

相关文章:

  • scp问题:Permission denied, please try again.
  • new CCDIKSolver( OOI.kira, iks ); // 创建逆运动学求解器
  • 【Go】Swagger v2 转 OpenApi v3 CLI - swag2op
  • python Z-score标准化
  • 人工智能的数学基础(高数)
  • React(四)memo、useCallback、useMemo Hook
  • 前端介绍及工具环境搭建
  • uniapp高校二手书交易商城回收系统 微信小程序python+java+node.js+php
  • Vue3 图片或视频下载跨域或文件损坏的解决方法
  • vue2和3区别
  • 倍福TwinCAT3 PLC编程软件下载安装
  • Linux一键式管理jar程序执行周期【完整脚本复制可用】
  • 设计模式之六大设计原则
  • 【iOS】UI学习(一)
  • 如何使用Vue和Markdown实现博客功能
  • 1初识C#
  • 查询指定会话免打扰
  • Linux-命令
  • STM32读写内部FLASH读取芯片id
  • 前端npm打包及报错解决
  • vbs执行报错vbs没有文件拓展,双击无法打开
  • 超详细的前后端实战项目(Spring系列加上vue3)前端篇(二)(一步步实现+源码)
  • 【国产中颖】SH79F9202U单片机驱动LCD段码液晶学习笔记
  • 人工智能初识
  • 【算法刷题day60】Leetcode:84. 柱状图中最大的矩形
  • ThingsBoard物联网网关在智慧城市数据采集中的应用
  • Java中的打印流PrintStream 和 PrintWriter
  • 【MATLAB源码-第217期】基于matlab的16QAM系统相位偏移估计HOS算法仿真,对比补偿前后的星座图误码率。
  • C# CryptoStream流的详解与示例
  • Kubernetes 之 ReplicaSet