调通了上传接口,新增下载文件接口。
This commit is contained in:
parent
effa0d9f4d
commit
3722dfb4f0
|
|
@ -66,4 +66,7 @@ public class ImportFileInfo extends BaseModel {
|
|||
@TableField(value = "permanent_deleted")
|
||||
private Boolean permanentDeleted;
|
||||
|
||||
@TableField(value = "src_path")
|
||||
private String srcPath;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,15 @@
|
|||
package com.electromagnetic.industry.software.manage.service;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface ImportPrjService {
|
||||
|
||||
boolean importPrj(MultipartFile file);
|
||||
|
||||
ResponseEntity<InputStreamResource> download(String id, HttpServletResponse response);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package com.electromagnetic.industry.software.manage.service.serviceimpl;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
|
@ -12,12 +15,20 @@ import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum;
|
|||
import com.electromagnetic.industry.software.common.exception.BizException;
|
||||
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
|
||||
import com.electromagnetic.industry.software.common.util.IdWorker;
|
||||
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
|
||||
import com.electromagnetic.industry.software.manage.mapper.ImportPrjInfoMapper;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.EdFileInfo;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.ImportFileInfo;
|
||||
import com.electromagnetic.industry.software.manage.service.ImportPrjService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.mockito.internal.util.io.IOUtil;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
|
@ -36,13 +47,14 @@ public class ImportPrjServiceImpl extends ServiceImpl<ImportPrjInfoMapper, Impor
|
|||
|
||||
@Resource
|
||||
private ElePropertyConfig elePropertyConfig;
|
||||
@Resource
|
||||
private CommonService commonService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean importPrj(MultipartFile file) {
|
||||
String zipTmpPath = elePropertyConfig.getEleTmpPath() + File.pathSeparator + UUID.randomUUID() + ".zip";
|
||||
ZipFile zipFile = null;
|
||||
// zipTmpPath = "D:\\tmp\\szsd\\data\\ele.zip";
|
||||
try {
|
||||
FileUtil.writeFromStream(file.getInputStream(), zipTmpPath);
|
||||
Map<String, String> pathIdMap = new HashMap<>();
|
||||
|
|
@ -82,6 +94,7 @@ public class ImportPrjServiceImpl extends ServiceImpl<ImportPrjInfoMapper, Impor
|
|||
.setFileContent("")
|
||||
.setDataType(isDirectory ? EleDataTypeEnum.FOLDER.code : EleDataTypeEnum.FILE.code)
|
||||
.setFileSize(entry.getSize())
|
||||
.setSrcPath(entryName)
|
||||
.setPermanentDeleted(false);
|
||||
resetPrjName(importFileInfo);
|
||||
if (isDirectory) {
|
||||
|
|
@ -102,9 +115,9 @@ public class ImportPrjServiceImpl extends ServiceImpl<ImportPrjInfoMapper, Impor
|
|||
String newId = entry.getKey();
|
||||
ZipEntry tmp = entry.getValue();
|
||||
InputStream inputStream = zipFile.getInputStream(tmp);
|
||||
String destPath = elePropertyConfig.getEleTmpPath() + File.pathSeparator + newId;
|
||||
String destPath = elePropertyConfig.getImportPrjPath() + File.separator + newId;
|
||||
FileUtil.writeFromStream(inputStream, destPath);
|
||||
EleCommonUtil.decryptFile(destPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
|
||||
EleCommonUtil.encryptFile(destPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new BizException("导入工程失败,原因 " + e.getMessage(), e);
|
||||
|
|
@ -115,6 +128,37 @@ public class ImportPrjServiceImpl extends ServiceImpl<ImportPrjInfoMapper, Impor
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<InputStreamResource> download(String id, HttpServletResponse response) {
|
||||
String fileName = "";
|
||||
ImportFileInfo fileInfo = this.baseMapper.selectById(id);
|
||||
String fileSysPath = elePropertyConfig.getImportPrjPath() + File.pathSeparator + id;
|
||||
String dbPath = fileInfo.getSrcPath();
|
||||
try {
|
||||
Assert.isTrue(FileUtil.exist(fileSysPath), "下载文件不存在,路径为 {}", dbPath);
|
||||
FileSystemResource fileSystemResource = new FileSystemResource(fileSysPath);
|
||||
fileName = fileSystemResource.getFilename();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
String newFileName = Base64.encode(fileInfo.getFileName() + "." + fileInfo.getFileType());
|
||||
response.setHeader("content-disposition", "attachment;filename=" + newFileName);
|
||||
UserThreadLocal.setSuccessInfo(fileInfo.getParentId(), id, "下载文件 {} 成功,文件路径 {}", fileInfo.getFileName() + "." + fileInfo.getFileType(), dbPath);
|
||||
// 构建响应实体(可以返回<byte[]或Resource,返回类型取决body入参类型)
|
||||
return ResponseEntity
|
||||
.ok()
|
||||
.headers(headers)
|
||||
.contentLength(fileSystemResource.contentLength())
|
||||
.contentType(MediaType.parseMediaType("application/octet-stream;charset=UTF-8"))
|
||||
.body(new InputStreamResource(fileSystemResource.getInputStream()));
|
||||
} catch (Exception e) {
|
||||
String info = StrFormatter.format("下载文件异常 {},路径 {}", fileName, dbPath);
|
||||
log.error(info, e);
|
||||
throw new BizException(info);
|
||||
}
|
||||
}
|
||||
|
||||
public void resetPrjName(ImportFileInfo importFileInfo) {
|
||||
if (!StrUtil.equals(importFileInfo.getParentId(), ElectromagneticConstants.PRJ_PARENT_ID)) {
|
||||
return;
|
||||
|
|
|
|||
Loading…
Reference in New Issue