|
|
@@ -2,7 +2,6 @@
|
|
|
package org.sky.scientific.controller;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
@@ -14,20 +13,18 @@ import lombok.SneakyThrows;
|
|
|
import org.sky.core.boot.ctrl.KdController;
|
|
|
import org.sky.core.secure.utils.SecureUtil;
|
|
|
import org.sky.core.tool.api.R;
|
|
|
-import org.sky.core.tool.utils.FileUtil;
|
|
|
import org.sky.scientific.mapper.DownloadTaskMapper;
|
|
|
import org.sky.scientific.pojo.entity.ArchiveAttachEntity;
|
|
|
import org.sky.scientific.pojo.entity.DownloadTask;
|
|
|
import org.sky.scientific.service.IArchiveService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
* 科研档案管理 控制器
|
|
|
@@ -66,128 +63,77 @@ public class ArchiveCenterController extends KdController {
|
|
|
service.downloadFjbczlZipFiles(request, response, year);
|
|
|
}
|
|
|
|
|
|
- @GetMapping("generate")
|
|
|
+ @PostMapping("generate")
|
|
|
@ApiOperationSupport(order = 6)
|
|
|
- @Operation(summary = "档案中心-一键下载-生成", description = "返回任务Id,后端生产待下载的文件")
|
|
|
- public R generateCenterZipFiles(DownloadTask task) {
|
|
|
+ @Operation(summary = "档案中心-一键下载/一键导出高新备查/一键导出加计备查-生成", description = "返回任务Id,后端生产待下载的文件")
|
|
|
+ public R generateCenterZipFiles(@Valid @RequestBody DownloadTask task) {
|
|
|
if (task.getId() != null) {
|
|
|
DownloadTask dbTask = downloadTaskMapper.selectById(task.getId());
|
|
|
if (dbTask == null) {
|
|
|
return R.fail("下载任务不存在,请联系管理员");
|
|
|
}
|
|
|
- if (dbTask.getStatus() == 2) {
|
|
|
- //todo 后续考虑过期需要删除已生成的文件
|
|
|
- return R.success("下载任务已完成");
|
|
|
- } else {
|
|
|
- return R.fail("下载任务进行中,请耐心等待");
|
|
|
- }
|
|
|
+ return R.data(dbTask);
|
|
|
+// if (dbTask.getStatus() == 2) {
|
|
|
+// //todo 后续考虑过期需要删除已生成的文件
|
|
|
+// return R.success("下载任务已完成");
|
|
|
+// } else {
|
|
|
+// return R.fail("下载任务进行中,请耐心等待");
|
|
|
+// }
|
|
|
} else {
|
|
|
- task.setYearAndMonth(task.getYearAndMonth());
|
|
|
+ task.setYearAndMonth(task.getYearAndMonth());
|
|
|
task.setTenantId(SecureUtil.getTenantId());
|
|
|
- int result = downloadTaskMapper.insert(task);
|
|
|
+ int result = downloadTaskMapper.insertDownloadTask( task);
|
|
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
|
|
if (result == 1) {
|
|
|
new Thread(() -> {
|
|
|
RequestContextHolder.setRequestAttributes(requestAttributes);
|
|
|
- service.downloadCenterZipFiles(task.getYearAndMonth(), task.getId());
|
|
|
+ service.downloadCenterZipFiles(task.getYearAndMonth(), task);
|
|
|
}).start();
|
|
|
return R.data(task);
|
|
|
}
|
|
|
}
|
|
|
return R.success();
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
@SneakyThrows
|
|
|
@GetMapping("center/download")
|
|
|
@ApiOperationSupport(order = 6)
|
|
|
- @Operation(summary = "档案中心-一键下载")
|
|
|
- public void downloadCenterZipFiles(HttpServletResponse response, String year, Long taskId) {
|
|
|
- System.out.println("---------------------"+Thread.currentThread().getName());
|
|
|
- if (taskId != null) {
|
|
|
- DownloadTask task = downloadTaskMapper.selectById(taskId);
|
|
|
- if (task == null) {
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- PrintWriter writer = response.getWriter();
|
|
|
- writer.write(JSON.toJSONString(R.fail("下载任务不存在,请联系管理员")));
|
|
|
- writer.flush();
|
|
|
- } else if (task.getStatus() == 2) {
|
|
|
- if (task.getExpireTime() != null && new Date().compareTo(task.getExpireTime()) > 0) {
|
|
|
- FileUtil.deleteQuietly(new File(task.getZipUrl()));
|
|
|
- downloadTaskMapper.deleteById(taskId);
|
|
|
- //24小时后需重新生成
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- PrintWriter writer = response.getWriter();
|
|
|
- writer.write(JSON.toJSONString(R.fail("文件已是24小时前生成的,请重新生成")));
|
|
|
- writer.flush();
|
|
|
- } else {
|
|
|
- //将磁盘生成的目录及文件压缩成zip
|
|
|
- OutputStream toClient = null;
|
|
|
- try {
|
|
|
- BufferedInputStream fis = new BufferedInputStream(new FileInputStream(task.getZipUrl()));
|
|
|
- byte[] buffer = new byte[fis.available()];
|
|
|
- fis.read(buffer);
|
|
|
- fis.close();
|
|
|
- response.reset();
|
|
|
- toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
- response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
|
- response.setContentType("application/octet-stream");
|
|
|
- response.setHeader("content-disposition", "attachment; filename=" + task.getZipUrl());
|
|
|
- toClient.write(buffer);
|
|
|
- toClient.flush();
|
|
|
- } catch (Exception e) {
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- PrintWriter writer = response.getWriter();
|
|
|
- writer.write(JSON.toJSONString(R.fail("下载zip压缩包过程发生异常,请联系管理员")));
|
|
|
- log.error("下载zip压缩包过程发生异常,{}", e.getLocalizedMessage());
|
|
|
- writer.flush();
|
|
|
- } finally {
|
|
|
- if (toClient != null) {
|
|
|
- try {
|
|
|
- toClient.close();
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("zip下载关流失败");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- PrintWriter writer = response.getWriter();
|
|
|
- writer.write(JSON.toJSONString(R.fail("下载任务进行中,请耐心等待")));
|
|
|
- writer.flush();
|
|
|
- }
|
|
|
- } else {
|
|
|
- DownloadTask task = new DownloadTask();
|
|
|
- task.setYearAndMonth(year);
|
|
|
- int result = downloadTaskMapper.insert(task);
|
|
|
- if (result == 1) {
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- PrintWriter writer = response.getWriter();
|
|
|
- JSONObject data = new JSONObject();
|
|
|
- data.put("taskId", task.getId());
|
|
|
- writer.write(JSON.toJSONString(R.data(data)));
|
|
|
- RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
|
|
- new Thread(() -> {
|
|
|
- RequestContextHolder.setRequestAttributes(requestAttributes);
|
|
|
- service.downloadCenterZipFiles(year, task.getId());
|
|
|
- }).start();
|
|
|
- writer.flush();
|
|
|
+ @Operation(summary = "档案中心-一键下载/一键导出高新备查/一键导出加计备查")
|
|
|
+ public void downloadCenterZipFiles(HttpServletResponse response, Long taskId) {
|
|
|
+ DownloadTask task = downloadTaskMapper.selectById(taskId);
|
|
|
+ if (task == null) {
|
|
|
+ response.setContentType("application/json;charset=UTF-8");
|
|
|
+ PrintWriter writer = response.getWriter();
|
|
|
+ writer.write(JSON.toJSONString(R.fail("下载任务不存在,请联系管理员")));
|
|
|
+ writer.flush();
|
|
|
+ } else if (task.getStatus() == 2) {
|
|
|
+ // 指定磁盘上已存在的ZIP文件路径
|
|
|
+ File zipFile = new File(task.getZipUrl());
|
|
|
+
|
|
|
+ // 检查文件是否存在
|
|
|
+ if (!zipFile.exists()) {
|
|
|
+ response.sendError(HttpStatus.NOT_FOUND.value(), "ZIP文件未找到");
|
|
|
+ return;
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- @GetMapping("center/high-tech/download")
|
|
|
- @ApiOperationSupport(order = 8)
|
|
|
- @Operation(summary = "档案中心-一键导出高新备查")
|
|
|
- public void downloadCenterHighTechZipFiles(HttpServletRequest request, HttpServletResponse response, String year) {
|
|
|
- service.downloadHighTechZipFiles(request, response, year);
|
|
|
- }
|
|
|
+ // 设置响应头
|
|
|
+ response.setContentType("application/zip");
|
|
|
+ response.setHeader("Content-Disposition",
|
|
|
+ "attachment; filename=\"" + zipFile.getName() + "\"");
|
|
|
+ response.setContentLengthLong(zipFile.length());
|
|
|
|
|
|
- @GetMapping("center/deduction/download")
|
|
|
- @ApiOperationSupport(order = 10)
|
|
|
- @Operation(summary = "档案中心-一键导出加计备查")
|
|
|
- public void downloadCenterDeductionZipFiles(HttpServletRequest request, HttpServletResponse response, String year) {
|
|
|
- service.downloadDeductionZipFiles(request, response, year);
|
|
|
- }
|
|
|
+ // 将文件写入响应输出流
|
|
|
+ try (InputStream fileInputStream = new FileInputStream(zipFile);
|
|
|
+ OutputStream responseOutputStream = response.getOutputStream()) {
|
|
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ int bytesRead;
|
|
|
+ while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
+ responseOutputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ responseOutputStream.flush();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|