日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(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

欄目分類
最近更新