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 导出功能,弹出浏览器文件下载框 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726113960a1055905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论