码迷,mamicode.com
首页 > 编程语言 > 详细

java基础第十九天_QQ、多线程下载

时间:2016-06-21 14:08:26      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:java 屏广

1.QQ

2.屏广软件,大数据包处理,frame缓冲区最多保持两帧画面,1s内没有集齐所有frameunit,则丢弃。

3.从互联网多线程下载

地址 : http://apache.opencas.org/hadoop/common/hadoop-2.7.2/hadoop-2.7.2.tar.gz

RandomAccessFile

Thread (>= 3)

HttpURLConnection.setRequestProperty("Range", "bytes=2097152-4194303");


GET / HTTP/1.1

Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*

Accept-Language: zh-CN

Cache-Control: no-cache

UA-CPU: AMD64

Range: bytes=2097152-4194303

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; .NET4.0C; .NET4.0E)

Host: www.baidu.com

Connection: Keep-Alive


getIs()









-------------------------------------------------------------------------------------------

项目源码;


package com.it18zhang.downloader;


/**

 * 下载信息对象

 */

public class DownloadInfo {


private String url;

private String local;

private int startPos; //起始位置

private int endPos; //结束位置

public DownloadInfo() {

}

public DownloadInfo(String url, String local, int startPos, int endPos) {

this.url = url;

this.local = local;

this.startPos = startPos;

this.endPos = endPos;

}



public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getLocal() {

return local;

}

public void setLocal(String local) {

this.local = local;

}

public int getStartPos() {

return startPos;

}

public void setStartPos(int startPos) {

this.startPos = startPos;

}

public int getEndPos() {

return endPos;

}

public void setEndPos(int endPos) {

this.endPos = endPos;

}


}

package com.it18zhang.downloader;


import java.io.RandomAccessFile;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;


/**

 * 下载管理器

 */

public class DownloadManager {

private String url ;

private String local ;

private int count ;

//文件总长度

private int totalLength ;

private List<DownloadInfo> infos ;

private UI ui ; //ui

/**

 * DownloadManager

 */

public DownloadManager(String url, String local, int count,UI ui) {

super();

this.url = url;

this.local = local;

this.count = count;

this.ui = ui ;

//准备下载

prepareDownload();

}


/*

 * 准备下载

 * 1.提取url文件大小

 * 2.创建本地文件

 * 3.准备DownloadInfo集合

 */

private void prepareDownload() {

infos = new ArrayList<DownloadInfo>();

try {

//1.提取文件大小

URL url0 = new URL(url);

this.totalLength = url0.openConnection().getContentLength();

//1‘.设置ui的进度条的最大值.

ui.setProgressbarMax(totalLength);

//2.创建本地文件

RandomAccessFile raf = new RandomAccessFile(local, "rw");

raf.setLength(totalLength);

raf.close();

//3.准备DownloadInfo集合

int block = totalLength / count;

//

DownloadInfo info = null ;

for(int i = 0 ; i < count ; i ++){

info = new DownloadInfo();

info.setUrl(url);

info.setLocal(local);

info.setStartPos(i * block);

if(i == (count - 1)){

info.setEndPos(totalLength - 1 );

}

else{

info.setEndPos((i + 1) * block - 1);

}

//

infos.add(info);

}

}

catch (Exception e) {

e.printStackTrace();

}

}

/**

 * 开始下载

 */

public void start(){

for(DownloadInfo info : infos){

new DownloadThread(info, ui).start();

}

}

}

package com.it18zhang.downloader;


import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.URL;

import java.net.URLConnection;


/**

 * 下载线程

 */

