| | |
| | | return R.status(attachService.updateById(attach)); |
| | | } |
| | | |
| | | // 流式附件下载接口,支持下载zip压缩包,不使用本地存储 |
| | | @ApiOperation(value = "流式附件下载接口", notes = "使用流方式返回数据,不使用本地存储") |
| | | @PostMapping("/downloadByByte") |
| | | public void downloadByByte( |
| | | @ApiParam(value = "附件下载参数") @RequestBody AttachmentDownloadParam param, |
| | | HttpServletResponse response) throws IOException { |
| | | |
| | | // 设置文件名 |
| | | String timestamp = DateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd_HHmmss"); |
| | | String fileName = "attachments_" + timestamp + ".zip"; |
| | | |
| | | // 设置响应头 |
| | | response.setContentType("application/zip"); |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())); |
| | | |
| | | // 获取响应输出流 |
| | | ServletOutputStream outputStream = null; |
| | | try { |
| | | outputStream = response.getOutputStream(); |
| | | |
| | | Boolean result = attachService.downloadByByte(param, outputStream); |
| | | if (!result) { |
| | | return; |
| | | } |
| | | } catch (Exception e) { |
| | | if (!response.isCommitted()) { |
| | | response.reset(); |
| | | response.setContentType("application/json;charset=UTF-8"); |
| | | ResponseResult errorResult = ResponseResult.error("下载文件失败: " + e.getMessage()); |
| | | |
| | | // 使用输出流写入错误信息,避免getWriter()冲突 |
| | | try { |
| | | byte[] errorBytes = JSON.toJSONString(errorResult).getBytes(StandardCharsets.UTF_8); |
| | | outputStream.write(errorBytes); |
| | | } catch (IOException ioException) { |
| | | log.error("Error writing error response: {}", ioException.getMessage()); |
| | | } |
| | | } else { |
| | | log.error("Cannot send error response: response already committed. Exception: {}", e.getMessage()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除 附件表及对应的附件信息 |