//下载url资源
public void download(String address) throws Exception{
URL url = new URL(address);//
URLConnection urlcon = url.openConnection();//打开连接
urlcon.connect();//连接url资源
InputStream in = urlcon.getInputStream();//从连接中获取输入流
String filePath = url.getFile();//得到下载的文件路径
int pos = filePath.lastIndexOf("/");
String fileName = filePath.substring(pos + 1);//得到下载文件名
FileOutputStream out = new FileOutputStream("D:\\" + fileName);//保存下载文件的路径
byte[] bytes = new byte[1024];
int len = in.read();
while(len != -1){
out.write(bytes, 0, len);//每次读取1024个字节数据写入输出流
len = in.read();
}
out.close();//关闭输出流
in.close();//关闭输入流
JOptionPane.showMessageDialog(this, "下载完毕");
}
public static void main(String[] args){
new SingleThreadDownload();
}
private final JPanel panel = new JPanel();
private final JLabel label2 = new JLabel("输入网络资源的网址:");
JButton StartButton = new JButton("点击开始下载");
JButton resetButton = new JButton("清空");
JButton exitButton = new JButton("退出");
JTextField urlField = new JTextField(20);