master
杜函宇 5 months ago
parent b074ca703a
commit 9773f9680b

@ -2,14 +2,17 @@ package com.ruoyi.jjh.declaration.controller;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil; import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpUtil;
import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.jjh.declaration.entity.BmsDeclarationRecords; import com.ruoyi.jjh.declaration.entity.BmsDeclarationRecords;
import com.ruoyi.jjh.declaration.entity.dto.ApprovalDeclarationRecordsQueryDto; import com.ruoyi.jjh.declaration.entity.dto.ApprovalDeclarationRecordsQueryDto;
import com.ruoyi.jjh.declaration.entity.dto.BmsApprovalInfoUpdateDto; import com.ruoyi.jjh.declaration.entity.dto.BmsApprovalInfoUpdateDto;
@ -20,26 +23,21 @@ import com.ruoyi.jjh.declaration.mapper.BmsDeclarationRecordsMapper;
import com.ruoyi.jjh.declaration.service.IBmsDeclarationRecordsService; import com.ruoyi.jjh.declaration.service.IBmsDeclarationRecordsService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileInputStream;
import java.io.UnsupportedEncodingException; import java.io.IOException;
import java.net.URLEncoder; import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List; import java.util.List;
/** /**
@ -58,15 +56,17 @@ public class BmsDeclarationRecordsController extends BaseController {
@Value("${ruoyi.profile}") @Value("${ruoyi.profile}")
private String fileAddress; private String fileAddress;
@Value("${server.port}")
private String port;
@Value("${server.servlet.context-path}") @Value("${server.servlet.context-path}")
private String path; private String path;
@Resource @Resource
private BmsDeclarationRecordsMapper bmsDeclarationRecordsMapper; private BmsDeclarationRecordsMapper bmsDeclarationRecordsMapper;
@Autowired
private ServerConfig serverConfig;
/** /**
* *
*/ */
@ -118,43 +118,47 @@ public class BmsDeclarationRecordsController extends BaseController {
// @RequiresPermissions("system:declarationRecords:list") // @RequiresPermissions("system:declarationRecords:list")
@GetMapping("/fileList") @GetMapping("/fileList")
@ApiOperation("根据列表id集合压缩所有文件") @ApiOperation("根据列表id集合压缩所有文件")
public ResponseEntity<byte[]> fileList(@RequestParam(name = "list") List<Long> list) throws FileNotFoundException, UnsupportedEncodingException { public void fileList(@RequestParam(name = "list") List<Long> list, HttpServletResponse response) throws IOException {
List<BmsDeclarationRecordsQueryVo> rd = bmsDeclarationRecordsMapper.selectDeclarationRecordsToIdList(list); List<BmsDeclarationRecordsQueryVo> rd = bmsDeclarationRecordsMapper.selectDeclarationRecordsToIdList(list);
// 创建父文件目录 String fatherFile = fileAddress + "/项目压缩文件";
String fatherFile = fileAddress + "/" + "项目压缩文件";
FileUtil.mkdir(fatherFile); FileUtil.mkdir(fatherFile);
for (BmsDeclarationRecordsQueryVo item : rd) { for (BmsDeclarationRecordsQueryVo item : rd) {
String itemsFileName = fatherFile + "/" +item.getEnterpriseName()+ item.getProjectName();
FileUtil.mkdir(itemsFileName);
List<String> splitList = bmsDeclarationRecordsService.getAllFile(item.getTemplateJson()); List<String> splitList = bmsDeclarationRecordsService.getAllFile(item.getTemplateJson());
for (String s : splitList) { for (String s : splitList) {
// 下载文件
try { try {
HttpUtil.downloadFile("http://localhost:" + port + s, itemsFileName); String urls = RuoYiConfig.getProfile() + StringUtils.substringAfter(s, Constants.RESOURCE_PREFIX);
Path sourcePath = Paths.get(urls);
String itemsFileName = fatherFile + "/" + item.getEnterpriseName() + item.getProjectName() + "/" + sourcePath.getFileName();
FileUtil.touch(itemsFileName);
// 目标文件路径
Path targetPath = Paths.get(itemsFileName);
try {
Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件成功复制到新的位置!");
} catch (IOException e) {
throw new RuntimeException("暂无该文件!");
}
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(itemsFileName + "某文件下载失败"); throw new RuntimeException("文件下载失败");
} }
} }
} }
// 压缩后的文件路径 // 压缩后的文件路径
String zipFilePath = fatherFile + ".zip"; String zipFilePath = fatherFile + ".zip";
// 使用Hutool压缩文件夹
ZipUtil.zip(fatherFile, zipFilePath); ZipUtil.zip(fatherFile, zipFilePath);
// 从本地读取压缩文件 File file = new File(zipFilePath);// 创建File对象
File zipFile = FileUtil.file(zipFilePath); response.setContentType("application/zip");// 设置内容类型为ZIP文件
// FileSystemResource resource = new FileSystemResource(zipFile); response.setHeader("Content-Disposition", "attachment; filename=\"file.zip\"");//设置文件名
// 读取本地压缩文件并转为字节数组 try (FileInputStream fileInputStream = new FileInputStream(file);
byte[] bytes = FileUtil.readBytes(zipFile); OutputStream outputStream = response.getOutputStream()) {
// 设置响应头 byte[] buffer = new byte[4096];
HttpHeaders headers = new HttpHeaders(); int by;
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); while ((by = fileInputStream.read(buffer)) != -1) {
headers.setContentDispositionFormData("attachment", URLEncoder.encode("项目压缩文件.zip", "UTF-8")); outputStream.write(buffer, 0, by);
// 删除压缩之前的文件夹 }
}
FileUtil.del(zipFilePath); FileUtil.del(zipFilePath);
// 删除压缩包
FileUtil.del(fatherFile); FileUtil.del(fatherFile);
// 创建响应实体
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
} }
/** /**

@ -113,7 +113,7 @@
FROM FROM
bms_declaration_records AS a bms_declaration_records AS a
inner join bms_template_record f on a.template_record_id = f.id inner join bms_template_record f on a.template_record_id = f.id
LEFT JOIN bms_enterprise_basic_info b ON a.enterprise_id = b.id LEFT JOIN bms_enterprise_basic_info b ON a.credit_code = b.tyshxydm
left join sys_dict_data e on a.status = e.dict_value and e.dict_type='bms_approval_status' left join sys_dict_data e on a.status = e.dict_value and e.dict_type='bms_approval_status'
<where> <where>
<if test="list != null and list.size>0"> <if test="list != null and list.size>0">

Loading…
Cancel
Save