前端png文件下载改base64格式
This commit is contained in:
parent
4fbd5f7dcb
commit
eb580beee0
@ -201,4 +201,5 @@ public interface ErrorCodeConstants {
|
|||||||
ErrorCode AGV_FILE_UPLOAD_CONTENT_IS_EMPTY = new ErrorCode(1_002_038_002, "AGV文件上传内容为空");
|
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 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_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失败");
|
||||||
}
|
}
|
||||||
|
@ -81,11 +81,12 @@ public class PositionMapController {
|
|||||||
return CommonResult.success(true);
|
return CommonResult.success(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/downloadPng")
|
@GetMapping("/downloadPngBase64")
|
||||||
@Operation(summary = "下载png文件")
|
@Operation(summary = "下载png文件")
|
||||||
@PermitAll
|
@PermitAll
|
||||||
public void downloadPng(@RequestParam Integer floor, @RequestParam String area, HttpServletResponse response) throws IOException {
|
public CommonResult<String> downloadPngBase64(@RequestParam Integer floor, @RequestParam String area) {
|
||||||
positionMapService.downloadPng(floor, area, response);
|
String base64 = positionMapService.downloadPngBase64(floor, area);
|
||||||
|
return CommonResult.success(base64);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/download")
|
@GetMapping("/download")
|
||||||
|
@ -85,7 +85,6 @@ public interface PositionMapService extends IService<PositionMapDO> {
|
|||||||
*
|
*
|
||||||
* @param floor
|
* @param floor
|
||||||
* @param area
|
* @param area
|
||||||
* @param response
|
|
||||||
*/
|
*/
|
||||||
void downloadPng(Integer floor, String area, HttpServletResponse response) throws IOException;
|
String downloadPngBase64(Integer floor, String area);
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -28,6 +29,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
@ -47,6 +49,8 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
|
|||||||
private PositionMapMapper positionMapMapper;
|
private PositionMapMapper positionMapMapper;
|
||||||
@Value("${map.file.upload-path}")
|
@Value("${map.file.upload-path}")
|
||||||
private String UPLOAD_DIR;
|
private String UPLOAD_DIR;
|
||||||
|
@Override
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long createPositionMap(PositionMapSaveReqVO createReqVO) {
|
public Long createPositionMap(PositionMapSaveReqVO createReqVO) {
|
||||||
@ -253,13 +257,17 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@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文件路径
|
// 根据楼层和区域查找对应的PNG文件路径
|
||||||
String basePath = UPLOAD_DIR + floor + "/" + area + "/";
|
String basePath = UPLOAD_DIR + floor + "/" + area + "/";
|
||||||
File directory = new File(basePath);
|
File directory = new File(basePath);
|
||||||
if (!directory.exists() || !directory.isDirectory()) {
|
if (!directory.exists() || !directory.isDirectory()) {
|
||||||
response.sendError(HttpStatus.NOT_FOUND.value(), "Directory not found for the given floor and area.");
|
throw exception(AGV_MAP_NOT_FOUND);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找PNG文件
|
// 查找PNG文件
|
||||||
@ -270,18 +278,21 @@ public class PositionMapServiceImpl extends ServiceImpl<PositionMapMapper, Posit
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pngFile == null || !pngFile.exists()) {
|
if (pngFile == null || !pngFile.exists()) {
|
||||||
response.sendError(HttpStatus.NOT_FOUND.value(), "PNG file not found for the given floor and area.");
|
throw exception(AGV_MAP_NOT_FOUND);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
String encodedString = null;
|
||||||
// 设置响应头以指示浏览器下载文件
|
try {
|
||||||
response.setContentType("image/png");
|
// 读取文件内容
|
||||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + pngFile.getName() + "\"");
|
byte[] fileContent = Files.readAllBytes(pngFile.toPath());
|
||||||
|
// 将文件内容转换为Base64字符串
|
||||||
// 将PNG文件写入HTTP响应输出流
|
encodedString = Base64.getEncoder().encodeToString(fileContent);
|
||||||
try (FileInputStream fis = new FileInputStream(pngFile)) {
|
} catch (IOException e) {
|
||||||
IOUtils.copy(fis, response.getOutputStream());
|
e.printStackTrace();
|
||||||
|
throw exception(AGV_IMAGE_CONVERSION_TO_BASE64_FAILED);
|
||||||
}
|
}
|
||||||
|
// -- 缓存30分钟
|
||||||
|
stringRedisTemplate.opsForValue().set(key, encodedString, 30, TimeUnit.MINUTES);
|
||||||
|
return encodedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user