内容简介

本文主要内容为使用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. TCP协议的11种状态及其变化过程?传输的内容又是什么?

    在TCP的11种状态变迁中,我们需要用到TCP头部的三个标志位: 1.SYN,SYN=1表示这是一个连接请求报文或者连接接受报文 2.ACK,ACK=1,表示确认号生效 3.FIN,FIN=1表示发送 ...

  2. [转帖]IBM报告:多国央行考虑发行数字货币 最快5年内问世

    IBM报告:多国央行考虑发行数字货币 最快5年内问世 https://news.cnblogs.com/n/646001/ DCEP 中国央行可能是第一家发布 数字货币的央行 DCEP 是基于 UTX ...

  3. JAVAWEB实现增删查改(图书信息管理)之修改功能实现

    首先通过点击index.jsp页面的修改按钮,获取该行的id:↓ 其次,跳转到updateBooks.jsp页面进行修改信息,页面代码如下:↓ <%@ page import="Boo ...

  4. mysql常用处理时间的相关函数

    1.DATE_ADD() 函数向日期添加指定的时间间隔 DAY) AS OrderPayDate FROM Orders 2.DATE_SUB() 函数从日期减去指定的时间间隔 DAY) AS Sub ...

  5. java之struts2之文件下载

    1.在实际应用开发中,文件下载功能也非常常见. 2.最简单的文件下载方式是通过超链接来进行文件下载: <body> <a href="download/s.txt" ...

  6. Java 面向对象知识扩展

    四种权限修饰符 java有四种权限修饰符:public > protected > (default) > private   public protected default pr ...

  7. 易百教程人工智能python修正-人工智能监督学习(分类)

    分类技术或模型试图从观测值中得出一些结论. 在分类问题中,我们有分类输出,如“黑色”或“白色”或“教学”和“非教学”. 在构建分类模型时,需要有包含数据点和相应标签的训练数据集. 例如,如果想检查图像 ...

  8. iOS - WebRTC的实现原理

    再简单地介绍一下webrtc: WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,简而言之它是一个支持网页浏览器进行实时语音对话或视频对话的技术. 它 ...

  9. Mybatis全部标签与解释说明

    一.定义SQL语句 (1)select 标签的使用 属性介绍: id :唯一的标识符. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User或user ...

  10. css设置全局变量和局部变量

    在我们使用less或者sass时常常会使用到局部变量和全局变量,其实在我们使用css做开发时也可以定义全局变量和局部 变量来简化我们的开发效率,很简单也很实用:1.设置全局变量只需要在我们的根引用的c ...