SpringBoot 将数据库数据导出成txt 并实现下载功能

ftl页面:


            {{data2.studentLevelName}}
            {{data2.totleNumber}}
            {{data2.averageLines}}
            {{data2.averageScore}}
            
                未备份
                已备份
            
            {{formatDate(data2.backupTime)}}
            
                备份  
                导出备份  
                清除
                删除
            


    

导出

controller层:

 

/**
 * 备份处理*/
@RequestMapping("/admin/backup")
public String backup(String levelId){
    System.out.println(levelId);
    ResultInfo resultInfo=studentLevelService.backUp(levelId);
    if (resultInfo.getResultCode()==ResultInfo.RESULT_CODE_ERROR){
        return "admin/404";
    }else {
        return "redirect:/admin/levelRecordListIndex";
    }
}

Service层:

//    备份某个级别的做题记录
    ResultInfo backUp(String levelId);

 

  实现层  :

/**
 * 备份*/
    @Override
    public ResultInfo backUp(String levelId) {
        ResultInfo resultInfo=new ResultInfo<>();
        StudentLevel studentLevel=studentLevelDao.findOne(levelId);
        File path1=null;
        try {
            path1=new File(ResourceUtils.getURL("classpath:").getPath());
            System.out.println(path1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //写入李静
        String path=path1+"/static/backup/"+studentLevel.getStudentLevelName()+".txt";
        try{
//            读取写入内容
            Student student=new Student();
            student.setStudentLevelId(levelId);
            Example example= Example.of(student);
            List studentList=studentDao.findAll(example);
            List infosList=new ArrayList<>();
            List list=new ArrayList();
            for (int i=0;i exerciseSubmitInfosExample =Example.of(exerciseSubmitInfos);
                infosList=exerciseSubmitInfosDao.findAll(exerciseSubmitInfosExample);
                for (int j=0;j 
  

Txt工具类:

package com.haue.jobonline.utils;

import com.haue.jobonline.entity.ExerciseSubmitInfos;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author 赵鑫
 * @Time: 2018/7/28
 * @Email:[email protected]
 */
public class TXTUtils {
    public static void writeToTxt( List list,String path) {
        FileOutputStream outSTr = null;
        BufferedOutputStream Buff = null;
        String path1 = path;
        String tab = "  ";
        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();
            }
        }
    }
}

 

 

下载

controller层:

@RequestMapping("/admin/downloadTxt")
public ResponseEntity download2(String levelId) throws IOException {
    System.out.println("请求执行");
    File file=studentLevelService.buildTxtById(levelId);
    return ResponseUtils.buildResponseEntity(file);
}

Service层:

//下载某个级别
    File buildTxtById(String levelId);

  实现层   :

/**
 * 下载备份*/
public File buildTxtById(String levelId){
    //do something to find this fi
    File file=null;
    try {
        Integer name=studentLevelDao.findOne(levelId).getStudentLevelName();
        file = ResourceUtils.getFile("classpath:static/backup/"+name+".txt");
        System.out.println("文件的路径为"+file.getPath());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return file;
}

下载文件工具类:

package com.haue.jobonline.utils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author 赵鑫
 * @Time: 2018/7/22
 * @Email:[email protected]
 */
public class ResponseUtils {
    public static ResponseEntity 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 entity = new ResponseEntity(body, headers, statusCode);
        return entity;
    }
}

你可能感兴趣的:(SpringBoot 将数据库数据导出成txt 并实现下载功能)