package com.ghdi.api.service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.*;
import java.util.Base64;
public class WebToAppService {
private String apiBaseUrl = System.getenv("LINUX_SERVER_URL") != null
? System.getenv("LINUX_SERVER_URL")
: "http://your-server:8080";
public String buildApkAsBase64(String webUrl, String iconPath, String appName) {
return buildApkAsBase64(webUrl, iconPath, appName, null);
}
public String buildApkAsBase64(String webUrl, String iconPath, String appName, String appId) {
if (webUrl == null || webUrl.trim().isEmpty()) {
throw new IllegalArgumentException("Web 地址不能为空");
}
if (iconPath == null || iconPath.trim().isEmpty()) {
throw new IllegalArgumentException("图标文件路径不能为空");
}
if (appName == null || appName.trim().isEmpty()) {
throw new IllegalArgumentException("应用名称不能为空");
}
if (appId == null || appId.trim().isEmpty()) {
appId = "com.webapp.app";
}
System.out.println("==========================================");
System.out.println("\[WebToApp\] 开始打包请求(返回 base64)");
System.out.println("==========================================");
System.out.println("API 地址:" + apiBaseUrl);
System.out.println("Web 地址:" + webUrl);
System.out.println("图标路径:" + iconPath);
System.out.println("应用名称:" + appName);
System.out.println("应用 ID: " + appId);
System.out.println("");
try {
System.out.println("\[WebToApp\] \[1/3\] 读取图标文件...");
String iconBase64 = readIconAsBase64(iconPath);
System.out.println("\[WebToApp\] ✓ 图标已读取并编码为 base64 (" + (iconBase64.length() / 1024) + " KB)");
System.out.println("");
System.out.println("\[WebToApp\] \[2/3\] 发送打包请求到服务器...");
BuildResponse response = sendBuildRequest(webUrl, iconBase64, appName, appId);
System.out.println("\[WebToApp\] ✓ 服务器响应:" + response.message);
System.out.println("");
if (!response.success) {
throw new RuntimeException("打包失败:" + response.message);
}
System.out.println("\[WebToApp\] \[3/3\] 下载 APK 文件并转换为 base64...");
System.out.println("\[WebToApp\] APK 路径:" + response.apkPath);
System.out.println("\[WebToApp\] APK 大小:" + (response.apkSize / 1024 / 1024) + " MB");
System.out.println("\[WebToApp\] APK URL: " + response.apkUrl);
if (response.apkUrl == null || response.apkUrl.trim().isEmpty()) {
throw new RuntimeException("APK 下载 URL 为空,无法下载 APK 文件");
}
try {
String apkBase64 = downloadApkAsBase64(response.apkUrl);
System.out.println("\[WebToApp\] ✓ APK 已下载并转换为 base64");
System.out.println("\[WebToApp\] Base64 长度:" + (apkBase64.length() / 1024) + " KB");
System.out.println("");
return apkBase64;
} catch (Exception e) {
System.err.println("\[WebToApp\] ✗ APK 下载失败:" + e.getMessage());
e.printStackTrace();
throw new RuntimeException("下载 APK 文件失败:" + e.getMessage(), e);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("调用打包服务时发生错误:" + e.getMessage(), e);
}
}
public String buildApkAsBase64WithIconBase64(String webUrl, String iconBase64, String appName, String appId) {
if (webUrl == null || webUrl.trim().isEmpty()) {
throw new IllegalArgumentException("Web 地址不能为空");
}
if (iconBase64 == null || iconBase64.trim().isEmpty()) {
throw new IllegalArgumentException("图标 base64 不能为空");
}
if (appName == null || appName.trim().isEmpty()) {
throw new IllegalArgumentException("应用名称不能为空");
}
if (appId == null || appId.trim().isEmpty()) {
appId = "com.webapp.app";
}
System.out.println("==========================================");
System.out.println("\[WebToApp\] 开始打包请求(使用 base64 图标,返回 base64)");
System.out.println("==========================================");
System.out.println("API 地址:" + apiBaseUrl);
System.out.println("Web 地址:" + webUrl);
System.out.println("应用名称:" + appName);
System.out.println("应用 ID: " + appId);
System.out.println("");
try {
System.out.println("\[WebToApp\] \[1/3\] 使用传入的 base64 图标...");
String cleanBase64 = iconBase64;
if (iconBase64.contains(",")) {
cleanBase64 = iconBase64.substring(iconBase64.indexOf(",") + 1);
}
System.out.println("\[WebToApp\] ✓ 图标已准备为 base64 (" + (cleanBase64.length() / 1024) + " KB)");
System.out.println("");
System.out.println("\[WebToApp\] \[2/3\] 发送打包请求到服务器...");
BuildResponse response = sendBuildRequest(webUrl, cleanBase64, appName, appId);
System.out.println("\[WebToApp\] ✓ 服务器响应:" + response.message);
System.out.println("");
if (!response.success) {
throw new RuntimeException("打包失败:" + response.message);
}
System.out.println("\[WebToApp\] \[3/3\] 下载 APK 文件并转换为 base64...");
System.out.println("\[WebToApp\] APK 路径:" + response.apkPath);
System.out.println("\[WebToApp\] APK 大小:" + (response.apkSize / 1024 / 1024) + " MB");
System.out.println("\[WebToApp\] APK URL: " + response.apkUrl);
if (response.apkUrl == null || response.apkUrl.trim().isEmpty()) {
throw new RuntimeException("APK 下载 URL 为空,无法下载 APK 文件");
}
try {
String apkBase64 = downloadApkAsBase64(response.apkUrl);
System.out.println("\[WebToApp\] ✓ APK 已下载并转换为 base64");
System.out.println("\[WebToApp\] Base64 长度:" + (apkBase64.length() / 1024) + " KB");
System.out.println("");
return apkBase64;
} catch (Exception e) {
System.err.println("\[WebToApp\] ✗ APK 下载失败:" + e.getMessage());
e.printStackTrace();
throw new RuntimeException("下载 APK 文件失败:" + e.getMessage(), e);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("调用打包服务时发生错误:" + e.getMessage(), e);
}
}
public String buildApk(String webUrl, String iconPath, String appName, String windowsOutputDir) {
return buildApk(webUrl, iconPath, appName, null, windowsOutputDir);
}
public String buildApk(String webUrl, String iconPath, String appName, String appId, String windowsOutputDir) {
if (webUrl == null || webUrl.trim().isEmpty()) {
throw new IllegalArgumentException("Web 地址不能为空");
}
if (iconPath == null || iconPath.trim().isEmpty()) {
throw new IllegalArgumentException("图标文件路径不能为空");
}
if (appName == null || appName.trim().isEmpty()) {
throw new IllegalArgumentException("应用名称不能为空");
}
if (windowsOutputDir == null || windowsOutputDir.trim().isEmpty()) {
throw new IllegalArgumentException("Windows 输出目录不能为空");
}
if (appId == null || appId.trim().isEmpty()) {
appId = "com.webapp.app";
}
System.out.println("==========================================");
System.out.println("\[WebToApp\] 开始打包请求");
System.out.println("==========================================");
System.out.println("API 地址:" + apiBaseUrl);
System.out.println("Web 地址:" + webUrl);
System.out.println("图标路径:" + iconPath);
System.out.println("应用名称:" + appName);
System.out.println("应用 ID: " + appId);
System.out.println("Windows 输出目录:" + windowsOutputDir);
System.out.println("");
try {
System.out.println("\[WebToApp\] \[1/4\] 读取图标文件...");
String iconBase64 = readIconAsBase64(iconPath);
System.out.println("\[WebToApp\] ✓ 图标已读取并编码为 base64 (" + (iconBase64.length() / 1024) + " KB)");
System.out.println("");
System.out.println("\[WebToApp\] \[2/4\] 发送打包请求到服务器...");
BuildResponse response = sendBuildRequest(webUrl, iconBase64, appName, appId);
System.out.println("\[WebToApp\] ✓ 服务器响应:" + response.message);
System.out.println("");
if (!response.success) {
throw new RuntimeException("打包失败:" + response.message);
}
System.out.println("\[WebToApp\] \[3/4\] 下载 APK 文件...");
System.out.println("\[WebToApp\] APK 路径:" + response.apkPath);
System.out.println("\[WebToApp\] APK 大小:" + (response.apkSize / 1024 / 1024) + " MB");
System.out.println("\[WebToApp\] APK URL: " + response.apkUrl);
if (response.apkUrl == null || response.apkUrl.trim().isEmpty()) {
throw new RuntimeException("APK 下载 URL 为空,无法下载 APK 文件");
}
System.out.println("\[WebToApp\] Windows 输出目录:" + windowsOutputDir);
try {
String windowsApkPath = downloadApkFromUrl(response.apkUrl, windowsOutputDir, appName);
Path apkFilePath = Paths.get(windowsApkPath);
if (!Files.exists(apkFilePath)) {
throw new RuntimeException("APK 文件下载后不存在:" + windowsApkPath);
}
long fileSize = Files.size(apkFilePath);
System.out.println("\[WebToApp\] ✓ APK 已下载到:" + windowsApkPath);
System.out.println("\[WebToApp\] 文件大小:" + (fileSize / 1024 / 1024) + " MB");
System.out.println("");
return windowsApkPath;
} catch (Exception e) {
System.err.println("\[WebToApp\] ✗ APK 下载失败:" + e.getMessage());
e.printStackTrace();
throw new RuntimeException("下载 APK 文件失败:" + e.getMessage(), e);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("调用打包服务时发生错误:" + e.getMessage(), e);
}
}
private String downloadApkAsBase64(String apkUrl) throws Exception {
System.out.println("\[WebToApp\] \[下载\] 开始下载 APK 到内存...");
System.out.println("\[WebToApp\] \[下载\] URL: " + apkUrl);
String fullUrl = apkUrl;
if (!apkUrl.startsWith("http")) {
fullUrl = apiBaseUrl + (apkUrl.startsWith("/") ? apkUrl : "/" + apkUrl);
System.out.println("\[WebToApp\] \[下载\] 完整 URL: " + fullUrl);
}
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000);
connection.setReadTimeout(300000);
System.out.println("\[WebToApp\] \[下载\] 连接服务器...");
int responseCode = connection.getResponseCode();
System.out.println("\[WebToApp\] \[下载\] HTTP 响应代码:" + responseCode);
if (responseCode != 200) {
String errorMessage = "下载 APK 失败 (HTTP " + responseCode + ")";
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getErrorStream(), "utf-8"))) {
StringBuilder errorResponse = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
errorResponse.append(line);
}
if (errorResponse.length() > 0) {
errorMessage += ": " + errorResponse.toString();
}
}
throw new RuntimeException(errorMessage);
}
System.out.println("\[WebToApp\] \[下载\] 开始下载文件内容到内存...");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (InputStream inputStream = connection.getInputStream()) {
byte[] buffer = new byte[8192];
int bytesRead;
long totalBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytes += bytesRead;
if (totalBytes % (1024 * 1024) == 0) {
System.out.println("\[WebToApp\] \[下载\] 已下载:" + (totalBytes / 1024 / 1024) + " MB");
}
}
System.out.println("\[WebToApp\] \[下载\] 下载完成,总大小:" + (totalBytes / 1024 / 1024) + " MB");
}
System.out.println("\[WebToApp\] \[转换\] 开始转换为 base64 编码...");
byte[] apkBytes = outputStream.toByteArray();
String base64 = Base64.getEncoder().encodeToString(apkBytes);
System.out.println("\[WebToApp\] \[转换\] Base64 编码完成");
System.out.println("\[WebToApp\] \[转换\] 原始大小:" + (apkBytes.length / 1024 / 1024) + " MB");
System.out.println("\[WebToApp\] \[转换\] Base64 大小:" + (base64.length() / 1024) + " KB");
return base64;
} catch (Exception e) {
System.err.println("\[WebToApp\] \[下载\] 下载失败:" + e.getClass().getSimpleName() + " - " + e.getMessage());
e.printStackTrace();
throw e;
} finally {
connection.disconnect();
}
}
private BuildResponse sendBuildRequest(String webUrl, String iconBase64, String appName, String appId) throws Exception {
URL url = new URL(apiBaseUrl + "/api/build");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setDoOutput(true);
connection.setConnectTimeout(30000);
connection.setReadTimeout(1800000);
String requestJson = createRequestJson(webUrl, iconBase64, appName, appId);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestJson.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = connection.getResponseCode();
InputStream inputStream = (responseCode == 200)
? connection.getInputStream()
: connection.getErrorStream();
StringBuilder response = new StringBuilder();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream, "utf-8"))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
String responseBody = response.toString();
if (responseCode != 200) {
throw new RuntimeException("HTTP 请求失败 (code: " + responseCode + "): " + responseBody);
}
BuildResponse buildResponse = parseBuildResponse(responseBody);
System.out.println("\[WebToApp\] \[调试\] 解析结果 - success: " + buildResponse.success + ", message: " + buildResponse.message);
return buildResponse;
} finally {
connection.disconnect();
}
}
private String downloadApkFromUrl(String apkUrl, String windowsOutputDir, String appName) throws Exception {
System.out.println("\[WebToApp\] \[下载\] 开始下载 APK...");
System.out.println("\[WebToApp\] \[下载\] URL: " + apkUrl);
Path outputDir = Paths.get(windowsOutputDir);
Files.createDirectories(outputDir);
System.out.println("\[WebToApp\] \[下载\] 输出目录:" + outputDir.toAbsolutePath());
String fullUrl = apkUrl;
if (!apkUrl.startsWith("http")) {
fullUrl = apiBaseUrl + (apkUrl.startsWith("/") ? apkUrl : "/" + apkUrl);
System.out.println("\[WebToApp\] \[下载\] 完整 URL: " + fullUrl);
}
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000);
connection.setReadTimeout(300000);
System.out.println("\[WebToApp\] \[下载\] 连接服务器...");
int responseCode = connection.getResponseCode();
System.out.println("\[WebToApp\] \[下载\] HTTP 响应代码:" + responseCode);
if (responseCode != 200) {
String errorMessage = "下载 APK 失败 (HTTP " + responseCode + ")";
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getErrorStream(), "utf-8"))) {
StringBuilder errorResponse = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
errorResponse.append(line);
}
if (errorResponse.length() > 0) {
errorMessage += ": " + errorResponse.toString();
}
}
throw new RuntimeException(errorMessage);
}
String fileName = getFileNameFromUrl(connection, fullUrl, appName);
System.out.println("\[WebToApp\] \[下载\] 文件名:" + fileName);
Path apkFile = outputDir.resolve(fileName);
System.out.println("\[WebToApp\] \[下载\] 保存路径:" + apkFile.toAbsolutePath());
System.out.println("\[WebToApp\] \[下载\] 开始下载文件内容...");
try (InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(apkFile.toFile())) {
byte[] buffer = new byte[8192];
int bytesRead;
long totalBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytes += bytesRead;
if (totalBytes % (1024 * 1024) == 0) {
System.out.println("\[WebToApp\] \[下载\] 已下载:" + (totalBytes / 1024 / 1024) + " MB");
}
}
System.out.println("\[WebToApp\] \[下载\] 下载完成,总大小:" + (totalBytes / 1024 / 1024) + " MB");
}
if (!Files.exists(apkFile)) {
throw new RuntimeException("文件下载后不存在:" + apkFile.toAbsolutePath());
}
long fileSize = Files.size(apkFile);
System.out.println("\[WebToApp\] \[下载\] 文件已保存,大小:" + (fileSize / 1024 / 1024) + " MB");
return apkFile.toAbsolutePath().toString();
} catch (Exception e) {
System.err.println("\[WebToApp\] \[下载\] 下载失败:" + e.getClass().getSimpleName() + " - " + e.getMessage());
e.printStackTrace();
throw e;
} finally {
connection.disconnect();
}
}
private String getFileNameFromUrl(HttpURLConnection connection, String url, String appName) {
String contentDisposition = connection.getHeaderField("Content-Disposition");
if (contentDisposition != null && contentDisposition.contains("filename=")) {
String fileName = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9);
fileName = fileName.replace("\"", "").trim();
if (fileName.endsWith(".apk")) {
return fileName;
}
}
String urlFileName = url.substring(url.lastIndexOf('/') + 1);
if (urlFileName.endsWith(".apk")) {
return urlFileName;
}
return appName.replaceAll("[^a-zA-Z0-9]", "_") + ".apk";
}
private BuildResponse parseBuildResponse(String jsonResponse) {
BuildResponse response = new BuildResponse();
try {
System.out.println("\[WebToApp\] \[调试\] 原始 JSON: " + jsonResponse);
boolean isSuccess = jsonResponse.matches("(?s).*\\"success\\"\\s*:\\s*true\\b.*");
if (!isSuccess) {
isSuccess = jsonResponse.contains("\\"success\\":true") ||
jsonResponse.contains("\\"success\\": true") ||
jsonResponse.contains("'success':true") ||
jsonResponse.contains("'success': true");
}
response.message = extractJsonValue(jsonResponse, "message");
if (isSuccess) {
response.success = true;
response.apkPath = extractJsonValue(jsonResponse, "apkPath");
response.apkUrl = extractJsonValue(jsonResponse, "apkUrl");
String apkSizeStr = extractJsonNumberValue(jsonResponse, "apkSize");
if (apkSizeStr != null && !apkSizeStr.isEmpty()) {
try {
response.apkSize = Long.parseLong(apkSizeStr);
} catch (NumberFormatException e) {
response.apkSize = 0;
}
}
} else {
response.success = false;
if (response.message == null || response.message.isEmpty()) {
response.message = "服务器返回失败响应(未检测到 success:true)";
}
}
} catch (Exception e) {
response.success = false;
response.message = "解析响应失败:" + e.getMessage();
System.out.println("\[WebToApp\] \[错误\] 解析异常:" + e.getClass().getSimpleName() + " - " + e.getMessage());
System.out.println("\[WebToApp\] \[错误\] 原始响应前 200 字符:" + jsonResponse.substring(0, Math.min(200, jsonResponse.length())));
}
return response;
}
private String readIconAsBase64(String iconInput) {
if (iconInput.length() > 50 && (iconInput.startsWith("iVBORw0KGgo") || iconInput.startsWith("/9j/"))) {
System.out.println("\[WebToApp\] \[图标\] 输入被识别为 base64 字符串,直接使用。");
return iconInput;
}
try {
Path iconFile = Paths.get(iconInput);
if (!Files.exists(iconFile)) {
throw new IllegalArgumentException("图标文件不存在:" + iconInput);
}
String iconFileName = iconFile.getFileName().toString().toLowerCase();
if (!iconFileName.endsWith(".png") && !iconFileName.endsWith(".jpg")
&& !iconFileName.endsWith(".jpeg")) {
throw new IllegalArgumentException("图标文件必须是 png 或 jpg 格式");
}
byte[] iconBytes = Files.readAllBytes(iconFile);
String base64 = Base64.getEncoder().encodeToString(iconBytes);
return base64;
} catch (IllegalArgumentException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("读取图标文件失败:" + e.getMessage(), e);
}
}
private String createRequestJson(String webUrl, String iconBase64, String appName, String appId) {
StringBuilder json = new StringBuilder();
json.append("{");
json.append("\\"webUrl\\":\\"").append(escapeJson(webUrl)).append("\\",");
json.append("\\"iconBase64\\":\\"").append(iconBase64).append("\\",");
json.append("\\"appName\\":\\"").append(escapeJson(appName)).append("\\",");
json.append("\\"appId\\":\\"").append(escapeJson(appId)).append("\\"");
json.append("}");
return json.toString();
}
private String escapeJson(String str) {
return str.replace("\\\\", "\\\\\\\\")
.replace("\"", "\\\\\"")
.replace("\\n", "\\\\n")
.replace("\\r", "\\\\r")
.replace("\\t", "\\\\t");
}
private String extractJsonValue(String json, String key) {
String searchKey = "\\"" + key + "\\"";
int keyIndex = json.indexOf(searchKey);
if (keyIndex == -1) {
return null;
}
int colonIndex = json.indexOf(":", keyIndex);
if (colonIndex == -1) {
return null;
}
int startIndex = json.indexOf("\"", colonIndex) + 1;
if (startIndex == 0) {
return null;
}
int endIndex = json.indexOf("\"", startIndex);
if (endIndex == -1) {
return null;
}
return json.substring(startIndex, endIndex).replace("\\\\\"", "\\"");
}
/**
* 从 JSON 中提取数字值
*/
private String extractJsonNumberValue(String json, String key) {
String searchKey = "\\"" + key + "\\"";
int keyIndex = json.indexOf(searchKey);
if (keyIndex == -1) {
return null;
}
int colonIndex = json.indexOf(":", keyIndex);
if (colonIndex == -1) {
return null;
}
// 跳过空格
int valueStart = colonIndex + 1;
while (valueStart < json.length() && Character.isWhitespace(json.charAt(valueStart))) {
valueStart++;
}
// 查找数字的结束位置(逗号、}或]
int valueEnd = valueStart;
while (valueEnd < json.length()) {
char c = json.charAt(valueEnd);
if (c == ',' || c == '}' || c == '\]' || Character.isWhitespace(c)) {
break;
}
valueEnd++;
}
if (valueEnd <= valueStart) {
return null;
}
return json.substring(valueStart, valueEnd).trim();
}
/**
* 构建响应对象
*/
private static class BuildResponse {
boolean success;
String message;
String apkPath;
String apkUrl;
long apkSize;
}
// Getter 和 Setter 方法
public void setApiBaseUrl(String apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}
public String getApiBaseUrl() {
return apiBaseUrl;
}
}