jeecgboot/JeecgBoot · error · JeecgBootException

下载word模版失败: {message}

Error message

下载word模版失败: {message}

What it means

AigcWordTemplateController.downloadTemplate() generates a .docx from a stored template and streams it to the HttpServletResponse. Any exception inside the try-with-resources (template null, POI generation failure, client-closed output stream) is wrapped as JeecgBootException with the cause message.

Source

Thrown at jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/src/main/java/org/jeecg/modules/airag/wordtpl/controller/AigcWordTemplateController.java:181

     */
    @GetMapping(value = "/download")
    public void downloadTemplate(@RequestParam(name = "id", required = true) String id, HttpServletResponse response) {
        AssertUtils.assertNotEmpty("请先选择模版", id);
        AigcWordTemplate template = eoaWordTemplateService.getById(id);
        try (ByteArrayOutputStream wordTemplateOut = new ByteArrayOutputStream();
             BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) {
            wordTplUtils.generateWordTemplate(template, wordTemplateOut);
            String fileName = template.getName();
            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".docx");
            response.addHeader("filename", encodedFileName + ".docx");
            byte[] bytes = wordTemplateOut.toByteArray();
            response.setHeader("Content-Length", String.valueOf(bytes.length));
            bos.write(bytes);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new JeecgBootException("下载word模版失败: " + e.getMessage(), e);
        }
    }


    /**
     * 解析word模版文件
     * @param file
     * @param id
     * @return
     * @author chenrui
     * @date 2025/7/9 14:38
     */
    @PostMapping(value = "/parse/file")
    public Result<?> parseWOrdFile(@RequestParam("file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            AigcWordTemplate eoaWordTemplate = wordTplUtils.parseWordFile(inputStream);
            log.info("解析的模版信息: {}", eoaWordTemplate);

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Read the cause stacktrace logged via log.error(e.getMessage(), e).
  2. Verify the template id exists: eoaWordTemplateService.getById(id) is non-null before generation.
  3. Reproduce generateWordTemplate locally with the template record.
  4. If the cause is a ClientAbortException/IOException on the stream, treat as a client disconnect, not a server fault.

Example fix

// before
catch (Exception e) {
    log.error(e.getMessage(), e);
    throw new JeecgBootException("下载word模版失败: " + e.getMessage(), e);
}
// after - null-check before streaming so the response stays clean
if (template == null) { response.sendError(404, "模版不存在"); return; }
Defensive patterns

Strategy: validation

Validate before calling

// guard before streaming
AigcWordTemplate template = eoaWordTemplateService.getById(id);
if (template == null) { response.sendError(404, "模版不存在"); return; }

Try / catch

try {
    ...
} catch (org.springframework.web.context.request.async.AsyncRequestNotUsableException | java.io.IOException clientEx) {
    log.warn("客户端断开连接,下载中止", clientEx); // client disconnect, do not throw
} catch (Exception e) {
    throw new JeecgBootException("下载word模版失败: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: getById(id) returns null (template deleted); wordTplUtils.generateWordTemplate throws (see error 128); response.getOutputStream() fails because the client disconnected; the encoded filename produces a header issue.

Common situations: Invalid or deleted template id; template body references a broken image; browser cancels the download mid-stream; concurrent request corrupting the doc.

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/2b77e8d90a2b1a1f. Report an issue: GitHub.