package data;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Download6 extends FocusAdapter implements Runnable,ActionListener
{
JFrame frame;
JTextField url_txt,dir_txt,name_txt;
JButton open_btn,start_btn;
JProgressBar pro;
JLabel l5;
JTextArea mes_txt;
static JFileChooser chooser;
String dir;
static Thread thread;
String urls,path;
int ID;
long start,end;
//初始化参数
public Download6(String urls,String path,int ID,long start,long end,JTextArea mes_txt)
{
this.urls=urls;
this.path=path;
this.ID=ID;
this.start=start;
this.end=end;
this.mes_txt=mes_txt;
}
//图形界面初始化
public Download6(String title)
{
frame=new JFrame(title);
frame.setLayout(null);
JLabel l1=new JLabel("网址(URL):");
frame.add(l1);
l1.setBounds(10,10,70,25);
url_txt=new JTextField(20);
url_txt.setText("http://");
url_txt.setCaretPosition(url_txt.getText().length());
url_txt.setFont(new Font(null,Font.BOLD,15));
url_txt.addFocusListener(this);
frame.add(url_txt);
url_txt.setBounds(75,10,315,25);
JSeparator line=new JSeparator();
frame.add(line);
line.setBounds(10,45,380,25);
JLabel l2=new JLabel("存放目录:");
frame.add(l2);
l2.setBounds(10,60,70,25);
dir_txt=new JTextField(15);
dir_txt.setText("请选择存放目录");
dir_txt.setEditable(false);
frame.add(dir_txt);
dir_txt.setBounds(75,60,250,25);
open_btn=new JButton("浏览");
frame.add(open_btn);
open_btn.setBounds(325,60,65,25);
JLabel l3=new JLabel("另存名称:");
frame.add(l3);
l3.setBounds(10,95,70,25);
name_txt=new JTextField(20);
frame.add(name_txt);
name_txt.setBounds(75,95,315,25);
name_txt.setFont(new Font(null,Font.BOLD,15));
mes_txt=new JTextArea();
mes_txt.setEditable(false);
mes_txt.setWrapStyleWord(true);
JScrollPane sp=new JScrollPane(mes_txt,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(sp);
sp.setBounds(10,125,380,80);
pro=new JProgressBar();
pro.setStringPainted(true);
frame.add(pro);
pro.setBounds(10,210,380,15);
JLabel l4=new JLabel("@逛悠人作品^_^");
frame.add(l4);
l4.setBounds(10,240,100,25);
l5=new JLabel();
frame.add(l5);
l5.setBounds(150,240,100,25);
start_btn=new JButton("开始下载");
frame.add(start_btn);
start_btn.setBounds(290,240,100,25);
frame.setLocation(300,200);
frame.setSize(405,300);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
open_btn.addActionListener(this);
start_btn.addActionListener(this);
}
//监听两个按钮方法
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s.equals(open_btn))
{
openfile();
}
else
{
String urls=url_txt.getText();
try
{
long time=System.currentTimeMillis()/1000;
URL url=new URL(urls);
URLConnection con=url.openConnection();
int filelength=con.getContentLength();
int num=10;
int size=filelength/num;
String path=dir_txt.getText()+name_txt.getText();
Thread t=null;
CountTime count=new CountTime(urls,path,name_txt.getText(),time,l5,pro,mes_txt,start_btn);
count.start();
for(int i=0;i<num;i++)
{
if(i!=num-1)
{
t=new Thread(new Download6(urls,path,i+1,size*i,size*(i+1)-1,mes_txt));
t.start();
}
else
{
t=new Thread(new Download6(urls,path,i+1,size*i,filelength,mes_txt));
t.start();
}
start_btn.setEnabled(false);
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
//获得焦点方法
public void focusGained(FocusEvent e)
{
url_txt.selectAll();
}
//打开存取目录
public void openfile()
{
chooser=new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("请选择存储目录");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=chooser.showOpenDialog(null);
if(result==chooser.APPROVE_OPTION)
{
dir=chooser.getSelectedFile().getPath();
if(dir.lastIndexOf('//')!=2)
{
dir=dir+'//';
}
dir_txt.setText(dir);
String urls=url_txt.getText();
int i=urls.lastIndexOf('/');
urls=urls.substring(i+1,urls.length());
name_txt.setText(urls);
}
}
//下载核心代码
public void run()
{
try
{
if(!urls.equals(null))
{
URL url=new URL(urls);
URLConnection con=url.openConnection();
con.setAllowUserInteraction(true);
con.setRequestProperty("Range","bytes="+start+"-"+end);
File file=new File(path);
if(file.exists())
{
file.delete();
file.createNewFile();
}
System.out.println("线程"+ID+"连接中...");
mes_txt.append("线程"+ID+"连接中..."+"/n");
mes_txt.setCaretPosition(mes_txt.getText().length());
RandomAccessFile rand=new RandomAccessFile(file,"rw");
rand.seek(start);
BufferedInputStream buffer=new BufferedInputStream(con.getInputStream());
byte[] b=new byte[4096];
int n=0;
while((n=buffer.read(b,0,b.length))!=-1)
{
rand.write(b,0,n);
}
System.out.println("线程"+ID+"下载完毕");
// mes_txt.append("线程"+ID+"下载完毕"+"/n");
mes_txt.setCaretPosition(mes_txt.getText().length());
buffer.close();
rand.close();
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public static void main(String[] args)
{
new Download6("多线程下载");
}
}
//计时和计算速度代码
class CountTime extends Thread
{
private String urls,path,name;
private long time;
private JLabel l5;
private JProgressBar pro;
private JTextArea mes_txt;
private JButton start_btn;
public CountTime(String urls,String path,String name,long time,JLabel l5,JProgressBar pro,JTextArea mes_txt,JButton start_btn)
{
this.urls=urls;
this.time=time;
this.path=path;
this.l5=l5;
this.pro=pro;
this.mes_txt=mes_txt;
this.start_btn=start_btn;
this.name=name;
}
public void run()
{
try
{
URL url=new URL(urls);
URLConnection con=url.openConnection();
int filelength=con.getContentLength();
pro.setMinimum(0);
pro.setMaximum(filelength);
File f=new File(path);
while(f.length()<filelength)
{
long LEN=f.length();
this.sleep(1000);
l5.setText((f.length()-LEN)/1024/1.0+"kb/s");
pro.setValue((int)f.length());
}
pro.setValue(0);
l5.setText("0.0kb/s");
mes_txt.setText("下载完毕"+"/n"+"文件名: "+name+"/n"+"文件存储路径: "+path+"/n"+"文件大小为: "+Math.round((float)filelength/1024/1024*100)/100.0+"MB"+"/n"+"下载用时:"+(System.currentTimeMillis()/1000-time)+"秒");
start_btn.setEnabled(true);
this.interrupt();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Download6 extends FocusAdapter implements Runnable,ActionListener
{
JFrame frame;
JTextField url_txt,dir_txt,name_txt;
JButton open_btn,start_btn;
JProgressBar pro;
JLabel l5;
JTextArea mes_txt;
static JFileChooser chooser;
String dir;
static Thread thread;
String urls,path;
int ID;
long start,end;
//初始化参数
public Download6(String urls,String path,int ID,long start,long end,JTextArea mes_txt)
{
this.urls=urls;
this.path=path;
this.ID=ID;
this.start=start;
this.end=end;
this.mes_txt=mes_txt;
}
//图形界面初始化
public Download6(String title)
{
frame=new JFrame(title);
frame.setLayout(null);
JLabel l1=new JLabel("网址(URL):");
frame.add(l1);
l1.setBounds(10,10,70,25);
url_txt=new JTextField(20);
url_txt.setText("http://");
url_txt.setCaretPosition(url_txt.getText().length());
url_txt.setFont(new Font(null,Font.BOLD,15));
url_txt.addFocusListener(this);
frame.add(url_txt);
url_txt.setBounds(75,10,315,25);
JSeparator line=new JSeparator();
frame.add(line);
line.setBounds(10,45,380,25);
JLabel l2=new JLabel("存放目录:");
frame.add(l2);
l2.setBounds(10,60,70,25);
dir_txt=new JTextField(15);
dir_txt.setText("请选择存放目录");
dir_txt.setEditable(false);
frame.add(dir_txt);
dir_txt.setBounds(75,60,250,25);
open_btn=new JButton("浏览");
frame.add(open_btn);
open_btn.setBounds(325,60,65,25);
JLabel l3=new JLabel("另存名称:");
frame.add(l3);
l3.setBounds(10,95,70,25);
name_txt=new JTextField(20);
frame.add(name_txt);
name_txt.setBounds(75,95,315,25);
name_txt.setFont(new Font(null,Font.BOLD,15));
mes_txt=new JTextArea();
mes_txt.setEditable(false);
mes_txt.setWrapStyleWord(true);
JScrollPane sp=new JScrollPane(mes_txt,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame.add(sp);
sp.setBounds(10,125,380,80);
pro=new JProgressBar();
pro.setStringPainted(true);
frame.add(pro);
pro.setBounds(10,210,380,15);
JLabel l4=new JLabel("@逛悠人作品^_^");
frame.add(l4);
l4.setBounds(10,240,100,25);
l5=new JLabel();
frame.add(l5);
l5.setBounds(150,240,100,25);
start_btn=new JButton("开始下载");
frame.add(start_btn);
start_btn.setBounds(290,240,100,25);
frame.setLocation(300,200);
frame.setSize(405,300);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
open_btn.addActionListener(this);
start_btn.addActionListener(this);
}
//监听两个按钮方法
public void actionPerformed(ActionEvent e)
{
Object s=e.getSource();
if(s.equals(open_btn))
{
openfile();
}
else
{
String urls=url_txt.getText();
try
{
long time=System.currentTimeMillis()/1000;
URL url=new URL(urls);
URLConnection con=url.openConnection();
int filelength=con.getContentLength();
int num=10;
int size=filelength/num;
String path=dir_txt.getText()+name_txt.getText();
Thread t=null;
CountTime count=new CountTime(urls,path,name_txt.getText(),time,l5,pro,mes_txt,start_btn);
count.start();
for(int i=0;i<num;i++)
{
if(i!=num-1)
{
t=new Thread(new Download6(urls,path,i+1,size*i,size*(i+1)-1,mes_txt));
t.start();
}
else
{
t=new Thread(new Download6(urls,path,i+1,size*i,filelength,mes_txt));
t.start();
}
start_btn.setEnabled(false);
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
//获得焦点方法
public void focusGained(FocusEvent e)
{
url_txt.selectAll();
}
//打开存取目录
public void openfile()
{
chooser=new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("请选择存储目录");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=chooser.showOpenDialog(null);
if(result==chooser.APPROVE_OPTION)
{
dir=chooser.getSelectedFile().getPath();
if(dir.lastIndexOf('//')!=2)
{
dir=dir+'//';
}
dir_txt.setText(dir);
String urls=url_txt.getText();
int i=urls.lastIndexOf('/');
urls=urls.substring(i+1,urls.length());
name_txt.setText(urls);
}
}
//下载核心代码
public void run()
{
try
{
if(!urls.equals(null))
{
URL url=new URL(urls);
URLConnection con=url.openConnection();
con.setAllowUserInteraction(true);
con.setRequestProperty("Range","bytes="+start+"-"+end);
File file=new File(path);
if(file.exists())
{
file.delete();
file.createNewFile();
}
System.out.println("线程"+ID+"连接中...");
mes_txt.append("线程"+ID+"连接中..."+"/n");
mes_txt.setCaretPosition(mes_txt.getText().length());
RandomAccessFile rand=new RandomAccessFile(file,"rw");
rand.seek(start);
BufferedInputStream buffer=new BufferedInputStream(con.getInputStream());
byte[] b=new byte[4096];
int n=0;
while((n=buffer.read(b,0,b.length))!=-1)
{
rand.write(b,0,n);
}
System.out.println("线程"+ID+"下载完毕");
// mes_txt.append("线程"+ID+"下载完毕"+"/n");
mes_txt.setCaretPosition(mes_txt.getText().length());
buffer.close();
rand.close();
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
public static void main(String[] args)
{
new Download6("多线程下载");
}
}
//计时和计算速度代码
class CountTime extends Thread
{
private String urls,path,name;
private long time;
private JLabel l5;
private JProgressBar pro;
private JTextArea mes_txt;
private JButton start_btn;
public CountTime(String urls,String path,String name,long time,JLabel l5,JProgressBar pro,JTextArea mes_txt,JButton start_btn)
{
this.urls=urls;
this.time=time;
this.path=path;
this.l5=l5;
this.pro=pro;
this.mes_txt=mes_txt;
this.start_btn=start_btn;
this.name=name;
}
public void run()
{
try
{
URL url=new URL(urls);
URLConnection con=url.openConnection();
int filelength=con.getContentLength();
pro.setMinimum(0);
pro.setMaximum(filelength);
File f=new File(path);
while(f.length()<filelength)
{
long LEN=f.length();
this.sleep(1000);
l5.setText((f.length()-LEN)/1024/1.0+"kb/s");
pro.setValue((int)f.length());
}
pro.setValue(0);
l5.setText("0.0kb/s");
mes_txt.setText("下载完毕"+"/n"+"文件名: "+name+"/n"+"文件存储路径: "+path+"/n"+"文件大小为: "+Math.round((float)filelength/1024/1024*100)/100.0+"MB"+"/n"+"下载用时:"+(System.currentTimeMillis()/1000-time)+"秒");
start_btn.setEnabled(true);
this.interrupt();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}