java spring boot 导出/下载文本文件操作(包含写文本文件)
内容简介
本文主要内容为使用java把内容写入文本文件,并实现下载/导出的功能。
实现步骤
1. controller层
@ResponseBody
@RequestMapping(value = "/exportLand2ndClassIndex", method = RequestMethod.GET)
public ResponseEntity<byte[]> exportLand2ndClassIndex(){
return extractTifService.exportLand2ndClassIndex();
}
2. 服务实现层,请自行定义服务接口,这里不展示了
/**
* 导出
* @return CallbackBody
*/
@Override
public ResponseEntity<byte[]> exportLand2ndClassIndex(){
//查询表数据
List<TswatLuc2ndClassIndex> list = tswatLuc2ndClassIndexDao.queryAllClassIndex();
if (list == null || list.size() <= 0){
return null;
}
List txtContentList = new ArrayList();
txtContentList.add("\"value\",\"name\"");
for(TswatLuc2ndClassIndex classIndex : list){
String value = classIndex.getLevel2Code();
String name = classIndex.getSwat();
txtContentList.add(value + "," + name);
}
//导出的文件存储目录
String fileSavePath = GisPathConfigurationUtil.getSwatLuc2ndClassIndexTxtFileSavePath();
//保存文本文件
writeToTxt(txtContentList, fileSavePath);
//获取文本文件的ResponseEntity
try{
ResponseEntity<byte[]> fileByte = buildResponseEntity(new File(fileSavePath));
return fileByte;
}catch (Exception e){
e.printStackTrace();
return null;
}
} /**
* 将数据写入文本文件
* @param list
* @param path
*/
private void writeToTxt(List list,String path) { String dir = path.substring(0,path.lastIndexOf("\\"));
File parent = new File(dir);
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
FileOutputStream outSTr = null;
BufferedOutputStream Buff = null;
String enter = "\r\n";
StringBuffer write ;
try {
outSTr = new FileOutputStream(new File(path));
Buff = new BufferedOutputStream(outSTr);
for (int i = 0; i < list.size(); i++) {
write = new StringBuffer();
write.append(list.get(i));
write.append(enter);
Buff.write(write.toString().getBytes("UTF-8"));
}
Buff.flush();
Buff.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Buff.close();
outSTr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} //读取文件
private ResponseEntity<byte[]> buildResponseEntity(File file) throws IOException {
byte[] body = null;
//获取文件
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
//设置文件类型
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
//设置Http状态码
HttpStatus statusCode = HttpStatus.OK;
//返回数据
ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
return entity;
}
java spring boot 导出/下载文本文件操作(包含写文本文件)的更多相关文章
- Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (二)实现一个过滤器Filter
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Spring Boot 导出Excel表格
Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...
- Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...
- Java Spring Boot VS .NetCore (一)来一个简单的 Hello World
系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...
- Java Spring Boot VS .NetCore (三)Ioc容器处理
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (五)MyBatis vs EFCore
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Java Spring Boot VS .NetCore (七) 配置文件
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
随机推荐
- JavaScript有用的代码片段和trick
浮点数取整 const x = 123.4545; x >> 0; ~~x; x | 0; Math.floor(x); 注意:前三种方法只适用于32个位整数,对于负数的处理上和Math. ...
- 【转帖】AMD Zen之父、Intel副总Jim Keller到底有多牛?
AMD Zen之父.Intel副总Jim Keller到底有多牛? https://www.cnbeta.com/articles/tech/907295.htm 几乎玩过 所有的中国国产化CPU的祖 ...
- JVM之java并发 ——线程安全与锁优化
概述 人们很难想象现实中的对象在一项工作进行期间,会被不停地中断和切换,对象的属性(数据)可能会在中断期间被修改和变“脏”,而这些事情在计算机世界中则是很正常的事情.有时候,良好的设计原则不得不向现实 ...
- 深入Java中的位操作
「WTF系列」深入Java中的位操作 关于WTF系列 引 学完本章节你将学会位的基础概念与语法,并且还会一些骚操作!! 与.或.非.位移 原码.反码.补码 字节.位.超区间...... 开始本章节之前 ...
- Winform 快速开发框架,上位机开发,工控机程序开发,CS程序开发
1.当客户让你做个CS程序时,当你手上一穷二白,所有都要重复造轮,你是不是很烦. 2.但如果有一个通用的,快速开发框架,就可以把你从这些基础的工作解救出来,你专注做业务就好了. 3.本人其中一个项目的 ...
- 面试4 --- constructor必须与class同名,但方法不能与class同名?
选 C “constructor必须与class同名,但方法不能与class同名”这句话是错误的,方法是可以和class同名的:方法可以和类名同名的,和构造方法唯一的区别就是,构造方法没有返回值.
- python 简单工厂模式
abc 是抽象类模块abc.ABC 是继承抽象类 也可直接继承 (metaclass=ABCMeta)abc.abstractmethod 是定义抽象方法 简单工厂模式:通过接口创建对象,但不会暴露 ...
- vue中使用iview组件库实现图片的上传
vue文件如下: iview中Upload 属性详情 https://www.iviewui.com/components/upload js文件 :
- H5调起IOS原生商店支付
参考文档:http://www.html5plus.org/doc/zh_cn/payment.html 申请内购项目摘自 https://www.jianshu.com/p/1e79bfbe46e2 ...
- 仿EXCEL插件,智表ZCELL产品V1.6 版本发布,增加自定义事件功能
详细请移步 智表(ZCELL)官网www.zcell.net 更新说明 这次更新主要应用户要求,主要解决了单元格值变化时事件的支持,并新增了按单元格名操作的相关API,欢迎大家体验使用. 本次版本更 ...