import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;

public class Demo10 extends JFrame {
// 定义组件
JPanel jp1, jp2, jp3,jp0;
JButton jb1, jb2;
JLabel jl1, jl2 ,jl0;
JTextField jt1,jt2,jt0;

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo10 de = new Demo10();
}

// 构造函数
public Demo10() {
// 创建组件

jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp0 = new JPanel();

jl1 = new JLabel("id ");
jl2 = new JLabel("num ");
jl0 = new JLabel("filepath");

jt1 = new JTextField(15);
jt2 = new JTextField(15);
jt0 = new JTextField(15);

jb1 = new JButton("start");
jb2 = new JButton("close");

this.setLayout(new GridLayout(4, 1));

jp0.add(jl0);
jp0.add(jt0);

jp1.add(jl1);
jp1.add(jt1);

jp2.add(jl2);
jp2.add(jt2);

jp3.add(jb1);
jp3.add(jb2);

this.add(jp0);
this.add(jp1);
this.add(jp2);
this.add(jp3);

String fx = "D:/local_software/Java/UI";
System.setProperty("use.dir",fx);
File f = new File(this.getClass().getResource("/").getPath());
String text_= f.toString();
jt0.setText(text_);

Button_action handler = new Button_action();
jb1.addActionListener(handler);
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

jt0.setTransferHandler(new TransferHandler()
{
private static final long serialVersionUID = 1L;
@Override
public boolean importData(JComponent comp, Transferable t) {
try {
Object o = t.getTransferData(DataFlavor.javaFileListFlavor);

String filepath = o.toString();
if (filepath.startsWith("[")) {
filepath = filepath.substring(1);
}
if (filepath.endsWith("]")) {
filepath = filepath.substring(0, filepath.length() - 1);
}
System.out.println(filepath);
jt0.setText(filepath);
return true;
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean canImport(JComponent comp, DataFlavor[] flavors) {
for (int i = 0; i < flavors.length; i++) {
if (DataFlavor.javaFileListFlavor.equals(flavors[i])) {
return true;
}
}
return false;
}
});

this.setTitle("爬虫");

this.setSize(300, 200);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
class Button_action implements ActionListener

{
public int readInputStream(InputStream inStream,String path)
{
try
{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 )
{
outStream.write(buffer, 0, len);
}
inStream.close();
File imageFile = new File(path);
FileOutputStream outStream1 = new FileOutputStream(path);
outStream1.write(outStream.toByteArray());
outStream1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return 0;
}

public void download(String url2,String url_,String path)
{
try
{
URL url = new URL(url2);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");
conn.addRequestProperty("Referer",url_);
conn.setReadTimeout(2000);
conn.setConnectTimeout(2000);
HttpURLConnection connection = null;
conn.setRequestMethod("GET");
conn.setConnectTimeout(2 * 1000);
InputStream inStream = conn.getInputStream();
readInputStream(inStream,path);
}catch(IOException e)
{
e.printStackTrace();
}
finally {
System.out.println("run...");
}
}


public void actionPerformed( ActionEvent event )
{
String id=jt1.getText().toString();
String num_data=jt2.getText().toString();
String file_path=jt0.getText().toString();

if(id.equals("") || num_data.equals("") || file_path.equals("") )
{
JOptionPane.showMessageDialog(null,"请补全信息!");
return;
}


String str = id;
String url1 ="https://www.mzitu.com/"+str+"/";
int all_num = Integer.parseInt(num_data);
File f = new File(file_path+"/"+str);
if(!f.exists()){
f.mkdirs();
}
System.out.println("start!");
int con=0;
for(int i=1;i<=all_num;i+=1)
{
try
{
con = con+1;
String url_ =url1 + i;
URL url = new URL(url_);
System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", "124.88.67.81");
System.getProperties().setProperty("http.proxyPort", "80");
URLConnection urlConnection = url.openConnection();
urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");
urlConnection.addRequestProperty("Referer",url_);
urlConnection.setReadTimeout(2000);
urlConnection.setConnectTimeout(2000);

HttpURLConnection connection = null;
if(urlConnection instanceof HttpURLConnection)
{
connection = (HttpURLConnection) urlConnection;
}
else
{
System.out.println("error!");
return;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null)
{
urlString += current;
}
/* String file = "stream.txt";
String charset = "UTF-8";
FileOutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(
outputStream);
try
{
writer.write(urlString);
}
finally {
writer.close();
} */
byte[] b = urlString.getBytes("gbk");
String urlStr = new String(b,"UTF-8");
Pattern p = Pattern.compile("<img class=\"blur\" src=\"(https://.*?.jpg)");
Matcher m = p.matcher(urlStr);
int num=0;
while(m.find()) {
String pic = m.group(num);

Matcher mt = Pattern.compile("(https://.*?.jpg)").matcher(pic);
if(mt.find())
{
System.out.println(url_);
String data = mt.group(num);
System.out.println(data);
String out = file_path+"/"+str+"/copy"+con+".jpg";
download(data,url_,out);
}

num++;
}
}
catch(IOException e)
{
//e.printStackTrace();
System.out.println("timeout or error!");
//break;
/*if(i>=all_num)
{
break;
}*/
}

}
System.out.println("finish!");
}
}
}

Java简易界面(爬虫)_safari

图片网址(举例):https://www.mzitu.com/204840
输入id=204840,图片数量num=23;(下载图片的数量)即可下载。
filepath会自动获取当前位置,也可支持拖拽目录到文本框。

因本人初学,不太会使用eclipse,配置好环境变量双击Java文件(或cmd运行)。