使用知识:
文件流
新建文件,新建文件夹
模拟浏览器请求
package 文件操作;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Pic696 {
`public static void main(String[] args) {
for(int i =1;i<72;i++) {
for (int j =1; j < 50; j++) {
if(i<10&&j<10) {
String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-00"+i+"/0"+j+".jpg";
`
// System.out.println(i+" "+j);
System.out.println(strUrl);
URL url;
try {
url = new URL(strUrl);
try {
Pig(url, i, j);
} catch (IOException e) {
System.out.println("404");
}
} catch (MalformedURLException e) {
` }
}
if(i<10&&j>=10) {
String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-00"+i+"/"+j+".jpg";
`
// System.out.println(i+" "+j);
System.out.println(strUrl);
URL url;
try {
url = new URL(strUrl);
try {
Pig(url, i, j);
} catch (IOException e) {
System.out.println("404");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
` }
if(i>=10&&j<10) {
String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-0"+i+"/0"+j+".jpg";
`
// System.out.println(i+" "+j);
System.out.println(strUrl);
URL url;
try {
url = new URL(strUrl);
try {
Pig(url, i, j);
} catch (IOException e) {
System.out.println("404");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
` }
if(i>=10&&j>=10) {
String strUrl ="https://mmslt1.com/tp/girl/FEILIN/A-0"+i+"/"+j+".jpg";
`
// System.out.println(i+" "+j);
System.out.println(strUrl);
URL url;
try {
url = new URL(strUrl);
try {
Pig(url, i, j);
} catch (IOException e) {
System.out.println("404");
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
` }
}
}
`
}
`static void Pig(URL urlStr,int i,int j) throws IOException {
//构造连接
HttpURLConnection conn = (HttpURLConnection)urlStr.openConnection();
conn.setRequestMethod("GET");
//这个网站要模拟浏览器才行
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0");
//打开连接
conn.connect();
//打开这个网站的输入流
InputStream inStream = conn.getInputStream();
//用这个做中转站 ,把图片数据都放在了这里,再调用toByteArray()即可获得数据的byte数组
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//用这个是很好的,不用一次就把图片读到了文件中
//要是需要把图片用作其他用途呢?所以直接把图片的数据弄成一个变量,十分有用
//相当于操作这个变量就能操作图片了
byte [] buf = new byte[1024];
//为什么是1024?
//1024Byte=1KB,分配1KB的缓存
int len = 0;
//读取图片数据
while((len=inStream.read(buf))!=-1){
`
// System.out.println(len);
outStream.write(buf,0,len);
}
inStream.close();
outStream.close();
` //把图片数据填入文件中
`
// File files = new File("E://Pic696");
// files.mkdirs();
File file = new File("E://Pic696/Pic"+i+"_"+j+".jpg");
` FileOutputStream op = new FileOutputStream(file);
op.write(outStream.toByteArray());
op.close();
}
`
}