加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 07:42:13 所属栏目:Java 来源:网络整理
导读:内容简介 本文主要内容为使用java把内容写入文本文件,并实现下载/导出的功能。 实现步骤 1. controller层 @ResponseBody @RequestMapping(value = "/exportLand2ndClassIndex",method = RequestMethod.GET) public ResponseEntity byte [] exportLand2ndCla

内容简介

本文主要内容为使用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 = "rn";
        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;
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读