diff --git a/src/main/java/com/glxp/api/controller/sync/SpsSyncDownloadController.java b/src/main/java/com/glxp/api/controller/sync/SpsSyncDownloadController.java index 3ad4649ec..1e8c1762d 100644 --- a/src/main/java/com/glxp/api/controller/sync/SpsSyncDownloadController.java +++ b/src/main/java/com/glxp/api/controller/sync/SpsSyncDownloadController.java @@ -1,5 +1,6 @@ package com.glxp.api.controller.sync; +import cn.hutool.core.util.StrUtil; import com.github.pagehelper.PageInfo; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; @@ -18,6 +19,13 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.List; @@ -80,6 +88,62 @@ public class SpsSyncDownloadController { } - + @GetMapping("/sps/sync/download/info/file") + public void downloadFile(HttpServletResponse response, String id){ + if (StrUtil.isBlank(id)) { + throw new RuntimeException("缺少唯一标识"); + } + BasicDownloadStatusEntity info = basicDownloadService.getById(id); + if (info == null) { + throw new RuntimeException("数据不存在"); + } + if (StrUtil.isBlank(info.getCacheFilePath())) { + throw new RuntimeException("文件未生成"); + } + File file = new File(info.getCacheFilePath()); + if (!file.exists()) { + throw new RuntimeException("文件丢失"); + } + String[] split = info.getCacheFilePath().split("/"); + String enFileName = URLEncoder.encode(split[split.length - 1], StandardCharsets.UTF_8); + // 设值返回文件属性,浏览器会根据属性调用下载文件方法 + response.addHeader("Content-Disposition", "attachment;filename=" + enFileName); + // 前端获取文件名,需要解码 + response.addHeader("downLoadName", enFileName); + // 定义输出流 + ServletOutputStream outputStream = null; + FileInputStream fileInputStream = null; + try { + outputStream = response.getOutputStream(); + // 定义输出类型为二进制流输出 + response.setContentType("application/octet-stream"); + fileInputStream = new FileInputStream(file); + byte[] bytes = new byte[fileInputStream.available()]; + fileInputStream.read(bytes); + // 把流写入response + outputStream.write(bytes); + // flush落盘 + outputStream.flush(); + // 关闭输出流 + outputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + if (fileInputStream != null) { + try { + fileInputStream.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + } } diff --git a/src/main/java/com/glxp/api/dao/sync/BasicDownloadDao.java b/src/main/java/com/glxp/api/dao/sync/BasicDownloadDao.java index 92d27bd61..183dfee8c 100644 --- a/src/main/java/com/glxp/api/dao/sync/BasicDownloadDao.java +++ b/src/main/java/com/glxp/api/dao/sync/BasicDownloadDao.java @@ -1,5 +1,6 @@ package com.glxp.api.dao.sync; +import com.glxp.api.dao.BaseMapperPlus; import com.glxp.api.entity.sync.BasicDownloadStatusEntity; import com.glxp.api.req.sync.BasicDownloadRequest; import org.apache.ibatis.annotations.Mapper; @@ -8,7 +9,7 @@ import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper -public interface BasicDownloadDao { +public interface BasicDownloadDao extends BaseMapperPlus { List filterDownloadStatus(BasicDownloadRequest basicDownloadRequest); diff --git a/src/main/java/com/glxp/api/service/sync/BasicDownloadService.java b/src/main/java/com/glxp/api/service/sync/BasicDownloadService.java index c47516bb1..331d4741c 100644 --- a/src/main/java/com/glxp/api/service/sync/BasicDownloadService.java +++ b/src/main/java/com/glxp/api/service/sync/BasicDownloadService.java @@ -1,12 +1,13 @@ package com.glxp.api.service.sync; +import com.baomidou.mybatisplus.extension.service.IService; import com.glxp.api.entity.sync.BasicDownloadStatusEntity; import com.glxp.api.req.sync.BasicDownloadRequest; import java.util.List; -public interface BasicDownloadService { +public interface BasicDownloadService extends IService { BasicDownloadStatusEntity findByData(String idDatas, Integer status); diff --git a/src/main/java/com/glxp/api/service/sync/impl/BasicDownloadServiceImpl.java b/src/main/java/com/glxp/api/service/sync/impl/BasicDownloadServiceImpl.java index b3bfeb73b..26491783e 100644 --- a/src/main/java/com/glxp/api/service/sync/impl/BasicDownloadServiceImpl.java +++ b/src/main/java/com/glxp/api/service/sync/impl/BasicDownloadServiceImpl.java @@ -1,6 +1,7 @@ package com.glxp.api.service.sync.impl; import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.glxp.api.dao.sync.BasicDownloadDao; import com.glxp.api.entity.sync.BasicDownloadStatusEntity; @@ -15,7 +16,7 @@ import java.util.List; @Service @Transactional(rollbackFor = Exception.class) -public class BasicDownloadServiceImpl implements BasicDownloadService { +public class BasicDownloadServiceImpl extends ServiceImpl implements BasicDownloadService { @Resource BasicDownloadDao basicDownloadDao;