|
|
|
@ -21,15 +21,18 @@ import org.jeecg.modules.assessmentReportUser.service.IAssessmentReportUserServi |
|
|
|
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember; |
|
|
|
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import sun.misc.BASE64Encoder; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.*; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.math.RoundingMode; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLConnection; |
|
|
|
import java.security.SecureRandom; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -307,6 +310,122 @@ public class ConfigServiceImpl implements ConfigService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/******************************************************************************************************************/ |
|
|
|
//图片转base64 |
|
|
|
@Override |
|
|
|
public Result<?> imageToBase64(String imgPath) { |
|
|
|
try { |
|
|
|
byte[] imageData = readImageData(imgPath); |
|
|
|
if (imageData != null && imageData.length > 0) { |
|
|
|
String base64 = Base64.getEncoder().encodeToString(imageData); |
|
|
|
System.out.println("转换成功: " + imageData.length + " 字节 -> " + base64.length() + " 字符"); |
|
|
|
return Result.OK("base64", base64); |
|
|
|
} |
|
|
|
return Result.error("图片转base64失败"); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return Result.error("图片转base64失败", e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Base64转图片文件 |
|
|
|
*/ |
|
|
|
public Result<?> base64ToImage(String base64Str, String savePath) { |
|
|
|
if (base64Str == null || base64Str.isEmpty()) { |
|
|
|
return Result.error("Base64字符串为空"); |
|
|
|
} |
|
|
|
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(savePath)) { |
|
|
|
// 清理Base64字符串 |
|
|
|
String cleanBase64 = cleanBase64String(base64Str); |
|
|
|
|
|
|
|
// 解码并保存 |
|
|
|
byte[] imageBytes = Base64.getDecoder().decode(cleanBase64); |
|
|
|
fos.write(imageBytes); |
|
|
|
|
|
|
|
log.info("图片保存成功: " + savePath + " (" + imageBytes.length + " 字节)"); |
|
|
|
return Result.OK("Base64转图片成功", savePath); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return Result.OK("Base64转图片失败", e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 读取图片数据 |
|
|
|
*/ |
|
|
|
private static byte[] readImageData(String imgPath) throws IOException { |
|
|
|
InputStream inputStream = null; |
|
|
|
ByteArrayOutputStream outputStream = null; |
|
|
|
|
|
|
|
try { |
|
|
|
// 创建输入流 |
|
|
|
if (imgPath.startsWith("http")) { |
|
|
|
URL url = new URL(imgPath); |
|
|
|
URLConnection connection = url.openConnection(); |
|
|
|
connection.setConnectTimeout(10000); |
|
|
|
connection.setReadTimeout(30000); |
|
|
|
connection.setRequestProperty("User-Agent", |
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"); |
|
|
|
inputStream = connection.getInputStream(); |
|
|
|
} else { |
|
|
|
inputStream = new FileInputStream(imgPath); |
|
|
|
} |
|
|
|
|
|
|
|
// 读取数据 |
|
|
|
outputStream = new ByteArrayOutputStream(); |
|
|
|
byte[] buffer = new byte[8192]; |
|
|
|
int bytesRead; |
|
|
|
int totalBytes = 0; |
|
|
|
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
|
|
|
outputStream.write(buffer, 0, bytesRead); |
|
|
|
totalBytes += bytesRead; |
|
|
|
} |
|
|
|
|
|
|
|
System.out.println("图片数据读取完成: " + totalBytes + " 字节"); |
|
|
|
return outputStream.toByteArray(); |
|
|
|
|
|
|
|
} finally { |
|
|
|
// 关闭流 |
|
|
|
if (inputStream != null) { |
|
|
|
try { |
|
|
|
inputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (outputStream != null) { |
|
|
|
try { |
|
|
|
outputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 清理Base64字符串 |
|
|
|
*/ |
|
|
|
private static String cleanBase64String(String base64Str) { |
|
|
|
String[] prefixes = { |
|
|
|
"data:image/png;base64,", "data:image/jpeg;base64,", |
|
|
|
"data:image/jpg;base64,", "data:image/gif;base64,", |
|
|
|
"data:image/bmp;base64," |
|
|
|
}; |
|
|
|
|
|
|
|
for (String prefix : prefixes) { |
|
|
|
if (base64Str.startsWith(prefix)) { |
|
|
|
return base64Str.substring(prefix.length()); |
|
|
|
} |
|
|
|
} |
|
|
|
return base64Str; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |