ftl页面:
<tr v-for="data2 in content"> <td>{{data2.studentLevelName}}</td> <td>{{data2.totleNumber}}</td> <td>{{data2.averageLines}}</td> <td>{{data2.averageScore}}</td> <td> <span style="color: red" v-if="data2.backupState==0">未备份</span> <span style="color: green" v-if="data2.backupState==1">已备份</span> </td> <td>{{formatDate(data2.backupTime)}}</td> <td> <a class="text-main" href="JavaScript:;" v-on:click="backup(data2.studentLevelId);">备份</a> <a class="text-main" href="JavaScript:;" v-on:click="exportBackup(data2.studentLevelId);">导出备份</a> <a class="text-red" href="JavaScript:;" v-on:click="delete_app(data2.studentLevelId,data2.backupState);">清除</a> <a class="text-red" href="JavaScript:;" v-on:click="delete_level_app(data2.studentLevelId);">删除</a> </td> </tr>
导出
controller层:
/** * 备份处理*/ @RequestMapping("/admin/backup") public String backup(String levelId){ System.out.println(levelId); ResultInfo<StudentLevel> resultInfo=studentLevelService.backUp(levelId); if (resultInfo.getResultCode()==ResultInfo.RESULT_CODE_ERROR){ return "admin/404"; }else { return "redirect:/admin/levelRecordListIndex"; } }
Service层:
// 备份某个级别的做题记录 ResultInfo<StudentLevel> backUp(String levelId);
实现层 :
/** * 备份*/ @Override public ResultInfo<StudentLevel> backUp(String levelId) { ResultInfo<StudentLevel> 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<Student> example= Example.of(student); List<Student> studentList=studentDao.findAll(example); List<ExerciseSubmitInfos> infosList=new ArrayList<>(); List list=new ArrayList(); for (int i=0;i<studentList.size();i++){ ExerciseSubmitInfos exerciseSubmitInfos=new ExerciseSubmitInfos(); exerciseSubmitInfos.setStuId(studentList.get(i).getStudentId()); Example<ExerciseSubmitInfos> exerciseSubmitInfosExample =Example.of(exerciseSubmitInfos); infosList=exerciseSubmitInfosDao.findAll(exerciseSubmitInfosExample); for (int j=0;j<infosList.size();j++){ Map map=new HashMap(); ExerciseSubmitInfos exerciseSubmitInfos1=infosList.get(j); map.put("问题id",exerciseSubmitInfos1.getExerciseQuestionId()); map.put("学生id",exerciseSubmitInfos1.getStuId()); map.put("代码行数",exerciseSubmitInfos1.getCodeLines()); map.put("代码类型",exerciseSubmitInfos1.getCodeStyle()); map.put("代码大小",exerciseSubmitInfos1.getCodeSize()); map.put("结果信息",exerciseSubmitInfos1.getResultMessage()); map.put("结果得分",exerciseSubmitInfos1.getResultScore()); map.put("结果状态",exerciseSubmitInfos1.getResultStatus()); map.put("代码内容",exerciseSubmitInfos1.getSubmitCode()); map.put("使用内存",exerciseSubmitInfos1.getUseMemory()); map.put("使用时间",exerciseSubmitInfos1.getUseTime()); //将map放入list集合传入工具类 list.add(map); } } TXTUtils.writeToTxt(list,path); //将备份状态改为已备份 studentLevel.setBackupState(Backup.COPY_SATE_AFTER); studentLevelDao.saveAndFlush(studentLevel); resultInfo.setResultCode(ResultInfo.RESULT_CODE_SUCCESS); }catch (Exception e){ logger.error(e.getMessage(),e); } return resultInfo; }
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:1272743926@qq.com */ 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<byte[]> 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:1272743926@qq.com */ public class ResponseUtils { public static 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; } }