IT十八掌作業_java基礎第20天_斷點續傳、屏廣軟件

感謝大家對IT十八掌大數據的支持,今天的作業如下:


闡述各個項目

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


斷點續傳

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

1.引入臨時文件,伴隨文件。

2.存放下載的數據 

3.文件名.tmp

[xxx.tmp]



4.邏輯

if(文件存在?){

if(臨時文件存在?){

//沒下完

//TODO 讀取臨時文件,構建下載信息

}

else{

//文件已下載完成

//TODO 刪除文件

}

}

//文件不存在

else{

//TODO 創建文件

//TODO 創建臨時文件。

}


5.線程下載過程期間,實時更新info信息.

6.下載完成時刪除臨時文件。

7.窗口退出的時候

if(下載未完成?){

//TODO 將下載信息存放到臨時文件中。

prop.store(os,comment);

}

8.窗口退出時,將infos集合內容構造成Properties對象,存成properties文件。

9.窗口打開是UI的佈局恢復成上次的進度。

10.現在線程從服務器中定位新的起始點。


屏廣軟件

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

1.將某個學生的屏幕分享給所有人。






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

項目源碼:


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.downloader2;


/**

 * 下載信息對象

 */

public class DownloadInfo {


//線程索引編號,主要是對應進度條

private int index ;

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 int getIndex() {

return index;

}


public void setIndex(int index) {

this.index = index;

}


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.downloader2;


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 ;

public List<DownloadInfo> getInfos() {

return infos;

}


public void setInfos(List<DownloadInfo> infos) {

this.infos = 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.setIndex(i); //索引

info.setUrl(url); //url

info.setLocal(local); //local

info.setStartPos(i * block); //startPos

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.downloader2;


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,info.getIndex());

}

raf.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

package com.it18zhang.downloader2;


public class StartUI {


public static void main(String[] args) {

new UI();

}

}

package com.it18zhang.downloader2;


import java.awt.Component;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.List;


import javax.swing.JButton;

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[] bars ;

//是否暫停

public boolean isPausing = false ;

//已經完成任務的線程數

private int completeCount = 0;

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);

//動態添加進度條

addBars(mgr.getInfos());

//

mgr.start();

}

});

//暫停

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?"繼續":"暫停");

}

});

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}


/**

* 動態添加進度條

*/

protected void addBars(List<DownloadInfo> infos) {

bars = new JProgressBar[infos.size()];

int y = 250 ;

for(int i = 0 ; i < infos.size() ; i ++){

DownloadInfo info = infos.get(i);

bars[i] = new JProgressBar();

bars[i].setMaximum(info.getEndPos() - info.getStartPos() + 1); //設置最大值

bars[i].setBounds(10, y + (i * 25), 400, 20);

this.add(bars[i]);

}

this.repaint();

}


/**

* 更新進度條

* @param indx 

*/

public void updateProcessBar(int len, int index) {

int newValue = bars[index].getValue() + len ;

bars[index].setValue(newValue);

if(newValue >= bars[index].getMaximum()){

completeCount ++ ;

if(completeCount >= bars.length){

//處理完成

processFinish();

}

}

}


/**

* 處理完成

*/

private void processFinish() {

for(Component cmp : bars){

this.remove(cmp);

}

this.repaint();

}

}

package com.it18zhang.downloader3;


/**

 * 下載信息對象

 */

public class DownloadInfo {


//線程索引編號,主要是對應進度條

private int index ;

private String url;

private String local;

private int startPos; //起始位置

private int endPos; //結束位置

private int amount ; //已經下載的數據量

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 int getIndex() {

return index;

}


public void setIndex(int index) {

this.index = index;

}


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;

}


public int getAmount() {

return amount;

}


public void setAmount(int amount) {

this.amount = amount;

}

}

package com.it18zhang.downloader3;


import java.io.File;

import java.io.FileInputStream;

import java.io.RandomAccessFile;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;


/**

 * 下載管理器

 */

