下载记录文件

master
wj 2 years ago
parent 37af4d2462
commit c0aa3f4107

@ -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);
}
}
}
}
}

@ -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<BasicDownloadDao, BasicDownloadStatusEntity, BasicDownloadStatusEntity> {
List<BasicDownloadStatusEntity> filterDownloadStatus(BasicDownloadRequest basicDownloadRequest);

@ -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> {
BasicDownloadStatusEntity findByData(String idDatas, Integer status);

@ -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<BasicDownloadDao,BasicDownloadStatusEntity> implements BasicDownloadService {
@Resource
BasicDownloadDao basicDownloadDao;

Loading…
Cancel
Save