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 ...
随机推荐
- Vue(六)插槽(2.6.0+)
插槽在vue2.6.0开始有了新的更新 具名插槽(数据来自父组件) 子组件(定义插槽)这里版本前后没什么变化 <template> <div> <header> & ...
- Arduino硬件之NCF技术(近场通信技术)
Arduino硬件之NCF技术(近场通信技术) 版权转载:https://blog.csdn.net/import_sadaharu/article/details/52437488 Android硬 ...
- python使用matplotlib在一个图形中绘制多个子图以及一个子图中绘制多条动态折线问题
在讲解绘制多个子图之前先简单了解一下使用matplotlib绘制一个图,导入绘图所需库matplotlib并创建一个等间隔的列表x,将[0,2*pi]等分为50等份,绘制函数sin(x).当没有给定x ...
- 基础数字电路的Verilog写法
Verilog是硬件描述电路,我对此一直稀里糊涂,于是将锆石科技开发板附带的的一些基础数字电路Verilog程序整理记录下来,并且查看他们的RTL视图,总算有点理解了. 1.基本运算符 module ...
- IdentityServer4(客户端授权模式)
1.新建三个项目 IdentityServer:端口5000 IdentityAPI:端口5001 IdentityClient: 2.在IdentityServer项目中添加IdentityServ ...
- [洛谷P5431]【模板】乘法逆元2
题目大意:给定$n(n\leqslant5\times10^6)$个正整数$a_i$,和$k$.求:$$\sum_{i=1}^n\dfrac{k^i}{a_i}\pmod p$$题解:$$令P=\pr ...
- MSMQ消息加密
证书实现非对称加密/解密的代码如下 //非对称加密密钥 static byte[] RSAEncrypt(byte[] enkey, X509Certificate2 Certificate) { R ...
- stack + positioned
stack 下套container, 发现最大的显示,小的都没显示, 把所有都套个POSITIONED, 都正常显示了.
- JavaScript前端图片压缩
实现思路 获取input的file 使用fileReader() 将图片转为base64 使用canvas读取base64 并降低分辨率 把canvas数据转成blob对象 把blob对象转file对 ...
- [React] 函数定义组件
函数定义组件的例子 function Welcome(props) { return <h1>Hello, {props.name}</h1>; } 该函数是一个有效的 Rea ...