fix:修复Bug【69876】
报告管理可以下载excel文件
This commit is contained in:
@@ -23,7 +23,7 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
@SpringBootTest
|
||||||
public class TestMessage {
|
public class TestMessage {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@@ -31,16 +31,18 @@ public class TestMessage {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMessagePublish() {
|
public void testMessagePublish() {
|
||||||
MessageEntity messageEntity = new MessageEntity();
|
for (int i = 0; i < 20; i++) {
|
||||||
messageEntity.setMessageTitle("测试批量发送标题")
|
MessageEntity messageEntity = new MessageEntity();
|
||||||
.setMessageType(MessageTypeEnum.SYSTEM_MESSAGE.getValue())
|
messageEntity.setMessageTitle("测试发送标题" + java.util.UUID.randomUUID())
|
||||||
.setId(UUID.randomUUID10())
|
.setMessageType(MessageTypeEnum.SYSTEM_MESSAGE.getValue())
|
||||||
.setContent("测试测试最后一次")
|
.setId(UUID.randomUUID10())
|
||||||
.setDelFlag(0)
|
.setContent("测试测试站内信内容" + java.util.UUID.randomUUID())
|
||||||
.setCreateUser("ZGGJP3N7FT")
|
.setDelFlag(0)
|
||||||
.setCreateDate(new Date());
|
.setCreateUser("ZGGJP3N7FT")
|
||||||
List<String> userIds = new ArrayList<>();
|
.setCreateDate(new Date());
|
||||||
userIds.add("ZGGJP3N7FT");
|
List<String> userIds = new ArrayList<>();
|
||||||
messageService.publishMessage(messageEntity, false, userIds);
|
userIds.add("ZGGJP3N7FT");
|
||||||
|
messageService.publishMessage(messageEntity, false, userIds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+167
-3
@@ -3,6 +3,7 @@ package com.adc.da.report.controller;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.adc.da.login.util.UserUtils;
|
import com.adc.da.login.util.UserUtils;
|
||||||
import com.adc.da.report.annotation.ReportLog;
|
import com.adc.da.report.annotation.ReportLog;
|
||||||
|
import com.adc.da.report.constant.ReportConstants;
|
||||||
import com.adc.da.report.dao.mysql.ReportDao;
|
import com.adc.da.report.dao.mysql.ReportDao;
|
||||||
import com.adc.da.report.dao.mysql.ReportLogBookDao;
|
import com.adc.da.report.dao.mysql.ReportLogBookDao;
|
||||||
import com.adc.da.report.eo.*;
|
import com.adc.da.report.eo.*;
|
||||||
@@ -253,6 +254,13 @@ public class ReportManageConroller {
|
|||||||
@ApiOperation("获取报告预览")
|
@ApiOperation("获取报告预览")
|
||||||
@GetMapping("/getReportHtml")
|
@GetMapping("/getReportHtml")
|
||||||
public ResponseMessage getReportHtml(@RequestParam String reportId, @RequestParam String fileId) {
|
public ResponseMessage getReportHtml(@RequestParam String reportId, @RequestParam String fileId) {
|
||||||
|
//判断要预览的是不是excel文件
|
||||||
|
FileEntity file = fileService.getFile(fileId);
|
||||||
|
String fileType = file.getFileType();
|
||||||
|
if (ReportConstants.FILE_EXTENSIONS_XLS.equalsIgnoreCase(fileType) || ReportConstants.FILE_EXTENSIONS_XLSX.equalsIgnoreCase(fileType)) {
|
||||||
|
return Result.error("无法预览excel文件");
|
||||||
|
}
|
||||||
|
|
||||||
String html = reportService.reportHtml(reportId, fileId);
|
String html = reportService.reportHtml(reportId, fileId);
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("content", html);
|
map.put("content", html);
|
||||||
@@ -411,12 +419,12 @@ public class ReportManageConroller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件(禁止下载Excel)
|
||||||
* @param response
|
* @param response
|
||||||
* @param fileIds
|
* @param fileIds
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@ApiOperation(value = "|下载文件")
|
@ApiOperation(value = "下载文件(禁止下载Excel)")
|
||||||
@GetMapping("/download")
|
@GetMapping("/download")
|
||||||
public void download(HttpServletResponse response, String[] fileIds, String reportId) throws IOException {
|
public void download(HttpServletResponse response, String[] fileIds, String reportId) throws IOException {
|
||||||
List<String> fileIdList = new ArrayList<>();
|
List<String> fileIdList = new ArrayList<>();
|
||||||
@@ -445,7 +453,7 @@ public class ReportManageConroller {
|
|||||||
//判断是从列表页面下载 还是预览页面下载
|
//判断是从列表页面下载 还是预览页面下载
|
||||||
if (CollectionUtils.isEmpty(fileIdList)) {
|
if (CollectionUtils.isEmpty(fileIdList)) {
|
||||||
//列表点击下载按钮
|
//列表点击下载按钮
|
||||||
downLoadFileIds = fileService.getDownloadFileIds(reportId);
|
downLoadFileIds = fileService.getDownloadFileIdsWithoutExcel(reportId);
|
||||||
if (CollectionUtils.isEmpty(downLoadFileIds)) {
|
if (CollectionUtils.isEmpty(downLoadFileIds)) {
|
||||||
log.info("无法下载,原因可能是无可下载文件(xls/xlsx文件无法下载)");
|
log.info("无法下载,原因可能是无可下载文件(xls/xlsx文件无法下载)");
|
||||||
try {
|
try {
|
||||||
@@ -571,6 +579,162 @@ public class ReportManageConroller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param response
|
||||||
|
* @param fileIds
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@ApiOperation(value = "下载文件")
|
||||||
|
@GetMapping("/downloadAll")
|
||||||
|
public void downloadAll(HttpServletResponse response, String[] fileIds, String reportId) throws IOException {
|
||||||
|
List<String> fileIdList = new ArrayList<>();
|
||||||
|
if (CollectionUtils.isNotEmpty(fileIds)) {
|
||||||
|
fileIdList = Arrays.asList(fileIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StrUtil.isBlank(reportId)) {
|
||||||
|
throw new AdcDaBaseException("缺少报告id");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StrUtil.isNotBlank(reportId)) {
|
||||||
|
//校验权限
|
||||||
|
if (!reportService.isAllowDownload(UserUtils.getUserId(), reportId)) {
|
||||||
|
throw new AdcDaBaseException("无此报告下载权限");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//待下载的文件id List
|
||||||
|
List<String> downLoadFileIds;
|
||||||
|
String reportName = reportDao.selectById(reportId).getName();
|
||||||
|
String folderName = reportName + ".zip";
|
||||||
|
// 获取编码后的文件名
|
||||||
|
String encodedZipName = URLEncoder.encode(folderName, "UTF-8");
|
||||||
|
|
||||||
|
//判断是从列表页面下载 还是预览页面下载
|
||||||
|
if (CollectionUtils.isEmpty(fileIdList)) {
|
||||||
|
//列表点击下载按钮
|
||||||
|
downLoadFileIds = fileService.getDownloadFileIds(reportId);
|
||||||
|
if (CollectionUtils.isEmpty(downLoadFileIds)) {
|
||||||
|
try {
|
||||||
|
// 设置响应头信息
|
||||||
|
response.setContentType("application/zip");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=" + encodedZipName);
|
||||||
|
response.setHeader("Content-Transfer-Encoding", "binary");
|
||||||
|
response.setHeader("Pragma", "public");
|
||||||
|
response.setHeader("Cache-Control", "no-store");
|
||||||
|
response.setHeader("Expires", "0");
|
||||||
|
|
||||||
|
// 创建空的ZipOutputStream
|
||||||
|
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
|
||||||
|
// 添加空文件夹条目
|
||||||
|
ZipEntry emptyDirectoryEntry = new ZipEntry(reportName + "/");
|
||||||
|
zipOutputStream.putNextEntry(emptyDirectoryEntry);
|
||||||
|
zipOutputStream.close();
|
||||||
|
return;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//预览点击下载按钮
|
||||||
|
downLoadFileIds = fileIdList;
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果只下载一个文件,则不压缩
|
||||||
|
if (downLoadFileIds.size() == 1) {
|
||||||
|
String fileId = downLoadFileIds.get(0);
|
||||||
|
try {
|
||||||
|
FileEntity entity = fileService.getFile(fileId);
|
||||||
|
String fileName = entity.getFileName();
|
||||||
|
// 获取编码后的文件名
|
||||||
|
String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
|
||||||
|
|
||||||
|
//PDF文件需要加水印
|
||||||
|
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
|
||||||
|
String path = uploadFile + entity.getSavePath();
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(path);
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
if ("hwobs".equals(uploadType)) {
|
||||||
|
obsBootUtil.getFileFromOBS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("alioss".equals(uploadType)) {
|
||||||
|
ossClientUtil.getFileFromOSS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("local".equals(uploadType)) {
|
||||||
|
localFileUtil.getFileFromLocal(entity.getSavePath(), outputStream);
|
||||||
|
}
|
||||||
|
//给文件添加水印
|
||||||
|
waterMarkUtil.addWatermarkToPdfAndWriteToResponse(new FileInputStream(path), response);
|
||||||
|
outputStream.close();
|
||||||
|
} else {
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs 本地:local
|
||||||
|
if ("hwobs".equals(uploadType)){
|
||||||
|
obsBootUtil.downFile(entity.getSavePath(), response);
|
||||||
|
}else if ("alioss".equals(uploadType)){
|
||||||
|
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
||||||
|
}else if ("local".equals(uploadType)){
|
||||||
|
localFileUtil.downFile(entity.getSavePath(), response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.info(ex.getMessage(),ex);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
List<String> filePaths = new ArrayList<>();
|
||||||
|
// 设置响应头信息
|
||||||
|
response.setContentType("application/zip");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=" + encodedZipName);
|
||||||
|
response.setHeader("Content-Transfer-Encoding", "binary");
|
||||||
|
response.setHeader("Pragma", "public");
|
||||||
|
response.setHeader("Cache-Control", "no-store");
|
||||||
|
response.setHeader("Expires", "0");
|
||||||
|
|
||||||
|
for (String fileId : downLoadFileIds) {
|
||||||
|
try {
|
||||||
|
FileEntity entity = fileService.getFile(fileId);
|
||||||
|
String fileName = entity.getFileName();
|
||||||
|
|
||||||
|
String path = uploadFile + entity.getSavePath();
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(path);
|
||||||
|
filePaths.add(path);
|
||||||
|
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
if ("hwobs".equals(uploadType)) {
|
||||||
|
obsBootUtil.getFileFromOBS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("alioss".equals(uploadType)) {
|
||||||
|
ossClientUtil.getFileFromOSS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("local".equals(uploadType)) {
|
||||||
|
localFileUtil.getFileFromLocal(entity.getSavePath(), outputStream);
|
||||||
|
}
|
||||||
|
//PDF文件需要加水印
|
||||||
|
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
|
||||||
|
waterMarkUtil.savePdfToFile(new FileInputStream(path), path);
|
||||||
|
}
|
||||||
|
outputStream.close();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.info(ex.getMessage(),ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//将文件压缩 写入Response
|
||||||
|
localFileUtil.compressFiles(filePaths, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(reportId)){
|
||||||
|
ReportLogBook reportLog = new ReportLogBook();
|
||||||
|
reportLog.setId(UUID.randomUUID().toString().replaceAll("-",""));
|
||||||
|
reportLog.setReportId(reportId);
|
||||||
|
reportLog.setType(2);
|
||||||
|
reportLog.setUpdateTime(new Date());
|
||||||
|
reportLog.setUserId(UserUtils.getUserId());
|
||||||
|
reportLogBookDao.insert(reportLog);
|
||||||
|
|
||||||
|
//报告日志管理
|
||||||
|
ReportEntity reportEntity = reportDao.selectById(reportId);
|
||||||
|
reportLog(reportEntity.getName(),"下载");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void reportLog(String reportName,String desc){
|
public void reportLog(String reportName,String desc){
|
||||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
LogEntity entity = new LogEntity();
|
LogEntity entity = new LogEntity();
|
||||||
|
|||||||
@@ -10,5 +10,8 @@ import java.util.List;
|
|||||||
* @Date 2021/6/28 15:19
|
* @Date 2021/6/28 15:19
|
||||||
*/
|
*/
|
||||||
public interface FileDao extends BaseMapper<FileEntity> {
|
public interface FileDao extends BaseMapper<FileEntity> {
|
||||||
|
|
||||||
|
List<String> getDownloadFileIdsWithoutExcel(String reportId);
|
||||||
|
|
||||||
List<String> getDownloadFileIds(String reportId);
|
List<String> getDownloadFileIds(String reportId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ public interface IFileService {
|
|||||||
*/
|
*/
|
||||||
boolean hasExcelFileId(List<String> fileIds);
|
boolean hasExcelFileId(List<String> fileIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据报告id返回所有可以下载的文件id(不要excel的)
|
||||||
|
*/
|
||||||
|
List<String> getDownloadFileIdsWithoutExcel(String reportId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据报告id返回所有可以下载的文件id
|
* 根据报告id返回所有可以下载的文件id
|
||||||
* @param reportId
|
* @param reportId
|
||||||
|
|||||||
@@ -197,6 +197,11 @@ public class IFileServiceImpl implements IFileService {
|
|||||||
return hasExcelFileId;
|
return hasExcelFileId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getDownloadFileIdsWithoutExcel(String reportId) {
|
||||||
|
return fileDao.getDownloadFileIdsWithoutExcel(reportId);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getDownloadFileIds(String reportId) {
|
public List<String> getDownloadFileIds(String reportId) {
|
||||||
return fileDao.getDownloadFileIds(reportId);
|
return fileDao.getDownloadFileIds(reportId);
|
||||||
|
|||||||
@@ -408,6 +408,7 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
|
|||||||
LambdaQueryWrapper<ReportContentEntity> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<ReportContentEntity> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.in(ReportContentEntity::getReportId, idList);
|
wrapper.in(ReportContentEntity::getReportId, idList);
|
||||||
iReportContentService.remove(wrapper);
|
iReportContentService.remove(wrapper);
|
||||||
|
//TODO 删除文件关联
|
||||||
return nameList;
|
return nameList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="com.adc.da.report.dao.mysql.FileDao">
|
<mapper namespace="com.adc.da.report.dao.mysql.FileDao">
|
||||||
|
|
||||||
<select id="getDownloadFileIds" resultType="string">
|
<select id="getDownloadFileIdsWithoutExcel" resultType="string">
|
||||||
SELECT
|
SELECT
|
||||||
f.FILE_ID
|
f.FILE_ID
|
||||||
FROM
|
FROM
|
||||||
@@ -17,4 +17,14 @@
|
|||||||
AND
|
AND
|
||||||
f.FILE_TYPE != "xlsx"
|
f.FILE_TYPE != "xlsx"
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getDownloadFileIds" resultType="string">
|
||||||
|
SELECT
|
||||||
|
f.FILE_ID
|
||||||
|
FROM
|
||||||
|
`ts_file` AS f
|
||||||
|
LEFT JOIN ts_report_file AS rf ON f.FILE_ID = rf.file_id
|
||||||
|
WHERE
|
||||||
|
rf.report_id = #{reportId}
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user