📄 main.java
字号:
package downLoadImage;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main extends JFrame {
private final String TITLE = "网站图片下载器";
private boolean hasStart = false;
private Connect con;
private JTextArea searchInfo;
private JTextField fileSize;
private JTextField savePath;
private JTextField prefix;
private JTextField searchDepth;
private JTextField maxThread;
private JTextField startPage;
private JTextField webURL;
//过滤网页选项
private JCheckBox shtmlCheckBox;
private JCheckBox htmlCheckBox;
private JCheckBox aspxCheckBox;
private JCheckBox aspCheckBox;
private JCheckBox htmCheckBox;
private JCheckBox jspCheckBox;
private JCheckBox phpCheckBox;
private JCheckBox doCheckBox;
//过滤图片选项
private JCheckBox jpgCheckBox;
private JCheckBox bmpCheckBox;
private JCheckBox pngCheckBox;
private JCheckBox gifCheckBox;
private JButton confirm;
public Main() {
super();
//////////////////////////////////////////////////////////////////////////////////////////////
//顶层菜单
final JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
final JMenuItem menuItem_1 = new JMenuItem();
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//if(!getStarted()){
new ShowDialog(Main.this,1);
//}
}
});
menuItem_1.setText("文件统计");
menuBar.add(menuItem_1);
final JMenuItem menuItem_2 = new JMenuItem();
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ShowDialog(Main.this,2);
}
});
menuItem_2.setText("查看设置");
menuBar.add(menuItem_2);
final JMenuItem menuItem = new JMenuItem();
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Runtime rt = Runtime.getRuntime();
try {
String cmd ="rundll32 SHELL32.DLL,ShellExec_RunDLL " + savePath.getText();
rt.exec(cmd);
} catch (Exception ex) {
JOptionPane.showConfirmDialog(Main.this, "您还没有创建文件夹,要创建吗?");
}
}
});
menuItem.setText("打开目录");
menuBar.add(menuItem);
final JMenuItem menuItem_4 = new JMenuItem();
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ShowDialog(Main.this,3);
}
});
menuItem_4.setText("帮助");
menuBar.add(menuItem_4);
final JMenuItem menuItem_3 = new JMenuItem();
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if( JOptionPane.showConfirmDialog(Main.this, "真的要停止下载并关闭窗口吗?")==0 ){
Main.this.dispose();
System.exit(0);
}
}
});
menuItem_3.setText("退出");
menuBar.add(menuItem_3);
////////////////////////////////////////////////////////////////////////////////////////////////
//选项布局
getContentPane().setLayout(null);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(new Color(238, 247, 232));
panel.setBounds(10, 10, 518, 183);
getContentPane().add(panel);
final JLabel label = new JLabel();
label.setBounds(0, 10, 58, 15);
label.setText("网站地址");
panel.add(label);
webURL = new JTextField();
webURL.setText("http://");
webURL.setBounds(72, 8, 186, 20);
panel.add(webURL);
final JLabel label_1 = new JLabel();
label_1.setText("起始页面");
label_1.setBounds(0, 45, 58, 15);
panel.add(label_1);
startPage = new JTextField();
startPage.setText("http://");
startPage.setBounds(72, 42, 328, 20);
panel.add(startPage);
final JLabel label_1_1 = new JLabel();
label_1_1.setText("下载图片");
label_1_1.setBounds(0, 114, 58, 15);
panel.add(label_1_1);
////////////////////////////////////////////////////////////////////////////////////////////////
//过滤图片按钮
jpgCheckBox = new JCheckBox();
jpgCheckBox.setSelected(true);
jpgCheckBox.setText("jpg");
jpgCheckBox.setBounds(66, 110, 48, 23);
panel.add(jpgCheckBox);
pngCheckBox = new JCheckBox();
pngCheckBox.setSelected(true);
pngCheckBox.setText("png");
pngCheckBox.setBounds(127, 110, 48, 23);
panel.add(pngCheckBox);
bmpCheckBox = new JCheckBox();
bmpCheckBox.setSelected(true);
bmpCheckBox.setText("bmp");
bmpCheckBox.setBounds(181, 110, 58, 23);
panel.add(bmpCheckBox);
gifCheckBox = new JCheckBox();
gifCheckBox.setText("gif");
gifCheckBox.setBounds(245, 110, 48, 23);
panel.add(gifCheckBox);
////////////////////////////////////////////////////////////////////////////////////////////////
//过滤网页按钮
final JLabel label_1_1_2 = new JLabel();
label_1_1_2.setText("网页过滤");
label_1_1_2.setBounds(0, 80, 58, 15);
panel.add(label_1_1_2);
shtmlCheckBox = new JCheckBox();
shtmlCheckBox.setSelected(true);
shtmlCheckBox.setText("shtml");
shtmlCheckBox.setBounds(64, 75, 61, 23);
panel.add(shtmlCheckBox);
htmlCheckBox = new JCheckBox();
htmlCheckBox.setSelected(true);
htmlCheckBox.setText("html");
htmlCheckBox.setBounds(130, 75, 55, 23);
panel.add(htmlCheckBox);
aspxCheckBox = new JCheckBox();
aspxCheckBox.setSelected(true);
aspxCheckBox.setText("aspx");
aspxCheckBox.setBounds(190, 75, 55, 23);
panel.add(aspxCheckBox);
aspCheckBox = new JCheckBox();
aspCheckBox.setSelected(true);
aspCheckBox.setText("asp");
aspCheckBox.setBounds(250, 75, 48, 23);
panel.add(aspCheckBox);
htmCheckBox = new JCheckBox();
htmCheckBox.setSelected(true);
htmCheckBox.setText("htm");
htmCheckBox.setBounds(305, 75, 48, 23);
panel.add(htmCheckBox);
jspCheckBox = new JCheckBox();
jspCheckBox.setText("jsp");
jspCheckBox.setBounds(355, 75, 48, 23);
panel.add(jspCheckBox);
phpCheckBox = new JCheckBox();
phpCheckBox.setSelected(true);
phpCheckBox.setText("php");
phpCheckBox.setBounds(410, 75, 48, 23);
panel.add(phpCheckBox);
doCheckBox = new JCheckBox();
doCheckBox.setText("do");
doCheckBox.setBounds(460, 75, 48, 23);
panel.add(doCheckBox);
////////////////////////////////////////////////////////////////////////////////////////////////
//下载选项
final JLabel label_1_1_1 = new JLabel();
label_1_1_1.setText("下载设置");
label_1_1_1.setBounds(0, 147, 58, 15);
panel.add(label_1_1_1);
maxThread = new JTextField();
maxThread.setText("10");
maxThread.setBounds(117, 145, 42, 20);
panel.add(maxThread);
final JLabel label_1_1_1_1 = new JLabel();
label_1_1_1_1.setText("线程数");
label_1_1_1_1.setBounds(72, 147, 42, 15);
panel.add(label_1_1_1_1);
final JLabel label_1_1_1_1_1 = new JLabel();
label_1_1_1_1_1.setText("搜索深度");
label_1_1_1_1_1.setBounds(165, 147, 58, 15);
panel.add(label_1_1_1_1_1);
searchDepth = new JTextField();
searchDepth.setText("2");
searchDepth.setBounds(227, 145, 42, 20);
panel.add(searchDepth);
prefix = new JTextField();
prefix.setBounds(330, 143, 42, 20);
panel.add(prefix);
final JLabel label_1_1_1_1_1_1 = new JLabel();
label_1_1_1_1_1_1.setText("添加前缀");
label_1_1_1_1_1_1.setBounds(275, 147, 58, 15);
panel.add(label_1_1_1_1_1_1);
final JLabel label_2 = new JLabel();
label_2.setText("保存路径");
label_2.setBounds(285, 10, 57, 15);
panel.add(label_2);
savePath = new JTextField();
savePath.setText("images");
savePath.setBounds(348, 8, 82, 20);
panel.add(savePath);
final JButton button = new JButton();
button.setText("选择");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("/");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setVisible(true);
if (chooser.showOpenDialog(Main.this) == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
savePath.setText(f.getAbsolutePath());
}
}
});
button.setBounds(436, 6, 72, 23);
panel.add(button);
final JLabel label_1_1_1_1_1_1_1 = new JLabel();
label_1_1_1_1_1_1_1.setText("大小设置");
label_1_1_1_1_1_1_1.setBounds(308, 114, 64, 15);
panel.add(label_1_1_1_1_1_1_1);
fileSize = new JTextField();
fileSize.setText("50");
fileSize.setBounds(373, 112, 57, 20);
panel.add(fileSize);
final JLabel label_3 = new JLabel();
label_3.setText("K 以上");
label_3.setBounds(437, 114, 48, 15);
panel.add(label_3);
////////////////////////////////////////////////////////////////////////////////////////////////
//开始下载
confirm = new JButton();
confirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (CheckValid()) {
if (!hasStart) {
hasStart = true;
Main.this.searchInfo.setText("");
JOptionPane.showMessageDialog(Main.this,
"图片即将下载,敬请等待收获。");
confirm.setText("停止");
setDownLoadSetEnable(false);
startSearch();
} else {
try {
hasStart = false;
while(con.isAlive()){
}
con=null;
setDownLoadSetEnable(true);
} catch (Exception eve) {
append("程序搜索时出现错误");
con = null;
hasStart = false;
setDownLoadSetEnable(true);
}
confirm.setText("继续下载");
}
}
}
});
confirm.setText("试试运气");
confirm.setBounds(379, 143, 129, 33);
panel.add(confirm);
////////////////////////////////////////////////////////////////////////////////////////////////
//下载情况查看
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBackground(new Color(238, 247, 232));
scrollPane.setBounds(10, 220, 518, 289);
getContentPane().add(scrollPane);
searchInfo = new JTextArea();
searchInfo.setEditable(false);
searchInfo.setDisabledTextColor(new Color(0, 128, 128));
searchInfo.setFont(new Font("", Font.BOLD, 12));
searchInfo.setBorder(new LineBorder(Color.black, 1, false));
scrollPane.setViewportView(searchInfo);
final JLabel label_4 = new JLabel();
label_4.setText("下载情况");
label_4.setBounds(10, 199, 60, 15);
getContentPane().add(label_4);
////////////////////////////////////////////////////////////////////////////////////////////////
//显示窗口
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(TITLE);
this.setSize(547, 570);
this.setLocation(200, 100);
this.setVisible(true);
setResizable(false);
}
private boolean startSearch() { //开始下载
try {
WebInfo.WebSite = this.webURL.getText().trim();
WebInfo.StartPage = this.startPage.getText().trim();
WebInfo.Prefix = this.prefix.getText().trim();
String temp = this.savePath.getText().trim();
WebInfo.SavePath = (temp.endsWith("\\") ? temp : temp + "\\");
temp = this.maxThread.getText().trim();
WebInfo.MaxThread = Integer.parseInt(temp);
temp = this.searchDepth.getText().trim();
WebInfo.SearchDepth = Integer.parseInt(temp);
temp = this.fileSize.getText().trim();
WebInfo.FileSize = Integer.parseInt(temp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -