news 2026/5/11 18:33:09

springboot controller返回的是HttpServletResponse成功返回excel文件流,失败就返回失败参数

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
springboot controller返回的是HttpServletResponse成功返回excel文件流,失败就返回失败参数

下面是一个完整的 Spring Boot Controller 示例,根据业务逻辑判断返回 Excel 文件流或失败参数:

方案一:使用 HttpServletResponse 直接输出

import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/api/excel") public class ExcelExportController { @PostMapping("/export") public void exportExcel(@RequestBody ExportRequest request, HttpServletResponse response) { try { // 1. 业务逻辑判断是否成功 BusinessResult result = processBusiness(request); if (!result.isSuccess()) { // 2. 失败时返回 JSON 格式的错误信息 response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); Map<String, Object> errorResponse = new HashMap<>(); errorResponse.put("code", 500); errorResponse.put("message", result.getErrorMessage()); errorResponse.put("data", result.getErrorData()); // 设置 HTTP 状态码 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // 写入错误信息 response.getWriter().write( new com.fasterxml.jackson.databind.ObjectMapper() .writeValueAsString(errorResponse) ); return; } // 3. 成功时返回 Excel 文件 byte[] excelData = generateExcel(result.getData()); // 设置响应头 String fileName = URLEncoder.encode("报表.xlsx", StandardCharsets.UTF_8.name()); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); // 写入 Excel 文件流 try (OutputStream os = response.getOutputStream()) { os.write(excelData); os.flush(); } } catch (Exception e) { // 异常处理 handleException(response, e); } } // 业务处理方法 private BusinessResult processBusiness(ExportRequest request) { // 模拟业务逻辑 if (request.getData() == null || request.getData().isEmpty()) { return BusinessResult.failure("导出数据不能为空", null); } // 其他业务逻辑... return BusinessResult.success("处理成功", request.getData()); } // 生成 Excel private byte[] generateExcel(List<Data> data) { // 使用 Apache POI 或其他库生成 Excel // 这里简化为示例 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); XSSFWorkbook workbook = new XSSFWorkbook()) { XSSFSheet sheet = workbook.createSheet("Sheet1"); // 填充数据... workbook.write(baos); return baos.toByteArray(); } catch (Exception e) { throw new RuntimeException("生成Excel失败", e); } } // 异常处理方法 private void handleException(HttpServletResponse response, Exception e) { try { response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); Map<String, Object> error = new HashMap<>(); error.put("code", 500); error.put("message", "服务器内部错误"); error.put("detail", e.getMessage()); response.getWriter().write( new com.fasterxml.jackson.databind.ObjectMapper() .writeValueAsString(error) ); } catch (Exception ex) { // 处理异常时的异常 } } }

方案二:使用 ResponseEntity 统一返回

