feat: 使用zip打包下载生成内容

This commit is contained in:
bunny 2025-04-09 22:21:28 +08:00
parent dfdf9546b0
commit ea18ded70c
4 changed files with 100 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import cn.bunny.service.VmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -31,10 +32,16 @@ public class VmsController {
return Result.success(list);
}
@Operation(summary = "生成控制器", description = "生成控制器代码")
@Operation(summary = "生成代码", description = "生成代码")
@PostMapping("generator")
public Result<List<GeneratorVo>> generator(@Valid @RequestBody VmsArgumentDto dto) {
List<GeneratorVo> list = vmsService.generator(dto);
return Result.success(list);
}
@Operation(summary = "打包成zip下载", description = "打包成zip下载")
@PostMapping("downloadByZip")
public ResponseEntity<byte[]> downloadByZip(@Valid @RequestBody VmsArgumentDto dto) {
return vmsService.downloadByZip(dto);
}
}

View File

@ -3,6 +3,8 @@ package cn.bunny.service;
import cn.bunny.dao.dto.VmsArgumentDto;
import cn.bunny.dao.vo.GeneratorVo;
import cn.bunny.dao.vo.VmsPathVo;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import java.util.List;
import java.util.Map;
@ -22,4 +24,12 @@ public interface VmsService {
* @return vms下的文件路径
*/
Map<String, List<VmsPathVo>> getVmsPathList();
/**
* 打包成zip下载
*
* @param dto VmsArgumentDto
* @return zip 文件
*/
ResponseEntity<byte[]> downloadByZip(@Valid VmsArgumentDto dto);
}

View File

@ -9,14 +9,24 @@ import cn.bunny.service.TableService;
import cn.bunny.service.VmsService;
import cn.bunny.utils.ResourceFileUtil;
import cn.bunny.utils.VmsUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.SneakyThrows;
import org.apache.velocity.VelocityContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service
@ -87,10 +97,64 @@ public class VmsServiceImpl implements VmsService {
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms");
return vmsRelativeFiles.stream().map(vmFile -> {
String[] filepathList = vmFile.split("/");
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
String[] filepathList = vmFile.split("/");
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
}).collect(Collectors.groupingBy(VmsPathVo::getType));
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
})
.collect(Collectors.groupingBy(VmsPathVo::getType));
}
/**
* 打包成zip下载
*
* @param dto VmsArgumentDto
* @return zip 文件
*/
@Override
public ResponseEntity<byte[]> downloadByZip(VmsArgumentDto dto) {
// 需要下载的数据
List<GeneratorVo> generatorVoList = generator(dto);
// 1. 创建临时ZIP文件
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
// 2. 遍历并创建
generatorVoList.forEach(generatorVo -> {
// zip中的路径
String path = generatorVo.getPath().replace(".vm", "");
// zip中的文件
String code = generatorVo.getCode();
ZipEntry zipEntry = new ZipEntry(path);
try {
// 如果有 / 会转成文件夹
zipOutputStream.putNextEntry(zipEntry);
// 写入文件
zipOutputStream.write(code.getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
// 2.1 文件不重名
long currentTimeMillis = System.currentTimeMillis();
String digestHex = MD5.create().digestHex(currentTimeMillis + "");
// 3. 准备响应
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=" + "vms-" + digestHex + ".zip");
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK);
}
}

View File

@ -0,0 +1,14 @@
package cn.bunny;
import cn.hutool.crypto.digest.MD5;
import org.junit.jupiter.api.Test;
public class TimeTest {
@Test
void timeTest() {
long currentTimeMillis = System.currentTimeMillis();
String digestHex = MD5.create().digestHex(currentTimeMillis + "");
System.out.println(currentTimeMillis);
System.out.println(digestHex);
}
}