📄 download.java
字号:
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Download implements ActionListener
{
JFrame jMain=new JFrame("小楠下载器");
private JLabel label=new JLabel("请输入下载地址:");
private final JTextField text=new JTextField(15);
private final JTextArea area=new JTextArea();
private JButton btn2=new JButton("下载"),
btn=new JButton("显示信息");
private JPanel north2=new JPanel()
,north1=new JPanel();
public void Downloading()
{
jMain.setSize(400,300);
jMain.setLocation(100, 100);
north1.add(label);
north1.add(text);
jMain.getContentPane().add(north1,"North");
jMain.getContentPane().add(area,"Center");
north2.add(btn);
north2.add(btn2);
jMain.getContentPane().add(north2,"South");
btn.addActionListener(this/*new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String str=text.getText();
try
{
URL url=new URL(str);
URLConnection connection=url.openConnection();
String line=System.getProperty("line.separator");
area.append("主机名:"+url.getHost());
area.append(line);
area.append("授权::"+url.getAuthority());
area.append(line);
area.append("文件名:"+url.getFile());
area.append(line);
area.append("端口号:"+url.getDefaultPort());
area.append(line);
area.append("Type: "+connection.getContentType());
area.append(line);
area.append("长度:"+connection.getContentLength());
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}*/);
btn2.addActionListener(this/*
* new ActionListener() { public void
* actionPerformed(ActionEvent e) { String
* str=text.getText(); try { URL url=new
* URL(str); URLConnection
* connection=url.openConnection();
* InputStream
* ips=connection.getInputStream();
* FileOutputStream fos=new
* FileOutputStream("D:\\rencai.txt"); int
* data; while((data=ips.read())!=-1) {
* fos.write(data); } ips.close();
* fos.close(); } catch(Exception ee) {
* ee.printStackTrace(); } } }
*/);
jMain.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jMain.setVisible(true);
}
public static void main(String[] args) {
Download down = new Download();
down.Downloading();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btn2)) {
String str = text.getText();
try {
URL url = new URL(str);
URLConnection connection = url.openConnection();
InputStream ips = connection.getInputStream();
FileOutputStream fos = new FileOutputStream("D:\\rencai.txt");
int data;
while ((data = ips.read()) != -1) {
fos.write(data);
}
ips.close();
fos.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
else if(e.getSource().equals(btn))
{
String str=text.getText();
try
{
URL url=new URL(str);
URLConnection connection=url.openConnection();
String line=System.getProperty("line.separator");
area.append("主机名:"+url.getHost());
area.append(line);
area.append("授权::"+url.getAuthority());
area.append(line);
area.append("文件名:"+url.getFile());
area.append(line);
area.append("端口号:"+url.getDefaultPort());
area.append(line);
area.append("Type: "+connection.getContentType());
area.append(line);
area.append("长度:"+connection.getContentLength());
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -