public static String test65(int start) {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
String initUrl = "http://bz.hzwdd.cn/api.php?cid=6&start="+start+"&count=10";
URL url = URLUtil.url(initUrl);
try {
reader = new BufferedReader(new InputStreamReader(url.openStream(),"utf-8"));
String temp = null;
if ((temp= reader.readLine()) != null) {
sb.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
扫VX 领Java资料,前端,测试,python等等资料都有
这里就是下载的一个方法,字节流读一个写一个哈哈哈,真像我刚学java的时候使用的ps:这里的URLUtil使用的是hutool工具记得使用完关闭嗷
public static void test49(String urll,String fileName) throws IOException {
URL url = URLUtil.url(urll);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(5 * 1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File("D:/test/test/");
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName+".jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
System.out.println("info:" + url + " download success");
}
这里就单独提出另一个方法获取字节 记得使用完关闭哦~
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
然后就是亘古不变的main方法啦~ 这里用的是阿里的fastjson
public static void main(String[] args) {
String s = test65(0);
System.out.println("s = " + s);
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject json1 = (JSONObject) jsonObject.get("data");
int total_page = (int) json1.get("total_page");
for (int i = 0; i < total_page; i++) {
s = test65(i);
System.out.println("s = " + s);
jsonObject = JSONObject.parseObject(s);
json1 = (JSONObject) jsonObject.get("data");
List<Map<String,String>> list = (List<Map<String, String>>) json1.get("list");
for (Map<String, String> map : list) {
try {
test49(map.get("url"),map.get("tag")+map.get("id"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
扫VX 领Java资料,前端,测试,python等等资料都有