文章目录
- 场景
- 解决方式
- 全部代码
场景
之前使用springboot下载文件一直正常,今天新对接一个接口出现文件破损,无法下载。
之前的代码:
@Override
public ResponseEntity<ByteArrayResource> resultExcel(ExcelResultDTO excelResultDTO) {
log.info("--------------- enter resultExcel method : args = {}------------------", JsonUtils.toJsonString(excelResultDTO));
EcTaskDO ecTaskDO = ecTaskMapper.selectById(excelResultDTO.getTaskId());
// 请求第三方接口
ForestResponse<byte[]> response = ecAiApi.result_excel(ecTaskDO.getMissionId(), excelResultDTO.getIndex());
if (response.isSuccess()) {
byte[] content = response.getResult();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "downloaded_file");
return ResponseEntity.ok()
.headers(headers)
.body(new ByteArrayResource(content));
} else {
// 处理请求失败的情况
return ResponseEntity.internalServerError().build();
}
}
现在有个新接口对接,仿照写了个
@Override
public ResponseEntity<ByteArrayResource> getBatchExcelModel(BatchExcelDTO batchExcelDTO) {
Long userId = getLoginUserId();
// 组装数据
CheckValidationPojo checkValidationPojo = new CheckValidationPojo();
checkValidationPojo.setFileId(batchExcelDTO.getAbacusId() + "");
checkValidationPojo.setTaskName("");
checkValidationPojo.setUserId(userId);
batchGetCheckValiationPojo(batchExcelDTO, checkValidationPojo);
// 赋值 (数据信息 和 按钮值 和输出信息信息)
ExcelModelReq excelModelReq = new ExcelModelReq();
excelModelReq.setJsonStr(new Gson().toJson(checkValidationPojo));
ForestResponse<byte[]> response = ecAiApi.getBatchList(excelModelReq);
if (response.isSuccess()) {
byte[] content = response.getResult();
String contentDisposition = response.getHeaders().get("Content-Disposition");
log.info(contentDisposition);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
if (contentDisposition!= null && contentDisposition.contains("filename=")) {
String fileName = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
log.info(fileName.replace("\"",""));
headers.setContentDispositionFormData("attachment", fileName.replace("\"",""));
} else {
// 如果响应头中没有文件名,可以设置一个默认文件名
headers.setContentDispositionFormData("attachment", "defaultFileName.xlsx");
}
return ResponseEntity.ok()
.headers(headers)
.body(new ByteArrayResource(content));
} else {
// 处理请求失败的情况
return ResponseEntity.internalServerError().build();
}
}
解决方式
添加这行代码就可以了。
全部代码
@Override
public ResponseEntity<ByteArrayResource> getBatchExcelModel(BatchExcelDTO batchExcelDTO) {
Long userId = getLoginUserId();
// 组装数据
CheckValidationPojo checkValidationPojo = new CheckValidationPojo();
checkValidationPojo.setFileId(batchExcelDTO.getAbacusId() + "");
checkValidationPojo.setTaskName("");
checkValidationPojo.setUserId(userId);
batchGetCheckValiationPojo(batchExcelDTO, checkValidationPojo);
// 赋值 (数据信息 和 按钮值 和输出信息信息)
ExcelModelReq excelModelReq = new ExcelModelReq();
excelModelReq.setJsonStr(new Gson().toJson(checkValidationPojo));
ForestResponse<byte[]> response = ecAiApi.getBatchList(excelModelReq);
if (response.isSuccess()) {
byte[] content = response.getResult();
String contentDisposition = response.getHeaders().get("Content-Disposition");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentType(new MediaType("application", "force-download"));
// headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=my_file.csv");
if (contentDisposition!= null && contentDisposition.contains("filename=")) {
String fileName = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
log.info(fileName.replace("\"",""));
headers.setContentDispositionFormData("attachment", fileName.replace("\"",""));
} else {
// 如果响应头中没有文件名,可以设置一个默认文件名
headers.setContentDispositionFormData("attachment", "defaultFileName.xlsx");
}
return ResponseEntity.ok()
.headers(headers)
.body(new ByteArrayResource(content));
} else {
// 处理请求失败的情况
return ResponseEntity.internalServerError().build();
}
}
大功告成!!!