import org.springframework.http.*; import org.springframework.web.bind.annotation.*; import org.springframework.core.io.ByteArrayResource; @RestController @RequestMapping("/api/excel") public class ExcelExportController2 { @PostMapping("/export2") public ResponseEntity<?> exportExcel(@RequestBody ExportRequest request) { try { BusinessResult result = processBusiness(request); if (!result.isSuccess()) { // 返回失败的 JSON 响应 Map<String, Object> errorResponse = new HashMap<>(); errorResponse.put("code", 400); errorResponse.put("message", result.getErrorMessage()); errorResponse.put("timestamp", System.currentTimeMillis()); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON) .body(errorResponse); } // 生成 Excel byte[] excelData = generateExcel(result.getData()); // 设置响应头 String fileName = "export_" + System.currentTimeMillis() + ".xlsx"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDisposition( ContentDisposition.attachment() .filename(fileName, StandardCharsets.UTF_8) .build() ); headers.setCacheControl("no-cache"); ByteArrayResource resource = new ByteArrayResource(excelData); return ResponseEntity.ok() .headers(headers) .contentLength(excelData.length) .body(resource); } catch (Exception e) { Map<String, Object> error = new HashMap<>(); error.put("code", 500); error.put("message", "导出失败"); error.put("detail", e.getMessage()); return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) .contentType(MediaType.APPLICATION_JSON) .body(error); } } }

辅助类定义

// 请求参数类 @Data public class ExportRequest { private String startDate; private String endDate; private List<Data> data; @Data public static class Data { private String name; private Integer value; } } // 业务结果类 @Data @AllArgsConstructor public class BusinessResult { private boolean success; private String errorMessage; private List<Data> data; public static BusinessResult success(String message, List<Data> data) { return new BusinessResult(true, message, data); } public static BusinessResult failure(String errorMessage, List<Data> data) { return new BusinessResult(false, errorMessage, data); } }

使用建议

  1. 方案一​ 更适合需要精细控制 HTTP 响应的场景

  2. 方案二​ 更符合 Spring 的编程风格,代码更简洁

  3. 文件命名:使用filename*=UTF-8''编码文件名,支持中文

  4. 响应头设置

    • 成功时设置application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

    • 失败时设置application/json

  5. 异常处理:确保异常时也能返回规范的错误信息

客户端调用示例

// 前端调用 fetch('/api/excel/export', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ startDate: '2024-01-01', endDate: '2024-12-31', data: [...] }) }) .then(async response => { const contentType = response.headers.get('content-type'); if (contentType.includes('application/json')) { // 错误响应 const error = await response.json(); console.error('导出失败:', error.message); } else if (contentType.includes('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) { // Excel 文件 const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = '报表.xlsx'; a.click(); } });

这样设计可以确保:

  1. 成功时正确下载 Excel 文件

  2. 失败时返回结构化的错误信息

  3. 客户端可以根据 Content-Type 区分处理结果

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/9 18:27:50

vLLM如何完美支持Open-AutoGLM?深入源码的7个发现

第一章&#xff1a;vLLM如何完美支持Open-AutoGLM&#xff1f;深入源码的7个发现在探索vLLM对Open-AutoGLM的支持机制时&#xff0c;通过对核心模块的源码分析&#xff0c;发现了七个关键设计点&#xff0c;这些发现揭示了其高效推理与动态调度背后的工程智慧。异步请求处理管道…

作者头像 李华
网站建设 2026/5/10 1:12:37

好写作AI:期刊审稿人视角——AI辅助写作的常见优势与注意事项?

作为期刊审稿人&#xff0c;当一篇语言流畅、格式精准、逻辑清晰的稿件呈现在眼前时&#xff0c;一个现实问题浮现&#xff1a;这份专业呈现的背后&#xff0c;AI工具究竟扮演了什么角色&#xff1f;其价值与风险何在&#xff1f; 随着AI写作辅助工具的普及&#xff0c;期刊审稿…

作者头像 李华
网站建设 2026/5/10 11:24:45

TensorFlow在游戏NPC行为模拟中的应用

TensorFlow在游戏NPC行为模拟中的应用 如今&#xff0c;一款游戏是否“聪明”&#xff0c;往往不再取决于画面多精美或剧情多动人&#xff0c;而在于它的非玩家角色&#xff08;NPC&#xff09;能否像真人一样思考、反应甚至“成长”。你有没有遇到过这样的场景&#xff1a;无论…

作者头像 李华
网站建设 2026/5/10 3:54:15

PyTorch中torch.flatten()函数的用法

1 问题在编写神经网络数据从输入层前向传播到输出层的函数时&#xff0c;使用了torch.flatten()函数&#xff0c;但是torch.flatten()函数的各个参数的含义是什么以及怎么用呢&#xff1f;2 方法flatten翻译成中文是扁平化&#xff0c;使……平坦&#xff0c;即把多维的输入拉伸…

作者头像 李华
网站建设 2026/5/9 20:34:12

AI Agent爆火!三份重磅报告揭秘大模型开发者必备技能,附下载

1月15日&#xff0c;OpenAI推出了一项名为Tasks的测试版新功能&#xff0c;主要面向Plus、Pro和Teams用户开放。Tasks旨在为ChatGPT提供自动化的任务处理能力&#xff0c;用户可以设置一次性提醒或重复性任务&#xff0c;让ChatGPT帮助完成各种任务。 有人认为&#xff0c;Tas…

作者头像 李华
网站建设 2026/5/9 16:53:18

如何用TensorFlow训练舞蹈动作生成模型?

如何用TensorFlow训练舞蹈动作生成模型&#xff1f; 在虚拟偶像的舞台上&#xff0c;一段流畅自然的舞蹈不再完全依赖真人动捕演员——AI 正悄然接管动作设计的核心环节。从 TikTok 上爆火的数字人主播&#xff0c;到游戏引擎中实时驱动的角色动画&#xff0c;自动生成的人体运…

作者头像 李华