徐徐爱coding
  • 首页
  • 爱情买卖
  • 导航
  • 私语
  • 友情链接
  • 关于
    关于本站
    知识库
    弹钢琴
徐徐爱coding

徐徐爱coding

徐徐爱coding是一个个人博客站点,记录编程经历的点点滴滴,分享自己的所见与所得,坚持自己的初心,践行自己的梦想生活不是等着暴风雨过去,而是学会在风雨中跳舞!

Copyright © 2023 徐徐爱coding All Rights Reserved.
陕公网安备61019602000456陕ICP备2023007787号-2

网站已稳定运行

java下载文章为markdown文件 | 徐徐爱coding
java下载文章为markdown文件

java下载文章为markdown文件

徐徐
后端
#Java
0 热度0 评论0 点赞
发布于2024-11-21 14:48:41
🌺前言
java下载文章为markdown文件

java
    /**
     * 导出
     * @param response
     */

    @GetMapping("/export")
    @Operation(summary = "导出文章")
    @Parameter(in= ParameterIn.HEADER,name="token",description = "token")
    public void export(@RequestParam("id") Long id,@RequestParam("ext") String ext,HttpServletResponse response){
        try {
            Article article = articleService.getById(id);
            OutputStream outputStream = response.getOutputStream();
            if(ext.equals("md")) {
                // 设置响应头
                response.setContentType("text/markdown");
                response.setHeader("Content-Disposition", "attachment; filename=output.md");
                // 写入文件内容
                outputStream.write(article.getContent().getBytes());
            }else{
                ResponseResult result = ResponseResult.errorResult(400,"不支持的导出格式");
                WebUtils.renderString(response, JSON.toJSONString(result));
                return;
            }
            response.setCharacterEncoding("utf-8");
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            //如果出现异常也要响应json
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }
}
文章最后更新于 2024-11-21 14:48:41
作者:徐徐
版权声明:转载请注明文章出处
留言
暂无数据

~~空空如也