嘿嘿,
半小时不见,我又来了
import java.io.*;
import java.net.*;
public class Test {
static int index = 100;
static String format = ".png";
static String url = "https://cdn.heweather.com/cond_icon/";
static String fileUrl = "C:\\Users\\admin\\Desktop";
static String folderName = "\\imgTemp";
public static void main(String[] args) throws IOException {
hashFolder(fileUrl + folderName);
String url;
while ((url = getUrl()) != null) {
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
if (connection.getResponseCode() == 200) {
InputStream put = connection.getInputStream();
File file = new File(getFileUrl());
if (!file.exists()) file.createNewFile();
OutputStream out = new FileOutputStream(file);
byte[] buff = new byte[1024];
int stream;
while ((stream = put.read(buff)) > 0) out.write(buff, 0, stream);
put.close();
out.close();
}
}
System.out.println("over");
}
static String getUrl() {
if (index >= 1000) return null;
return url + index++ + format;
}
static String getFileUrl() {
return fileUrl + folderName + '\\' +index + format;
}
static void hashFolder(String url) {
File file = new File(url);
if (file.isDirectory()) deleteFolder(file);
if (file.exists()) file.delete();
file.mkdir();
}
static void deleteFolder(File folder) {
for (File file: folder.listFiles())
if (file.isFile()) file.delete();
else deleteFolder(file);
}
}