java spring boot 导出/下载文本文件操作(包含写文本文件)

摘要:
本文的主要内容是使用java将内容写入文本文件,并实现下载/导出功能。

内容简介

本文主要内容为使用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 = "
";
        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 导出/下载文本文件操作(包含写文本文件)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【Go语言系列】在VsCode中配置Go的开发环境SQL SERVER 2008 R2 自动备份并删除过期备份数据下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

shell脚本删除远程过期文件

应用场景: 我们在工作中经常会有要定时输出一些文件到磁盘上用作离线词表或者数据的冷备,但是如果这些数据不定期清理的话,则会对我们的磁盘造成巨大的浪费,人工手动清理的话,总是会有遗忘的时候,所以此刻就需要有一个自动清理的脚本。 假设现场:我们有一个定时生成词表到指定目录的程序,输出的目录名和词表前缀相同,且在生成词表的同时,会将词表名和对应的md5输出到词表...

easyPoi 工具类

import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.ea...

Servlet第六篇【Session介绍、API、生命周期、应用】

什么是Session Session 是另一种记录浏览器状态的机制。不同的是Cookie保存在浏览器中,Session保存在服务器中。用户使用浏览器访问服务器的时候,服务器把用户的信息以某种的形式记录在服务器,这就是Session 如果说Cookie是检查用户身上的”通行证“来确认用户的身份,那么Session就是通过检查服务器上的”客户明细表“来确认用...

【转】centos 服务开机启动

转自: http://blog.csdn.net/educast/article/details/49558945 http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html 1、利用 chkconfig 来配置启动级别在CentOS或者RedHat其他系统下,如果是后面安装的服务...

Qt编写地图综合应用6-百度在线地图

一、前言 百度在线地图的应用老早就做过,后面经过不断的完善才到今天的这个程序,除了基本的可以载入地图并设置一些相关的属性以外,还增加了各种js函数直接异步加载数据比如动态添加点、矩形、圆形、行政区划等各种。当然最大的是增加了离线地图的支持,当年这个离线地图拖了很久很久才去做,最终还是搞定了。 在线地图没有太多的难点,搞一个简单的在线地图demo绝对是分分钟...

ThreadPool.QueueUserWorkItem的性能问题

在WEB开发中,为了减少页面等待时间提高用户体验,我们往往会把一些浪费时间的操作放到新线程中在后台运行。 简单的实现代码就是: //代码一 new Thread(()=>{ //do something }).Start();   但是对于一个请求量大的网址这样做是很不现实的——每一个操作都要开启一个新线程,最终会因CPU不堪重负而...