后台接口返回void有设置response相关内容,前端通过open打开接口下载excel文件
1、引入依赖,用来生成excel
(图片来源网络,侵删)
com.alibaba
easyexcel
2.1.2
2、接口类代码如下:
/**
* 企业列表--导出
*/
@ApiOperation("企业列表--导出")
@GetMapping(value = "/downloadTenantList")
public void downloadTenantList(HttpServletRequest request,
HttpServletResponse response) throws IOException {
log.info("downloadTenantList begin.");
String tenantIds = request.getParameter("tenantIds");
Map 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 exportTenantListByIds(String ids) {
String[] idStrs = ids.split(",");
List list = tenantMapper.listTenantsByIds(Arrays.asList(idStrs));
List 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 map = new HashMap();
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; /** ** 企业列表 ** * @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博客
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
