|
@@ -15,6 +15,7 @@ import com.rf.kjb.utils.Result;
|
|
|
import com.rf.kjb.utils.ZipUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -31,11 +32,12 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
@@ -62,51 +64,51 @@ public class ChatController extends BaseController {
|
|
|
|
|
|
@GetMapping("/getQuestion/{label}")
|
|
|
@ApiOperation("查询问题信息:首次查询时,id传空字符串即可查询出第一道题目;questionType=0表示选择题,questionType=1表示填空题;当改问题的nextQuestionNo 不为空是,则表示改题为陈述,没有答案,直接在显示此问题之后再次请求该接口显示questionNo对应的题目信息即可")
|
|
|
- public Result getQuestion(String id , @PathVariable String label){
|
|
|
+ public Result getQuestion(String id, @PathVariable String label) {
|
|
|
|
|
|
- if(StringUtils.isBlank(id)){
|
|
|
+ if (StringUtils.isBlank(id)) {
|
|
|
id = "1";
|
|
|
}
|
|
|
- ChatQuestionEntity questionEntity = this.questionService.findByIdAndLabel(id,label);
|
|
|
+ ChatQuestionEntity questionEntity = this.questionService.findByIdAndLabel(id, label);
|
|
|
return success(questionEntity);
|
|
|
}
|
|
|
|
|
|
@PostMapping("/import/{label}/faq")
|
|
|
@ApiOperation("智能问答导入")
|
|
|
- public Result importQuestionAndAnswer(@RequestPart("file") MultipartFile file, @PathVariable String label){
|
|
|
+ public Result importQuestionAndAnswer(@RequestPart("file") MultipartFile file, @PathVariable String label) {
|
|
|
// FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
- String [] fileNames = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
+ String[] fileNames = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
File targetFile = null;
|
|
|
List<ChatQuestionEntity> chatQuestionEntityList = new ArrayList<>();
|
|
|
List<ChatAnswerEntity> chatAnswerEntityList = new ArrayList<>();
|
|
|
try {
|
|
|
// targetFile = new File("./"+fileNames[0]+ UUID.randomUUID()+"."+fileNames[1]);
|
|
|
// FileUtils.copyInputStreamToFile(file.getInputStream(),targetFile);
|
|
|
- targetFile = File.createTempFile(fileNames[0],"."+fileNames[1]);
|
|
|
+ targetFile = File.createTempFile(fileNames[0], "." + fileNames[1]);
|
|
|
file.transferTo(targetFile);
|
|
|
List<List<List<Object>>> datas = ExcelUtil.getBankListByExcelSheet(Files.newInputStream(Paths.get(targetFile.getAbsolutePath())), targetFile.getName());
|
|
|
- List<List<Object>> questionList = datas.get(0);
|
|
|
- List<List<Object>> answerList = datas.get(1);
|
|
|
- questionList.forEach(item ->{
|
|
|
+ List<List<Object>> questionList = datas.get(0);
|
|
|
+ List<List<Object>> answerList = datas.get(1);
|
|
|
+ questionList.forEach(item -> {
|
|
|
ChatQuestionEntity chatQuestionEntity = new ChatQuestionEntity();
|
|
|
chatQuestionEntity.setId((String) item.get(0));
|
|
|
chatQuestionEntity.setQuestion((String) item.get(1));
|
|
|
chatQuestionEntity.setNextQuestionNo((String) item.get(2));
|
|
|
String questionType = (String) item.get(3);
|
|
|
- if (questionType.equals("单选")){
|
|
|
+ if (questionType.equals("单选")) {
|
|
|
chatQuestionEntity.setQuestionType("0");
|
|
|
- }else if (questionType.equals("填空")){
|
|
|
+ } else if (questionType.equals("填空")) {
|
|
|
chatQuestionEntity.setQuestionType("1");
|
|
|
- }else if (questionType.equals("多选")){
|
|
|
+ } else if (questionType.equals("多选")) {
|
|
|
chatQuestionEntity.setQuestionType("2");
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
chatQuestionEntity.setQuestionType("0");
|
|
|
}
|
|
|
//chatQuestionEntity.setScaleFlag((String) item.get(4));
|
|
|
chatQuestionEntity.setLabel(label);
|
|
|
chatQuestionEntityList.add(chatQuestionEntity);
|
|
|
});
|
|
|
- answerList.forEach(item ->{
|
|
|
+ answerList.forEach(item -> {
|
|
|
ChatAnswerEntity chatAnswerEntity = new ChatAnswerEntity();
|
|
|
chatAnswerEntity.setQuestionNo((String) item.get(0));
|
|
|
chatAnswerEntity.setNextQuestionNo((String) item.get(1));
|
|
@@ -118,7 +120,7 @@ public class ChatController extends BaseController {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
|
|
|
- if(chatQuestionEntityList.size()>0 && chatAnswerEntityList.size()>0){
|
|
|
+ if (chatQuestionEntityList.size() > 0 && chatAnswerEntityList.size() > 0) {
|
|
|
this.questionService.deleteByLabel(label);
|
|
|
this.answerService.deleteByLabel(label);
|
|
|
|
|
@@ -130,39 +132,40 @@ public class ChatController extends BaseController {
|
|
|
return success();
|
|
|
|
|
|
}
|
|
|
+
|
|
|
@GetMapping("/getAnswer/{questionNo}/{label}")
|
|
|
@ApiOperation("查询答案列表,questionNo为问题的id字段,nextQuestionNo 为此答案被选中后的下一个应该呈现的问题")
|
|
|
- public Result getAnswer(@PathVariable String questionNo,@PathVariable String label){
|
|
|
- List<ChatAnswerEntity> answerEntities = this.answerService.findByQuestionNoAndLabel(questionNo,label);
|
|
|
+ public Result getAnswer(@PathVariable String questionNo, @PathVariable String label) {
|
|
|
+ List<ChatAnswerEntity> answerEntities = this.answerService.findByQuestionNoAndLabel(questionNo, label);
|
|
|
return success(answerEntities);
|
|
|
}
|
|
|
|
|
|
@GetMapping("/getNextQuestionByScaleResult")
|
|
|
@ApiOperation("根据量表测试结果查询下一问题编号")
|
|
|
- public Result getNextQuestionByScaleResult(String label,String result){
|
|
|
+ public Result getNextQuestionByScaleResult(String label, String result) {
|
|
|
|
|
|
- ResultQuestionEntity resultQuestionEntity = this.resultQuestionService.findByLabelAndResult(label,result);
|
|
|
- if (resultQuestionEntity == null){
|
|
|
+ ResultQuestionEntity resultQuestionEntity = this.resultQuestionService.findByLabelAndResult(label, result);
|
|
|
+ if (resultQuestionEntity == null) {
|
|
|
return fail(ErrorCode.NEXT_QUESTION_NO_NOT_FOUND);
|
|
|
}
|
|
|
ChatQuestionEntity byIdAndLabel = this.questionService.findByIdAndLabel(resultQuestionEntity.getNextQuestionNo(), label);
|
|
|
- if(byIdAndLabel == null){
|
|
|
+ if (byIdAndLabel == null) {
|
|
|
return fail(ErrorCode.NEXT_QUESTION_NO_ERROR);
|
|
|
}
|
|
|
return success(resultQuestionEntity);
|
|
|
}
|
|
|
|
|
|
@PostMapping("/complete/chat")
|
|
|
- @ApiOperation(value = "结束会话,保存会话记录",notes = "identifier:用户编号;label:只能对话分类 1-焦虑;2-抑郁;3-失眠;4-压力;content:会话详情")
|
|
|
- public Result completeChat(@RequestBody String json ) throws IOException {
|
|
|
- ChatRecordEntity chatRecordEntity = JSONObject.parseObject(json,ChatRecordEntity.class);
|
|
|
+ @ApiOperation(value = "结束会话,保存会话记录", notes = "identifier:用户编号;userName:用户名;label:智能对话分类 1-焦虑;2-抑郁;3-失眠;4-压力;content:会话详情")
|
|
|
+ public Result completeChat(@RequestBody String json) throws IOException {
|
|
|
+ ChatRecordEntity chatRecordEntity = JSONObject.parseObject(json, ChatRecordEntity.class);
|
|
|
|
|
|
//生成文件
|
|
|
- List<ChatEntity> chatEntityList = JSONObject.parseArray(chatRecordEntity.getContent(),ChatEntity.class);
|
|
|
- if(chatEntityList!= null &&chatEntityList.size()>0){
|
|
|
- String filePath = "./对话记录/"+chatRecordEntity.getIdentifier()+"-对话记录-"+ DateUtil.getNowTime_CN()+".xlsx";
|
|
|
+ List<ChatEntity> chatEntityList = JSONObject.parseArray(chatRecordEntity.getContent(), ChatEntity.class);
|
|
|
+ if (chatEntityList != null && chatEntityList.size() > 0) {
|
|
|
+ String filePath = "./对话记录/" + chatRecordEntity.getIdentifier() + "-对话记录-" + DateUtil.getNowTime_CN() + ".xlsx";
|
|
|
File file = new File(filePath);
|
|
|
- if(!file.exists()){
|
|
|
+ if (!file.exists()) {
|
|
|
file.createNewFile();
|
|
|
}
|
|
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
|
@@ -170,63 +173,61 @@ public class ChatController extends BaseController {
|
|
|
XSSFSheet sheet = workbook.createSheet("对话记录");
|
|
|
XSSFRow titleRow = sheet.createRow(0);
|
|
|
XSSFCell cellOrder = titleRow.createCell(0);
|
|
|
- cellOrder.setCellValue("序号");
|
|
|
+ cellOrder.setCellValue("角色");
|
|
|
XSSFCell cellQuestion = titleRow.createCell(1);
|
|
|
- cellQuestion.setCellValue("问题");
|
|
|
- XSSFCell cellAnswer = titleRow.createCell(2);
|
|
|
- cellAnswer.setCellValue("答案");
|
|
|
- int index = 1;
|
|
|
- chatEntityList.forEach(item -> {
|
|
|
- XSSFRow row = sheet.createRow(index);
|
|
|
- for (int i=0;i<4;i++){
|
|
|
- XSSFCell cellOrder1 = row.createCell(0);
|
|
|
- cellOrder1.setCellValue(index);
|
|
|
- XSSFCell cellQuestion1 = row.createCell(1);
|
|
|
- cellQuestion1.setCellValue(item.getQuestion());
|
|
|
- XSSFCell cellAnswer1 = row.createCell(2);
|
|
|
- cellAnswer1.setCellValue(item.getAnswer());
|
|
|
+ cellQuestion.setCellValue("内容");
|
|
|
+ for (int i = 0; i < chatEntityList.size(); i++) {
|
|
|
+ XSSFRow row = sheet.createRow(i + 1);
|
|
|
+ XSSFCell cellOrder1 = row.createCell(0);
|
|
|
+ if (chatEntityList.get(i).getFrom().equals("1")) {
|
|
|
+ cellOrder1.setCellValue("智能助手:");
|
|
|
+ } else {
|
|
|
+ cellOrder1.setCellValue("用户:");
|
|
|
}
|
|
|
- });
|
|
|
+ XSSFCell cellQuestion1 = row.createCell(1);
|
|
|
+ cellQuestion1.setCellValue(chatEntityList.get(i).getQuestion());
|
|
|
+ }
|
|
|
OutputStream outputStream = Files.newOutputStream(file.toPath());
|
|
|
workbook.write(outputStream);
|
|
|
outputStream.close();
|
|
|
//保存记录
|
|
|
chatRecordEntity.setFilePath(filePath);
|
|
|
+ chatRecordEntity.setCreateTime(new Date());
|
|
|
this.chatRecordService.save(chatRecordEntity);
|
|
|
return success();
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
return fail(ErrorCode.CHAT_CONTENT_EMPTY);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@GetMapping("/chat/list")
|
|
|
- @ApiOperation(value = "智能对话列表",notes = "identifier:编号;beginDate:查询起始日期;endDate:查询结束日期;pageNum:页数;pageSize:每页记录数")
|
|
|
- public Result getChatList(String identifier,String beginDate,String endDate,int pageNum,int pageSize){
|
|
|
- Page<ChatEntity> chatEntities = this.chatRecordService.find(identifier,beginDate,endDate,pageNum,pageSize);
|
|
|
- return success(chatEntities);
|
|
|
+ @ApiOperation(value = "智能对话列表", notes = "identifier:编号;beginDate:查询起始日期;endDate:查询结束日期;pageNum:页数;pageSize:每页记录数")
|
|
|
+ public Result getChatList(String identifier, String beginDate, String endDate, int pageNum, int pageSize) {
|
|
|
+ Page<ChatRecordEntity> chatRecordEntities = this.chatRecordService.find(identifier, beginDate, endDate, pageNum, pageSize);
|
|
|
+ return success(chatRecordEntities);
|
|
|
}
|
|
|
|
|
|
@GetMapping("/chat/download")
|
|
|
- @ApiOperation(value = "下载对话记录",notes = "")
|
|
|
+ @ApiOperation(value = "下载对话记录", notes = "")
|
|
|
public Result downloadChatList(@RequestBody String json, HttpServletResponse response) throws IOException {
|
|
|
- List<String> list = JSONObject.parseArray(json,String.class);
|
|
|
- if(list.size()>0){
|
|
|
+ List<String> list = JSONObject.parseArray(json, String.class);
|
|
|
+ if (list.size() > 0) {
|
|
|
for (String item : list) {
|
|
|
File file = new File(item);
|
|
|
- if(file.exists()){
|
|
|
+ if (file.exists()) {
|
|
|
InputStream inputStream = null;
|
|
|
OutputStream outputStream = null;
|
|
|
try {
|
|
|
inputStream = Files.newInputStream(file.toPath());
|
|
|
- outputStream = new FileOutputStream("./对话列表/"+item);
|
|
|
- IOUtils.copy(inputStream,outputStream);
|
|
|
+ outputStream = new FileOutputStream("./对话列表/" + item);
|
|
|
+ IOUtils.copy(inputStream, outputStream);
|
|
|
} catch (IOException e) {
|
|
|
- log.error("对话记录下载失败:"+e.getMessage());
|
|
|
- }finally {
|
|
|
- if(inputStream != null){
|
|
|
+ log.error("对话记录下载失败:" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
- if(outputStream != null){
|
|
|
+ if (outputStream != null) {
|
|
|
outputStream.close();
|
|
|
}
|
|
|
}
|
|
@@ -234,15 +235,257 @@ public class ChatController extends BaseController {
|
|
|
}
|
|
|
|
|
|
File dir = new File("./对话列表");
|
|
|
- if(!dir.exists()){
|
|
|
+ if (!dir.exists()) {
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ } else {
|
|
|
+ ZipUtils.createZip("./对话列表", "./对话列表" + DateUtil.getNowTime_CN() + ".zip", false);
|
|
|
+ FileUtils.downloadFile(new File("./对话列表", "对话列表" + DateUtil.getNowTime_CN() + ".zip"), "", "对话列表" + DateUtil.getNowTime_CN() + ".zip", response);
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "对话记录下载")
|
|
|
+ @GetMapping("/chatRecord/download")
|
|
|
+ public Result chatRecordDownload(String id, HttpServletResponse response) throws Exception {
|
|
|
+ if (id != null) {
|
|
|
+ ChatRecordEntity chatRecordEntity = this.chatRecordService.findById(id);
|
|
|
+ if (chatRecordEntity == null) {
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+ File file = null;
|
|
|
+ if (new File(chatRecordEntity.getFilePath()).exists()) {
|
|
|
+ file = new File(chatRecordEntity.getFilePath());
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentLength((int) file.length());
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
+
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ bis = new BufferedInputStream(fis);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int i = bis.read(buffer);
|
|
|
+ while (i != -1) {
|
|
|
+ os.write(buffer, 0, i);
|
|
|
+ i = bis.read(buffer);
|
|
|
+ }
|
|
|
+ return success();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (bis != null) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ @ApiOperation(value = "对话记录批量下载接口")
|
|
|
+ @GetMapping("/chatRecord/batchDownload")
|
|
|
+ public Result chatRecordBatchDownload(@RequestParam(value = "ids", required = false) List<String> recordingIds, HttpServletResponse response) {
|
|
|
+ //原始记录文件
|
|
|
+ File file = null;
|
|
|
+ //原始记录复制后文件
|
|
|
+ File destFile = null;
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
+ String filePath ="./对话记录/记录下载";
|
|
|
+ //原始记录文件名
|
|
|
+ String fileName = null;
|
|
|
+ for (int i = 0; i < recordingIds.size(); i++) {
|
|
|
+ ChatRecordEntity chatRecordEntity = this.chatRecordService.findById(recordingIds.get(i));
|
|
|
+ String[] split = chatRecordEntity.getFilePath().split("/");
|
|
|
+ if (split.length > 1){
|
|
|
+ fileName = split[split.length-1];
|
|
|
}else {
|
|
|
- ZipUtils.createZip("./对话列表","./对话列表"+ DateUtil.getNowTime_CN()+".zip",false);
|
|
|
- FileUtils.downloadFile(new File("./对话列表","对话列表"+ DateUtil.getNowTime_CN()+".zip"),"","对话列表"+ DateUtil.getNowTime_CN()+".zip",response);
|
|
|
+ fileName = chatRecordEntity.getFilePath();
|
|
|
+ }
|
|
|
+ //设置文件路径
|
|
|
+ file = new File(chatRecordEntity.getFilePath());
|
|
|
+ if (file.exists()){
|
|
|
+ destFile = new File(filePath+"/"+fileName+".xlsx");
|
|
|
+ //将原始文件进行复制
|
|
|
+ org.apache.commons.io.FileUtils.copyFile(file, destFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
+ ZipUtils.createZip(filePath, filePath + ".zip", true);
|
|
|
+ File zipFile = new File(filePath + ".zip");
|
|
|
+ //下载压缩后文件
|
|
|
+ if (zipFile.exists()) {
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentLength((int) zipFile.length());
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("chatRecord.zip", "UTF-8"));
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(zipFile);
|
|
|
+ bis = new BufferedInputStream(fis);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int i = bis.read(buffer);
|
|
|
+ while (i != -1) {
|
|
|
+ os.write(buffer, 0, i);
|
|
|
+ i = bis.read(buffer);
|
|
|
+ }
|
|
|
return success();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (bis != null) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹删除
|
|
|
+ Path path = Paths.get(filePath);
|
|
|
+ try (Stream<Path> walk = Files.walk(path)) {
|
|
|
+ walk.sorted(Comparator.reverseOrder())
|
|
|
+ .forEach(ChatController::deleteDirectoryStream);
|
|
|
+ }
|
|
|
+ //删除生成的压缩包
|
|
|
+ Files.delete(Paths.get(filePath + ".zip"));
|
|
|
}
|
|
|
+ }
|
|
|
+ //删除生成的压缩包
|
|
|
+ Files.delete(Paths.get(filePath + ".zip"));
|
|
|
+ //将复制后的整个文件夹删除
|
|
|
+ Path path = Paths.get(filePath);
|
|
|
+ try (Stream<Path> walk = Files.walk(path)) {
|
|
|
+ walk.sorted(Comparator.reverseOrder())
|
|
|
+ .forEach(ChatController::deleteDirectoryStream);
|
|
|
+ }
|
|
|
+ return fail(ErrorCode.FAILED);
|
|
|
+ }
|
|
|
|
|
|
+ @SneakyThrows
|
|
|
+ @ApiOperation(value = "对话记录全部下载接口")
|
|
|
+ @GetMapping("/chatRecord/AllDownload")
|
|
|
+ public Result chatRecordAllDownload(String identifier, String beginDate, String endDate, HttpServletResponse response) {
|
|
|
+ List<ChatRecordEntity> chatRecordEntityList = this.chatRecordService.findAll(identifier, beginDate, endDate);
|
|
|
+ //原始记录文件
|
|
|
+ File file = null;
|
|
|
+ //原始记录复制后文件
|
|
|
+ File destFile = null;
|
|
|
+ //所有记录汇总后文件夹,用来压缩
|
|
|
+ String filePath ="./对话记录/记录下载";
|
|
|
+ //原始记录文件名
|
|
|
+ String fileName = null;
|
|
|
+ for (int i = 0; i < chatRecordEntityList.size(); i++) {
|
|
|
+ ChatRecordEntity chatRecordEntity = chatRecordEntityList.get(i);
|
|
|
+ String[] split = chatRecordEntity.getFilePath().split("/");
|
|
|
+ if (split.length > 1){
|
|
|
+ fileName = split[split.length-1];
|
|
|
+ }else {
|
|
|
+ fileName = chatRecordEntity.getFilePath();
|
|
|
+ }
|
|
|
+ //设置文件路径
|
|
|
+ file = new File(chatRecordEntity.getFilePath());
|
|
|
+ if (file.exists()){
|
|
|
+ destFile = new File(filePath+"/"+fileName+".xlsx");
|
|
|
+ //将原始文件进行复制
|
|
|
+ org.apache.commons.io.FileUtils.copyFile(file, destFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹打包
|
|
|
+ ZipUtils.createZip(filePath, filePath + ".zip", true);
|
|
|
+ File zipFile = new File(filePath + ".zip");
|
|
|
+ //下载压缩后文件
|
|
|
+ if (zipFile.exists()) {
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("application/octet-stream");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentLength((int) zipFile.length());
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("chatRecord.zip", "UTF-8"));
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ FileInputStream fis = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ fis = new FileInputStream(zipFile);
|
|
|
+ bis = new BufferedInputStream(fis);
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
+ int i = bis.read(buffer);
|
|
|
+ while (i != -1) {
|
|
|
+ os.write(buffer, 0, i);
|
|
|
+ i = bis.read(buffer);
|
|
|
+ }
|
|
|
+ return success();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (bis != null) {
|
|
|
+ try {
|
|
|
+ bis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将复制后的整个文件夹删除
|
|
|
+ Path path = Paths.get(filePath);
|
|
|
+ try (Stream<Path> walk = Files.walk(path)) {
|
|
|
+ walk.sorted(Comparator.reverseOrder())
|
|
|
+ .forEach(ChatController::deleteDirectoryStream);
|
|
|
+ }
|
|
|
+ //删除生成的压缩包
|
|
|
+ Files.delete(Paths.get(filePath + ".zip"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //删除生成的压缩包
|
|
|
+ Files.delete(Paths.get(filePath + ".zip"));
|
|
|
+ //将复制后的整个文件夹删除
|
|
|
+ Path path = Paths.get(filePath);
|
|
|
+ try (Stream<Path> walk = Files.walk(path)) {
|
|
|
+ walk.sorted(Comparator.reverseOrder())
|
|
|
+ .forEach(ChatController::deleteDirectoryStream);
|
|
|
}
|
|
|
return fail(ErrorCode.FAILED);
|
|
|
}
|
|
|
+
|
|
|
+ private static void deleteDirectoryStream(Path path) {
|
|
|
+ try {
|
|
|
+ Files.delete(path);
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|