admin管理员组

文章数量:1532047

 其他解决方案:(1054条消息) java 如何实现导出文件_java怎么导出文件_我是大头鸟的博客-CSDN博客https://blog.csdn/datouniao1/article/details/94593886


    @PostMapping(value = "/exportDaily")
    public void exportDailys(@RequestBody EquDailyDO equDailyDO, HttpServletResponse response) throws ParseException {
        try {
            ClassPathResource classPathResource = new ClassPathResource("设备报表.docx"); // 模板文件
            String path = classPathResource.getURL().getPath().toLowerCase();
            String templateurl = URLDecoder.decode(path, "UTF-8");

            InputStream is = new FileInputStream(new File(templateurl));
            XWPFDocument doc = new XWPFDocument(is);

            // 替换word模板数据
            //PoiUtil.replaceAll(doc,map);
            response.reset();
            String filename = "设备报表_"+equDailyDO.getType()+"_";
            String codedFileName = new String(filename.getBytes("gbk"), "iso-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName +".docx");
            // 响应类型,编码
            response.setContentType("application/octet-stream;charset=UTF-8");
            // 形成输出流
            OutputStream osOut = response.getOutputStream();
            doc.write(osOut);
            // 刷新此输出流并强制将所有缓冲的输出字节被写出
            osOut.flush();
            // 关闭流
            osOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

主要就是 下面的代码

String filename = "设备报表_"+equDailyDO.getType()+"_";
            String codedFileName = new String(filename.getBytes("gbk"), "iso-8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + codedFileName +".docx");
            // 响应类型,编码
            response.setContentType("application/octet-stream;charset=UTF-8");
            // 形成输出流
            OutputStream osOut = response.getOutputStream();
            doc.write(osOut);
            // 刷新此输出流并强制将所有缓冲的输出字节被写出
            osOut.flush();
            // 关闭流
            osOut.close();

调用后,重启项目,在postman 中请求接口,点击Send and Download 按钮,即可使用postman 下载文件

本文标签: 弹出浏览器功能文件Java