java获取王者皮肤之前说有个使用json判断的方法,今天也补充上,仅供参考
这样就两种方式都有了,一个是根据页面元素、一个是根据json文件
方法一:https://blog.csdn.net/yao694899744/article/details/109004459
方式二:本文
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
public class getWzSkinFormJson2 {
public static void main(String[] args) throws Exception {
String url = "https://pvp.qq.com/web201605/js/herolist.json";
StringBuilder jsonstr = new StringBuilder();
URL urlObject = new URL(url);
URLConnection uc = urlObject.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
jsonstr.append(inputLine);
}
in.close();
String jsonstr1=jsonstr.toString();
System.out.println("json:"+jsonstr1);
JSONArray jsonarray = new JSONArray(jsonstr1);
for (Object obj : jsonarray) {
JSONObject json = new JSONObject(obj.toString());
// 调用下载方法
System.out.println("==="+obj.toString());
if(json.has("skin_name")){
getImg(json.get("ename").toString(), json.get("cname").toString(), json.get("title").toString(), json.get("skin_name").toString());
}else{
getImg(json.get("ename").toString(), json.get("cname").toString(), json.get("title").toString(), null);
}
}
}
/**
* 根据图片地址,下载图片到本地
*
* @param heroNo
* 英雄编号
* @param heroTitle
* 英雄介绍
* @param heroName
* 英雄名称
* @param HeroSkin
* 英雄皮肤数量
*/
public static void getImg(String heroNo, String heroName, String heroTitle, String HeroSkin) {
String url = "http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/111/111-bigskin-1.jpg";
int HeroSkinCount = 1;
if (null!=HeroSkin&&HeroSkin.indexOf("|") > 0) {
String[] split = HeroSkin.split("\\|");
HeroSkinCount = split.length;
for (int i = 0; i < HeroSkinCount; i++) {
url = "http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/" + heroNo + "/" + heroNo + "-bigskin-" + (i + 1) + ".jpg";
downImg(heroNo,url,heroName,split[i]);
}
}else{
downImg(heroNo,url,heroName,heroTitle);
}
}
public static void downImg(String heroNo,String urlpic,String heroName,String HeroSkinName) {
try {
System.out.println(urlpic);
URL url2 = new URL(urlpic);
InputStream inputStream = url2.openStream();
File file = new File("C://王者荣耀//all2//"+heroNo+"_"+heroName+"_"+HeroSkinName+".jpg");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream out = new FileOutputStream(file);
// 需要创建一个byte数组
byte[] b = new byte[1024];
// 读取到的数据临时存放在b字节数组内
int count = inputStream.read(b);// count 指的是读取的有效字节个数
while (count != -1) {
out.write(b, 0, count);// 一个字节一个字节得往里写,直到count==-1
// 清空流管道
out.flush();
// 再读取下一次
count = inputStream.read(b);// 重新计算count的值
}
out.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}