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 ...
随机推荐
- python基础 — 循环重新输入
后续完善各种循环案例 while True: try: str_num = input('input a number:') num = float(str_num) print("你输入的 ...
- day38——线程queue、事件event、协程
day38 线程queue 多线程抢占资源 只能让其串行--用到互斥锁 线程queue 队列--先进先出(FIFO) import queue q = queue.Queue(3) q.put(1) ...
- 01-打印Hello World、变量
一.打印Hello World 下面就是我们写的打印hello world程序 在go语言中://代表单行注释,/**/代表多行注释 //单行注释 /* 多行注释 多行注释 */ package ma ...
- 【scratch3.0教程】 2.3 奥运五环
(1)编程前的准备 在设计一个作品之前,必须先策划一个脚本,然后再根据脚本,收集或制作素材(图案,声音等),接着就可以启动Scratch,汇入角色.舞台,利用搭程序积木的方式编辑程序,制作出符合脚本的 ...
- ORACLE数据库实现自增的两种方式
Mysql数据库因为其有自动+1,故一般我们不需要花费太多时间,直接用关键字auto_increment即可,但是Oracle不行,它没有自动增长机制.顾我们需要自己去实现.一般有两种方式,但是这两种 ...
- SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径
Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...
- Unity的学习笔记(鼠标移动控制视角移动)
using UnityEngine; public class MouseLook : MonoBehaviour { , MouseX = , MouseY = } //定义一个枚举,移动xy,或者 ...
- MySql 严格模式相关配置
目录 MySql 严格模式 MySQL的sql_mode合理设置 sql model 常用来解决下面几类问题 sql_mode常用值 注意 改为严格模式后可能会存在的问题 模式设置和修改(以解决上述问 ...
- 用Visual Studio 2015 编写 MASM 汇编程序(二)从头开发一个Win32汇编程序
一,建立一个VC的控制台类型的空工程: 1,从VS菜单中选择“文件”->“新建”->“项目”. 2,在新建项目中选择:“Visual c++”->"Win32"- ...
- Linux expect实现自动登录
expect expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我们就没有办法 ...