public class DownloadManager {

private String url ;

private String local ;

private int count ;

//文件總長度

private int totalLength ;

private List<DownloadInfo> infos ;

public List<DownloadInfo> getInfos() {

return infos;

}


public void setInfos(List<DownloadInfo> infos) {

this.infos = 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 {

//0.判斷下載文件是否存在

File file = new File(local);

if(file.exists()){

File metaFile = new File(local + ".tmp");

//是否存在

if(metaFile.exists()){

this.infos = readInfosFromFile(metaFile);

}

}

//1.提取文件大小

URL url0 = new URL(url);

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

//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.setIndex(i); //索引

info.setUrl(url); //url

info.setLocal(local); //local

info.setStartPos(i * block); //startPos

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

info.setEndPos(totalLength - 1 );

}

else{

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

}

//

infos.add(info);

}

}

catch (Exception e) {

e.printStackTrace();

}

}

/**

* 從元文件讀取下載信息

*/

private List<DownloadInfo> readInfosFromFile(File metaFile) {

List<DownloadInfo> infos = new ArrayList<DownloadInfo>();

try {

//Hashtable子類

Properties prop = new Properties();

prop.load(new FileInputStream(metaFile));

String url = prop.getProperty("url");

int count = Integer.parseInt(prop.getProperty("count"));

DownloadInfo info = null ;

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

String value = prop.getProperty("thread." + i);

//arr[0]=startPos arr[1] arr[2]

String[] arr = value.split(";");

info = new DownloadInfo();

info.setIndex(i);

info.setUrl(url);

info.setStartPos(Integer.parseInt(arr[0]));

info.setEndPos(Integer.parseInt(arr[1]));

info.setAmount(Integer.parseInt(arr[2]));

String path = metaFile.getAbsolutePath() ;

path = path.substring(0, path.lastIndexOf("."));

info.setLocal(path);

infos.add(info);

}

}

catch (Exception e) {

e.printStackTrace();

}

return null;

}


/**

* 開始下載

*/

public void start(){

for(DownloadInfo info : infos){

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

}

}

}


package com.it18zhang.downloader3;


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();

}

}

//1.寫入文件

raf.write(buf, 0, len);

//2.更新進度條

ui.updateProcessBar(len,info.getIndex());

//3.更新下載信息

info.setAmount(info.getAmount() + len);

}

raf.close();

}

catch (Exception e) {

e.printStackTrace();

}

}

}

package com.it18zhang.downloader3;


public class StartUI {


public static void main(String[] args) {

new UI();

}

}

package com.it18zhang.downloader3;


import java.io.FileInputStream;

import java.util.Properties;


public class TestApp {


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

Properties prop = new Properties();

prop.load(new FileInputStream("d:/conf.properties"));

for(Object key : prop.keySet()){

Object value = prop.get(key);

System.out.println(key + "=" + value);

}

}


}


package com.it18zhang.downloader3;


import java.awt.Component;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.List;


import javax.swing.JButton;

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[] bars ;

//是否暫停

public boolean isPausing = false ;

//已經完成任務的線程數

private int completeCount = 0;

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);

//動態添加進度條

addBars(mgr.getInfos());

//

mgr.start();

}

});

//暫停

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?"繼續":"暫停");

}

});

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

}


/**

* 動態添加進度條

*/

protected void addBars(List<DownloadInfo> infos) {

bars = new JProgressBar[infos.size()];

int y = 250 ;

for(int i = 0 ; i < infos.size() ; i ++){

DownloadInfo info = infos.get(i);

bars[i] = new JProgressBar();

bars[i].setMaximum(info.getEndPos() - info.getStartPos() + 1); //設置最大值

bars[i].setBounds(10, y + (i * 25), 400, 20);

this.add(bars[i]);

}

this.repaint();

}


/**

* 更新進度條

* @param indx 

*/

public void updateProcessBar(int len, int index) {

int newValue = bars[index].getValue() + len ;

bars[index].setValue(newValue);

if(newValue >= bars[index].getMaximum()){

completeCount ++ ;

if(completeCount >= bars.length){

//處理完成

processFinish();

}

}

}


/**

* 處理完成

*/

private void processFinish() {

for(Component cmp : bars){

this.remove(cmp);

}

this.repaint();

}

}


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();

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章