response=$curl -s httpsapphuashijieartapiworkdetailV2visitorId=-1&workId=180830738&cur_user_id=-1&platform=android&os_version=28&version_code=217&device_brand=vivo&device_model=vivo20X21&token=&channe
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
try {
String apiUrl = "https://app.huashijie.art/api/work/detailV2?visitorId=-1&workId=180830738&cur_user_id=-1&platform=android&os_version=28&version_code=217&device_brand=vivo&device_model=vivo%20X21&token=&channel=vivo";
String response = sendGetRequest(apiUrl);
String json = response.trim();
String b = getJsonValue(json, "data.multiImages[0].imgPath");
System.out.println(b);
Pattern pattern = Pattern.compile("http://bsyimg.pandapaint.net/v2/work_cover/user/[0-9]+/[0-9]+.jpg");
Matcher matcher = pattern.matcher(b);
if (matcher.find()) {
String imageUrl = matcher.group();
System.out.println("下载图片:" + imageUrl);
// 使用Java下载图片
downloadImage(imageUrl);
} else {
System.out.println("未能提取到图片地址");
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String sendGetRequest(String apiUrl) throws IOException {
URL url = new URL(apiUrl);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
return response.toString();
}
private static String getJsonValue(String json, String key) {
String[] keys = key.split("\\.");
for (String k : keys) {
if (json.startsWith("{") && json.endsWith("}")) {
json = json.substring(1, json.length() - 1);
} else {
return "";
}
String regex = "\"" + k + "\":\\s*\"(.*?)\"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(json);
if (matcher.find()) {
json = matcher.group(1);
} else {
return "";
}
}
return json;
}
private static void downloadImage(String imageUrl) throws IOException {
URL url = new URL(imageUrl);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
String savePath = "./" + fileName;
// 创建输出流,将图片保存到本地
Files.copy(reader, Paths.get(savePath));
System.out.println("图片已保存:" + savePath);
}
}
}
原文地址: https://www.cveoy.top/t/topic/jaAM 著作权归作者所有。请勿转载和采集!