public class DownloadThread extends Thread {

private DownloadInfo info ;

private UI ui ;


public DownloadThread(DownloadInfo info,UI ui) {

this.info = info;

this.ui = ui ;

}


public void run(){

try {

URL url = new URL(info.getUrl());

URLConnection conn = url.openConnection();

//设置header的key-value

conn.setRequestProperty("Range", "bytes=" + info.getStartPos() + "-" + info.getEndPos());

//获取流

InputStream is = conn.getInputStream();

//定位本地文件

RandomAccessFile raf = new RandomAccessFile(info.getLocal(), "rw");

raf.seek(info.getStartPos());

byte[] buf = new byte[1024];

int len = 0 ;

while((len = is.read(buf)) != -1){

//

while(ui.isPausing){

try {

Thread.sleep(500);

}

catch (Exception e) {

e.printStackTrace();

}

}

raf.write(buf, 0, len);

//更新进度条

ui.updateProcessBar(len);

}

raf.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

package com.it18zhang.downloader;


public class StartUI {


public static void main(String[] args) {

new UI();

}

}

package com.it18zhang.downloader;


import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;


import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JProgressBar;

import javax.swing.JTextField;


public class UI extends JFrame {

//url

private JLabel lbUrl;

private JTextField tfUrl;

//本地地址

private JLabel lbLocal;

private JTextField tfLocal;

//线程数量

private JLabel lbCount;

private JTextField tfCount;

//按钮

private JButton btnStart;

//暂停

private JButton btnPause ;

//进度条

private JProgressBar bar ;

//是否暂停

public boolean isPausing = false ;

public UI(){

ini();

}

/**

 * 初始化

 */

private void ini(){

this.setBounds(0, 0, 600, 400);

this.setLayout(null);

//url 标签

lbUrl = new JLabel("url : ");

lbUrl.setBounds(0, 0, 100, 50);

this.add(lbUrl);

tfUrl = new JTextField("http://localhost:8000/jdk-7u25-linux-x86_64.tar.gz");

tfUrl.setBounds(120, 0, 400, 50);

this.add(tfUrl);

//local地址

lbLocal = new JLabel("local : ");

lbLocal.setBounds(0, 60, 100, 50);

this.add(lbLocal);

tfLocal = new JTextField("d:/xx.tar.gz");

tfLocal.setBounds(120, 60, 400, 50);

this.add(tfLocal);

//count地址

lbCount = new JLabel("Count : ");

lbCount.setBounds(0, 120, 100, 50);

this.add(lbCount);

tfCount = new JTextField("3");

tfCount.setBounds(120, 120, 400, 50);

this.add(tfCount);

//下载按钮

btnStart = new JButton("开始");

btnStart.setBounds(10, 180, 100, 50);

this.add(btnStart);

btnStart.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

String url = tfUrl.getText();

String local = tfLocal.getText();

int count = Integer.parseInt(tfCount.getText());

DownloadManager mgr = new DownloadManager(url, local, count, UI.this);

mgr.start();

bar.setVisible(true);

}

});

//暂停

btnPause = new JButton("暂停");

btnPause.setBounds(120, 180, 100, 50);

this.add(btnPause);

btnPause.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

UI.this.isPausing = !UI.this.isPausing ;

btnPause.setText(UI.this.isPausing?"继续":"暂停");

}

});

//进度条

bar = new JProgressBar();

bar.setBounds(5, 240, 550,10);

this.add(bar);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}


/**

 * 更新进度条

 */

public synchronized void updateProcessBar(int len) {

int newValue = bar.getValue() + len ;

bar.setValue(newValue);

if(newValue >= bar.getMaximum()){

JDialog d = new JDialog();

d.setTitle("下载完成!");

d.setVisible(true);

d.setBounds(400, 300, 200, 100);

bar.setValue(0);

bar.setVisible(false);

}

}


public void setProgressbarMax(int length) {

bar.setMaximum(length);

}

}


package com.it18zhang.urldemo;


import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;


public class TestApp {


public static void main(String[] args) throws Exception {

String urlStr = "http://localhost:8000/jdk-7u25-linux-x86_64.tar.gz";

URL url = new URL(urlStr);

URLConnection conn = url.openConnection();

String type = conn.getContentType();

int length = conn.getContentLength();

System.out.println(type + " : " + length);

InputStream is = conn.getInputStream();

FileOutputStream fos = new FileOutputStream("d:/jdk.gz");

byte[] buf = new byte[1024];

int len = 0 ;

while((len = is.read(buf)) != -1){

fos.write(buf, 0, len);

}

fos.close();

is.close();

}

}


本文出自 “作业” 博客,请务必保留此出处http://10718270.blog.51cto.com/10708270/1791319

java基础第十九天_QQ、多线程下载

标签:java 屏广

原文地址:http://10718270.blog.51cto.com/10708270/1791319

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!