前端png文件下载改base64格式

This commit is contained in:
aikai 2025-01-13 14:54:35 +08:00
parent 4fbd5f7dcb
commit eb580beee0
4 changed files with 30 additions and 18 deletions

View File

@ -201,4 +201,5 @@ public interface ErrorCodeConstants {
ErrorCode AGV_FILE_UPLOAD_CONTENT_IS_EMPTY = new ErrorCode(1_002_038_002, "AGV文件上传内容为空");
ErrorCode PLEASE_UPLOAD_PNG_AND_YAML_FILES = new ErrorCode(1_002_038_003, "请上传png和yaml两个文件并且文件内容不为空");
ErrorCode AGV_MAP_NOT_FOUND = new ErrorCode(1_002_038_004, "找不到AGV地图信息");
ErrorCode AGV_IMAGE_CONVERSION_TO_BASE64_FAILED = new ErrorCode(1_002_038_005, "AGV图片转base64失败");
}

View File

@ -81,11 +81,12 @@ public class PositionMapController {
return CommonResult.success(true);
}
@GetMapping("/downloadPng")
@GetMapping("/downloadPngBase64")
@Operation(summary = "下载png文件")
@PermitAll
public void downloadPng(@RequestParam Integer floor, @RequestParam String area, HttpServletResponse response) throws IOException {
positionMapService.downloadPng(floor, area, response);
public CommonResult<String> downloadPngBase64(@RequestParam Integer floor, @RequestParam String area) {
String base64 = positionMapService.downloadPngBase64(floor, area);
return CommonResult.success(base64);
}
@GetMapping("/download")

View File

@ -85,7 +85,6 @@ public interface PositionMapService extends IService<PositionMapDO> {
*
* @param floor
* @param area
* @param response
*/
void downloadPng(Integer floor, String area, HttpServletResponse response) throws IOException;
String downloadPngBase64(Integer floor, String area);
}

View File

@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -28,6 +29,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -47,6 +49,8 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
private PositionMapMapper positionMapMapper;
@Value("${map.file.upload-path}")
private String UPLOAD_DIR;
@Override
private StringRedisTemplate stringRedisTemplate;
@Override
public Long createPositionMap(PositionMapSaveReqVO createReqVO) {
@ -253,13 +257,17 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
@Override
public void downloadPng(Integer floor, String area, HttpServletResponse response) throws IOException {
public String downloadPngBase64(Integer floor, String area) {
String key = "pngBase64" + "_" + floor + "_" + area;
Boolean b = stringRedisTemplate.hasKey(key);
if (b) {
return stringRedisTemplate.opsForValue().get(key);
}
// 根据楼层和区域查找对应的PNG文件路径
String basePath = UPLOAD_DIR + floor + "/" + area + "/";
File directory = new File(basePath);
if (!directory.exists() || !directory.isDirectory()) {
response.sendError(HttpStatus.NOT_FOUND.value(), "Directory not found for the given floor and area.");
return;
throw exception(AGV_MAP_NOT_FOUND);
}
// 查找PNG文件
@ -270,18 +278,21 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
}
if (pngFile == null || !pngFile.exists()) {
response.sendError(HttpStatus.NOT_FOUND.value(), "PNG file not found for the given floor and area.");
return;
throw exception(AGV_MAP_NOT_FOUND);
}
// 设置响应头以指示浏览器下载文件
response.setContentType("image/png");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + pngFile.getName() + "\"");
// 将PNG文件写入HTTP响应输出流
try (FileInputStream fis = new FileInputStream(pngFile)) {
IOUtils.copy(fis, response.getOutputStream());
String encodedString = null;
try {
// 读取文件内容
byte[] fileContent = Files.readAllBytes(pngFile.toPath());
// 将文件内容转换为Base64字符串
encodedString = Base64.getEncoder().encodeToString(fileContent);
} catch (IOException e) {
e.printStackTrace();
throw exception(AGV_IMAGE_CONVERSION_TO_BASE64_FAILED);
}
// -- 缓存30分钟
stringRedisTemplate.opsForValue().set(key, encodedString, 30, TimeUnit.MINUTES);
return encodedString;
}
}