我们在开发的时候会遇到各种导出文件的情况,有的需要导出excel
JAVA导出EXCEL通用型
JAVA导出EXCEL定制型
但是有的需要导出txt文件。txt文件如何操作呢。
首先有导出txt方法,如下,其中text就是需要导出的内容
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/* 导出txt文件 * @author * @param response * @param text 导出的字符串 * @return */ public void exportTxt(HttpServletResponse response, String text) { response.setCharacterEncoding("utf-8"); //设置响应的内容类型 response.setContentType("text/plain"); //设置文件的名称和格式 response.addHeader("Content-Disposition", "attachment;filename=" + genAttachmentFileName(DateUtil.getNowShortStringDate(), "JSON_FOR_UCC_")//设置名称格式,没有这个中文名称无法显示 + ".txt"); BufferedOutputStream buff = null; ServletOutputStream outStr = null; try { outStr = response.getOutputStream(); buff = new BufferedOutputStream(outStr); buff.write(text.getBytes("UTF-8")); buff.flush(); buff.close(); } catch (Exception e) { //LOGGER.error("导出文件文件出错:{}",e); } finally { try { buff.close(); outStr.close(); } catch (Exception e) { //LOGGER.error("关闭流对象出错 e:{}",e); } } }
最后
以上就是大气薯片最近收集整理的关于JAVA导出txt文件JAVA导出EXCEL通用型JAVA导出EXCEL定制型的全部内容,更多相关JAVA导出txt文件JAVA导出EXCEL通用型JAVA导出EXCEL定制型内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复