@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;
}
下载文件返回文件流
最新推荐文章于 2023-07-12 13:03:38 发布
该代码片段展示了一个用于导出抽检计划的任务管理API。通过@PostMapping方法处理/exportSpotCheck请求,接收RequestBody中的Map数据,调用spotcheckPlanService导出计划并检查返回状态。如果成功,它将读取文件内容,创建一个ByteArrayResource,并设置HTTP响应头以供下载。如果出现异常,则返回null。
摘要由CSDN通过智能技术生成