下载文件返回文件流

该代码片段展示了一个用于导出抽检计划的任务管理API。通过@PostMapping方法处理/exportSpotCheck请求,接收RequestBody中的Map数据,调用spotcheckPlanService导出计划并检查返回状态。如果成功,它将读取文件内容,创建一个ByteArrayResource,并设置HTTP响应头以供下载。如果出现异常,则返回null。
摘要由CSDN通过智能技术生成

@Log(title = “任务管理”, businessType = BusinessType.EXPORT)
@ApiOperation(value = “抽检计划-导出”)
@PostMapping(“/exportSpotCheck”)
public ResponseEntity exportSpotCheck(@RequestBody Map map) {
try {
AjaxResult result = spotcheckPlanService.exportSpotcheckPlan((List) map.get(“ids”), 1);
if (String.valueOf(result.get(“code”)).equals(“200”)) {
String path = String.valueOf(result.get(“msg”));
String fileName = path.substring(path.lastIndexOf(File.separatorChar) + 1);
File file = new File(path);
ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
return ResponseEntity.ok().header(“Content-Disposition”, “attachment;filename=” + URLEncoder.encode(fileName, “UTF-8”))
.contentLength(file.length()).contentType(MediaType.parseMediaType(“application/octet-stream”))
.body(resource);
}
} catch (Exception e) {
return null;
}
return null;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值