修改了资源路径1

This commit is contained in:
wucongxing8150 2025-08-01 11:02:30 +08:00
parent 5c6718b905
commit a8c9f2126c
2 changed files with 15 additions and 11 deletions

View File

@ -18,6 +18,7 @@ import java.text.SimpleDateFormat;
import java.io.File;
import java.util.Date;
import java.util.Objects;
import org.springframework.core.io.ClassPathResource;
@Service
public class CertService {
@ -34,7 +35,7 @@ public class CertService {
){
try {
// 加载背景模板图片
BufferedImage background = ImageIO.read(new File("static/cert/template.png"));
BufferedImage background = ImageIO.read(new ClassPathResource("static/cert/template.png").getInputStream());
Graphics2D g2d = (Graphics2D) background.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

View File

@ -1,19 +1,22 @@
package com.example.caseData.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
public class ImageUtil {
public static byte[] readImageToBytes(String imagePath) throws IOException {
File file = new File(imagePath);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] bytes = new byte[(int) file.length()];
int read = fis.read(bytes);
if (read != bytes.length) {
throw new IOException("读取文件长度不一致");
ClassPathResource resource = new ClassPathResource(imagePath);
try (InputStream inputStream = resource.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
return bytes;
return outputStream.toByteArray();
}
}
}