java多线程下载
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程下载相关的知识,希望对你有一定的参考价值。
package DownLoaderItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DownLoaderUI extends JFrame implements ActionListener{
private JLabel lburl;
private TextField tfurl;
private JLabel lblocation;
private TextField tfloacation;
private JLabel lbcount;
private TextField tfcount;
private JButton btnstar;
public JProgressBar bar;
public DownLoaderUI(){
init();
this.setVisible(true);
}
private void init() {
this.setBounds(100,50,800,600);
this.setLayout(null);
lburl=new JLabel("url地址");
lburl.setBounds(0,0,100,30);
this.add(lburl);
tfurl=new TextField("http://localhost:8080/zhao.txt");
tfurl.setBounds(0,40,800,30);
this.add(tfurl);
lblocation=new JLabel("保存地址");
lblocation.setBounds(0,80,100,30);
this.add(lblocation);
tfloacation=new TextField("e:\test\zhaouu.txt");
tfloacation.setBounds(0,120,800,30);
this.add(tfloacation);
lbcount=new JLabel("线程数");
lbcount.setBounds(0,160,100,30);
this.add(lbcount);
tfcount=new TextField("3");
tfcount.setBounds(0,200,800,30);
this.add(tfcount);
btnstar=new JButton("开始");
btnstar.setBounds(0,240,100,30);
btnstar.addActionListener(this);
this.add(btnstar);
bar=new JProgressBar();
bar.setBounds(0,280,750,30);
this.add(bar);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(-1);
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==btnstar){
String url=tfurl.getText();
String location=tfloacation.getText();
int count=Integer.parseInt(tfcount.getText());
DownLoader d = new DownLoader(url,location,count,this);
bar.setMaximum(d.getLength());
d.startDownload();
}
}
}
package DownLoaderItem;
public class Mainx {
public static void main(String[] args) {
new DownLoaderUI();
}
}
package DownLoaderItem;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoader {
private String url;
private String location;
private int count;
private DownLoaderUI ui;
private int length;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public DownLoaderUI getUi() {
return ui;
}
public void setUi(DownLoaderUI ui) {
this.ui = ui;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public DownLoader(String url, String location, int count, DownLoaderUI ui) {
this.url = url;
this.location = location;
this.count = count;
this.ui = ui;
this.length=getContentLen();
}
private int getContentLen() {
try {
URL u = new URL(this.url);
HttpURLConnection conn= (HttpURLConnection) u.openConnection();
int len = conn.getContentLength();
return len;
}catch (Exception e){
e.printStackTrace();
}
return -1;
}
public void startDownload() {
int blockSize=length/count;
int startpos;
int endpos;
for(int i=0;i<count;i++){
if(i==count-1){
endpos=length-1;
}else{
endpos=(i+1)*blockSize-1;
}
new DownLoaderThread(url,location,i*blockSize,endpos,ui).start();
}
}
}
package DownLoaderItem;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoaderThread extends Thread {
private String url;
private String location;
private int startpos;
private int endpos;
private DownLoaderUI ui;
public DownLoaderThread(String url, String location, int startpos, int endpos, DownLoaderUI ui) {
this.url = url;
this.location = location;
this.startpos = startpos;
this.endpos = endpos;
this.ui = ui;
}
@Override
public void run() {
try {
URL u = new URL(url);
HttpURLConnection conn= (HttpURLConnection) u.openConnection();
conn.setRequestProperty("Range","bytes="+startpos+"_"+endpos);
InputStream in = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(location, "rw");
raf.seek(startpos);
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1){
raf.write(buf,0,len);
synchronized (ui.bar){
ui.bar.setValue(ui.bar.getValue()+len);
}
}
raf.close();
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
以上是关于java多线程下载的主要内容,如果未能解决你的问题,请参考以下文章