網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
解決No converter for XXX with preset Content-Type ‘a(chǎn)pplication/octet-stream;charset=UTF-8‘
作者:Pisces_224 更新時(shí)間: 2022-02-27 編程語(yǔ)言如題
我在后臺(tái)封裝下載excel的接口邏輯時(shí),文件流關(guān)閉后響應(yīng)返回success,但是后臺(tái)報(bào)錯(cuò)
原因就是我系統(tǒng)整體封裝的響應(yīng)邏輯,所以我在下載文件后也返回了成功和失敗的邏輯。但是并不支持。
try {
List<ExportExcelData> excelDataList = getData(wo_id); // 獲取表體數(shù)據(jù)
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//設(shè)置頭居中
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//內(nèi)容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//設(shè)置 水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 這里URLEncoder.encode可以防止中文亂碼 當(dāng)然和easyexcel沒有關(guān)系
String fileName = URLEncoder.encode(work_order+"報(bào)表導(dǎo)出測(cè)試", "UTF-8").replaceAll("\\+", "%20");
// response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.addHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
// 合并策略
Map<String, List<RowRangeDto>> srategyMap = ExcelUtil.addMergeStrategy(excelDataList);
// 這里需要設(shè)置不關(guān)閉流;
EasyExcel.write(response.getOutputStream(), ExportExcelData.class).autoCloseStream(Boolean.FALSE)
// 注冊(cè)合并策略
.registerWriteHandler(new BizMergeStrategy(srategyMap))
.registerWriteHandler(ExcelUtil.CellStyleStrategy())
.sheet("ckd出貨信息表")
.doWrite(excelDataList);
return "文件下載成功"; // 改為void,不反回
} catch (Exception e) {
// 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
logger.error(e.getMessage());
e.printStackTrace();
return "文件下載失敗"; // 改為void,不反回
}
一個(gè)解決辦法是并不返回提示,或者返回null即可避免此類報(bào)錯(cuò)。
但是很遺憾,如果我非要想返回 成功或失敗提示,目前還沒有辦法。
原文鏈接:https://blog.csdn.net/qq_36256590/article/details/122256170
相關(guān)推薦
- 2022-08-21 caffe的python接口繪制loss和accuracy曲線_python
- 2023-11-11 Jetson nano 安裝swapfile 解決Cannot allocate memory 問題
- 2022-04-25 Golang將Map的鍵值對(duì)調(diào)的實(shí)現(xiàn)示例_Golang
- 2022-09-12 ios開發(fā)UITableViewCell圖片加載優(yōu)化詳解_IOS
- 2023-04-24 numpy?產(chǎn)生隨機(jī)數(shù)的幾種方法_python
- 2021-12-24 基于PostgreSQL/openGauss?的分布式數(shù)據(jù)庫(kù)解決方案_PostgreSQL
- 2022-04-02 ?Python錯(cuò)誤與異常處理_python
- 2023-03-02 Go語(yǔ)言讀取文本文件的三種方式總結(jié)_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支