内容简介

本文主要内容为使用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 导出/下载文本文件操作(包含写文本文件)的更多相关文章

  1. Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. Spring Boot 导出Excel表格

    Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...

  4. Spring Boot(二):数据库操作

    本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...

  5. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  6. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  7. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java Spring Boot VS .NetCore (七) 配置文件

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  2. Linux学习-基本命令2

    安装tree命令 yum -y install tree 测试 tree /tmp [root@wyx ~]# tree /tmp/ /tmp/ ├── anaconda.log ├── hsperf ...

  3. Python开发之virtualenv和virtualenvwrapper详解

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  4. Python简介及开发环境搭建

    Python简介 Python是一门动态解释性的强类型定义的计算机程序设计语言,是一种完全面向对象的语言,由荷兰人"龟叔"-Guido van Rossum于1989年开发,于19 ...

  5. Java集合对比

    1.array和ArrayList 的区别?1.1:ArrayList是Array的复杂版本1.2:数组不能扩容集合可以扩容1.3:存储的数据类型:Array只能存储相同数据类型的数据,而ArrayL ...

  6. QT支持TTF字体

    1.编译QT库时需要支持TTF字体 ./configure -qt-freetype -fontconfig ... 2.将TTF字符库拷贝到QT_QPA_FONTDIR下 # 注意,如果不清楚字体所 ...

  7. Helm命令日常使用

    更换仓库 默认的stable仓库地址是:https://kubernetes-charts.storage.googleapis.com 若遇到Unable to get an update from ...

  8. JavaScript,数组,方法;

    JavaScript,数组除了常见的增删查改,其他的方法也有很多. let arr = [1, 3, 7, 6, 9, 4, 6, 2]; 1:every方法, 对数组进行遍历,每个元素都满足条件时, ...

  9. mac上使用gitlab拉项目报错Permissions 0644 for ...

    解决办法:执行命令sudo chmod 0600 /Users/***(电脑名)/.ssh/id_rsa

  10. Selenium文件上传

    转自:https://www.cnblogs.com/miaojjblog/p/9679915.html Web上本地上传图片,弹出的框Selenium是无法识别的,也就是说,selenium本身没有 ...