DownPage.java
public class DownPage {
public static void main(String[] args){
pageDown gui = new pageDown();
gui.setVisible(true);
}
}
pageDown.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class pageDown extends JFrame {
public pageDown() {
initComponents();
}
private void thisWindowClosing(WindowEvent e) {
System.exit(0);
}
private void dwnldMouseClicked(MouseEvent e) {
try {
URL url = new URL("http://"+ urlField.getText());
String lct = lcFile.getText();
if(fileName.getText().equals("")){
String str = urlField.getText().substring(0, urlField.getText().indexOf("."));
String name =null;
if(str.equals("www")){
String nameTmp = urlField.getText().substring(urlField.getText().indexOf(".") + 1, urlField.getText().length());
name = nameTmp.substring(0,nameTmp.indexOf("."));
}else{
name = str;
}
file = new File(lct + "\\" + name + ".html");
}else{
file = new File(lct + "\\" + fileName.getText() + ".html");
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader down = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
BufferedWriter save = new BufferedWriter(new FileWriter(file));
String line = null;
while((line = down.readLine()) != null){
save.write(line);
save.newLine();
save.flush();
}
connection.disconnect();
save.close();
JOptionPane.showMessageDialog(this, "下载完成");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
} catch (IOException e1) {
JOptionPane.showMessageDialog(this, "下载失败,请检查网络连接并验证网页地址的有效性");
// TODO Auto-generated catch block
}catch (IllegalArgumentException e1){
JOptionPane.showMessageDialog(this, "请输入网页地址");
} catch(StringIndexOutOfBoundsException e1){
JOptionPane.showMessageDialog(this, "请检查你的网址是否有误");
}
}
private void openMouseClicked(MouseEvent e) {
// TODO add your code here
try {
if(file == null){
JOptionPane.showMessageDialog(this, "请先下载文件");
}else{
Desktop.getDesktop().open(file);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (IllegalArgumentException e1){
JOptionPane.showMessageDialog(this, "请先下载文件");
}
}
private void openFileMouseClicked(MouseEvent e) {
// TODO add your code here
JFileChooser jfChooser = new JFileChooser(lcFile.getText());
jfChooser.setDialogTitle("选择目标文件夹");
jfChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfChooser.showOpenDialog(this);
File fileIn = jfChooser.getCurrentDirectory();
lcFile.setText(fileIn.toString());
}
private void initComponents() {
dialogPane = new JPanel();
contentPanel = new JPanel();
label1 = new JLabel();
label3 = new JLabel();
urlField = new JTextField();
label2 = new JLabel();
lcFile = new JTextField();
openFile = new JButton();
label4 = new JLabel();
fileName = new JTextField();
label5 = new JLabel();
buttonBar = new JPanel();
dwnld = new JButton();
open = new JButton();
//======== this ========
setResizable(false);
setTitle("\u7f51\u9875\u4e0b\u8f7d\u5668");
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
thisWindowClosing(e);
}
});
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
//======== dialogPane ========
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setPreferredSize(new Dimension(400, 300));
dialogPane.addPropertyChangeListener(new java.beans.PropertyChangeListener(){
public void propertyChange(java.beans.PropertyChangeEvent e){
if("border".equals(e.getPropertyName()))
throw new RuntimeException();
}
});
dialogPane.setLayout(new BorderLayout());
//======== contentPanel ========
{
contentPanel.setPreferredSize(new Dimension(380, 30));
contentPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
//---- label1 ----
label1.setPreferredSize(new Dimension(360, 20));
label1.setText("\u8bf7\u8f93\u5165\u76ee\u6807URL\uff1a");
contentPanel.add(label1);
//---- label3 ----
label3.setText("http://");
contentPanel.add(label3);
//---- urlField ----
urlField.setPreferredSize(new Dimension(300, 25));
contentPanel.add(urlField);
//---- label2 ----
label2.setPreferredSize(new Dimension(360, 20));
label2.setText("\u8bf7\u8f93\u5165\u5b58\u50a8\u76ee\u6807\u5730\u5740\uff1a");
contentPanel.add(label2);
//---- lcFile ----
lcFile.setPreferredSize(new Dimension(300, 25));
lcFile.setText("C:\\Users\\Administrator\\Downloads");
contentPanel.add(lcFile);
//---- openFile ----
openFile.setText("...");
openFile.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
openFileMouseClicked(e);
}
});
contentPanel.add(openFile);
//---- label4 ----
label4.setText("\u5b58\u50a8\u4e3a\uff1a");
contentPanel.add(label4);
//---- fileName ----
fileName.setPreferredSize(new Dimension(150, 25));
contentPanel.add(fileName);
//---- label5 ----
label5.setText(".html");
contentPanel.add(label5);
}
dialogPane.add(contentPanel, BorderLayout.CENTER);
//======== buttonBar ========
{
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};
//---- dwnld ----
dwnld.setText("\u4e0b\u8f7d");
dwnld.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
dwnldMouseClicked(e);
}
});
buttonBar.add(dwnld, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
//---- open ----
open.setText("\u67e5\u770b");
open.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
openMouseClicked(e);
}
});
buttonBar.add(open, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
//GEN-END:initComponents
}
//GEN-BEGIN:variables
private JPanel dialogPane;
private JPanel contentPanel;
private JLabel label1;
private JLabel label3;
private JTextField urlField;
private JLabel label2;
private JTextField lcFile;
private JButton openFile;
private JLabel label4;
private JTextField fileName;
private JLabel label5;
private JPanel buttonBar;
private JButton dwnld;
private JButton open;
private File file;//GEN-END:variables
}