?? duoxancheng.txt
字號:
先說下思路吧
主要類包括
DownInfo.java 線程下載信息類
DownloadFrame.java 下載界面
DownloadHis.java 下載歷史記錄,定時記錄下載信息,為斷點續傳提供基礎
DownloadMain.java 下載主線程
DownloadStat.java 下載信息統計類
DownThread.java 下載子線程
ResponseCode.java response響應編碼
UpdateService.java 更新服務類,用于讀取本地版本信息,服務器更新信息,該類需要自己實現
1 從客戶端讀取版本及服務器地址等信息
2 通過webservice將本地版本號與服務器版本號進行比對,如果服務器發布的版本號大于客戶端版本號,則從服務器查找要更新的文件。
3 啟動主線程,根據線程個數劃分每個線程的下載范圍。
4啟動下載子線程開始下載,子線程將下載的數據量調用統計類進行累計。
4 通過下載統計類得到下載信息,包括下載速度、下載量、剩余時間估計、完成百分比等等。
5 通過下載信息存檔類定時將下載信息寫入磁盤,作為斷點續傳的基礎。
源碼如下:
1 從客戶端讀取版本及服務器地址等信息
2 通過webservice將本地版本號與服務器版本號進行比對,如果服務器發布的版本號大于客戶端版本號,則從服務器查找要更新的文件。
3 啟動主線程,根據線程個數劃分每個線程的下載范圍。
4啟動下載子線程開始下載,子線程將下載的數據量調用統計類進行累計。
4 通過下載統計類得到下載信息,包括下載速度、下載量、剩余時間估計、完成百分比等等。
5 通過下載信息存檔類定時將下載信息寫入磁盤,作為斷點續傳的基礎。
源碼如下:
1 從客戶端讀取版本及服務器地址等信息
2 通過webservice將本地版本號與服務器版本號進行比對,如果服務器發布的版本號大于客戶端版本號,則從服務器查找要更新的文件。
3 啟動主線程,根據線程個數劃分每個線程的下載范圍。
4啟動下載子線程開始下載,子線程將下載的數據量調用統計類進行累計。
4 通過下載統計類得到下載信息,包括下載速度、下載量、剩余時間估計、完成百分比等等。
5 通過下載信息存檔類定時將下載信息寫入磁盤,作為斷點續傳的基礎。
源碼如下:
/**
* 線程下載信息
* @author zean
*/
public class DownInfo
{
private String threadId;//線程id
private BigDecimal startPos=new BigDecimal(0);//開始位置,隨著下載增長
private BigDecimal endPos=new BigDecimal(0);//結束位置
public DownInfo(String threadId, long startPos, long endPos)
{
this.threadId=threadId;
this.startPos = new BigDecimal(startPos);
this.endPos = new BigDecimal(endPos);
}
public long getStartPos()
{
return startPos.longValue();
}
public long getEndPos()
{
return endPos.longValue();
}
public void updateStartPos(long size)
{
this.startPos =startPos.add(new BigDecimal(size));
}
public String getThreadId()
{
return threadId;
}
public void setThreadId(String threadId)
{
this.threadId = threadId;
}
}
/**
* 下載界面類
* @author zean
* @create date 2008-06-28
*/
public class DownloadFrame extends JFrame
{
protected JTextField addressField = new JTextField();
protected JTextField totalFileField = new JTextField();
protected JTextField currentFileField = new JTextField();
protected JTextField localFilePathField = new JTextField();
protected JTextField threadNumField = new JTextField("3",2);
protected JTextField speedField = new JTextField();
protected JTextArea textArea = new JTextArea();
protected JProgressBar progressBar = new JProgressBar(0,10000);
protected JButton run= new JButton("開始下載");
private DownloadHis downloadHis;
private UpdateService updateService;
private DownloadMain dm;
private boolean isDownloadedAll=true;
protected JButton stop;
public DownloadFrame()
{
init();
}
/**
* 初始化
*/
private void init()
{
WindowHandler.launch(this,"更新程序",600,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
try
{
this.updateService=new UpdateService();
}
catch (GeneralFailureException e)
{
WindowHandler.alerm(this,"啟動更新程序出錯:"+e.getMessage(),"出錯");
run.setEnabled(true);
e.printStackTrace();
return;
}
if(updateService.getVersionInfo()==null||updateService.getUpdateFiles()==null||updateService.getUpdateFiles().size()<1)
{
WindowHandler.showMsg(this,"沒有需要更新的文件");
this.dispose();
return;
}
createFace();
List files=updateService.getUpdateFiles();
if(files==null||files.size()<1)
{
return;
}
/**
* 定位需要下載的文件
*/
downloadHis=new DownloadHis();
DataRecord file=null;
for(int i=0;i<files.size();i++)
{
file=(DataRecord)files.get(i);
downloadHis.initDownloadedInfo(file.getString("FILE_NAME"));
if(downloadHis.getHisPercent()!=10000)
{
isDownloadedAll=false;
break;
}
}
if(isDownloadedAll)
{
this.textArea.setText("更新程序已經下載完畢!");
this.run.setEnabled(false);
this.stop.setEnabled(false);
return;
}
else
{
progressBar.setValue(downloadHis.getHisPercent());
totalFileField.setText(String.valueOf(files.size()));
}
}
/**
* 創建界面
*/
private void createFace()
{
addressField.setPreferredSize(new Dimension(280,20));
localFilePathField.setPreferredSize(new Dimension(280,20));
totalFileField.setPreferredSize(new Dimension(280,20));
currentFileField.setPreferredSize(new Dimension(280,20));
speedField.setPreferredSize(new Dimension(280,20));
progressBar.setPreferredSize(new Dimension(280, 18));
progressBar.setBackground(Color.white);
progressBar.setForeground(Color.GRAY);
progressBar.setStringPainted(true);
progressBar.setStringPainted(true);
WindowHandler.launch(this, "下載", 380, 360);
ContentTemplet templet = new ContentTemplet();
templet.addCompnent(getInfoPanel(), 2);
templet.nextRow();
templet.addCompnent(new JLabel(" "), 2);
templet.nextRow();
templet.addComplex("更新文件數", totalFileField, "address");
templet.nextRow();
templet.addComplex("當前文件", currentFileField, "address");
templet.nextRow();
templet.addComplex("下載地址", addressField, "address");
templet.nextRow();
templet.addComplex("保存路徑", localFilePathField, "localFilePathField");
templet.nextRow();
templet.addComplex("線程數", threadNumField, "threadNum");
templet.nextRow();
templet.addComplex("下載速度", speedField, "threadNum");
templet.nextRow();
templet.addLabel(new JLabel("完成百分比"));
templet.addCompnent(progressBar, 1);
run.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
download();
}
});
stop = new JButton("暫停下載");
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
stopDown();
}
});
templet.addButton(run);
templet.addButton(stop);
this.getContentPane().add(templet.getFace());
this.getContentPane().setVisible(true);
}
private JScrollPane getInfoPanel()
{
JScrollPane content = new JScrollPane(this.textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
content.setPreferredSize(new Dimension(360, 100));
return content;
}
/**
* 停止下載
*/
public void stopDown()
{
run.setEnabled(true);
dm.stopDown();
}
/**
* 響應下載按鈕事件,啟動下載主線程
*/
public void download()
{
dm=new DownloadMain(this,Integer.parseInt(threadNumField.getText()),updateService);
dm.start();
run.setEnabled(false);
}
public static void main(String[] args)
{
new DownloadFrame().setVisible(true);
}
}
/**
* 下載線程
* @author zean
* @create date 2008-06-25
*/
public class DownThread extends Thread
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -