zoukankan      html  css  js  c++  java
  • 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 = "
    ";
            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;
        }
  • 相关阅读:
    移动端兼容
    三点优化
    面向对象(一)
    BootCDN和npm
    分页逻辑
    多物体运动框架
    兼容样式
    省略
    行内元素在水平和垂直排列的时候会有间距
    [Swift]LeetCode1053.交换一次的先前排列 | Previous Permutation With One Swap
  • 原文地址:https://www.cnblogs.com/codecat/p/11556169.html
Copyright © 2011-2022 走看看