feat: 使用zip打包下载生成内容
This commit is contained in:
parent
dfdf9546b0
commit
ea18ded70c
|
@ -8,6 +8,7 @@ import cn.bunny.service.VmsService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -31,10 +32,16 @@ public class VmsController {
|
||||||
return Result.success(list);
|
return Result.success(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "生成控制器", description = "生成控制器代码")
|
@Operation(summary = "生成代码", description = "生成代码")
|
||||||
@PostMapping("generator")
|
@PostMapping("generator")
|
||||||
public Result<List<GeneratorVo>> generator(@Valid @RequestBody VmsArgumentDto dto) {
|
public Result<List<GeneratorVo>> generator(@Valid @RequestBody VmsArgumentDto dto) {
|
||||||
List<GeneratorVo> list = vmsService.generator(dto);
|
List<GeneratorVo> list = vmsService.generator(dto);
|
||||||
return Result.success(list);
|
return Result.success(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "打包成zip下载", description = "打包成zip下载")
|
||||||
|
@PostMapping("downloadByZip")
|
||||||
|
public ResponseEntity<byte[]> downloadByZip(@Valid @RequestBody VmsArgumentDto dto) {
|
||||||
|
return vmsService.downloadByZip(dto);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package cn.bunny.service;
|
||||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||||
import cn.bunny.dao.vo.GeneratorVo;
|
import cn.bunny.dao.vo.GeneratorVo;
|
||||||
import cn.bunny.dao.vo.VmsPathVo;
|
import cn.bunny.dao.vo.VmsPathVo;
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -22,4 +24,12 @@ public interface VmsService {
|
||||||
* @return vms下的文件路径
|
* @return vms下的文件路径
|
||||||
*/
|
*/
|
||||||
Map<String, List<VmsPathVo>> getVmsPathList();
|
Map<String, List<VmsPathVo>> getVmsPathList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打包成zip下载
|
||||||
|
*
|
||||||
|
* @param dto VmsArgumentDto
|
||||||
|
* @return zip 文件
|
||||||
|
*/
|
||||||
|
ResponseEntity<byte[]> downloadByZip(@Valid VmsArgumentDto dto);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,24 @@ import cn.bunny.service.TableService;
|
||||||
import cn.bunny.service.VmsService;
|
import cn.bunny.service.VmsService;
|
||||||
import cn.bunny.utils.ResourceFileUtil;
|
import cn.bunny.utils.ResourceFileUtil;
|
||||||
import cn.bunny.utils.VmsUtil;
|
import cn.bunny.utils.VmsUtil;
|
||||||
|
import cn.hutool.crypto.digest.MD5;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.apache.velocity.VelocityContext;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
|
||||||
|
@ -91,6 +101,60 @@ public class VmsServiceImpl implements VmsService {
|
||||||
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
|
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
|
||||||
|
|
||||||
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
|
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
|
||||||
}).collect(Collectors.groupingBy(VmsPathVo::getType));
|
})
|
||||||
|
.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue