Posted on 2008-09-01 16:17
沙漠中的魚 閱讀(4291)
評論(0) 編輯 收藏 所屬分類:
Java
最近由于一個工程需要做應用程序啟動時,自動更新的項目
在GOOGLE上找了半天也沒見到什么比較好的辦法
自己動手寫了一個通過版本號檢查網絡上是不是存在新的更新文件,并自動通過HTTP下載文件的程序
希望對正在找此類程序的朋友有幫助
本地文件需要一個ver.txt 此文件內容為本地軟件版本號
網絡上我直接在一個頁面上打印出網絡存在的版本號
例如,這個例子里,我在 http://XXX.XXX.XXX/AutoUpdate/ver 這里直接打印出版本號
源文件:http://211.136.109.100/beiouwolf/AutoUpdate.rar
import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;


public class CheckUpdate extends JFrame
{
JFrame c = this;


public CheckUpdate()
{
//設置窗體屬性
setAttb();

JLabel title = new JLabel("正在檢查網絡上的更新資源
");
this.add(title, BorderLayout.NORTH);
JTextArea msg = new JTextArea();
this.add(msg, BorderLayout.CENTER);
JLabel process = new JLabel();
this.add(process, BorderLayout.SOUTH);
//啟動更新線程
new Check(msg, process).start();
}


private class Check extends Thread
{
//標識,是否存在新的更新文件
private boolean isUpdated = false;
//保存最新的版本
String netVersion;
//本地版本文件名
String LocalVerFileName = "ver.txt";

//顯示信息
private JTextArea msg;
private JLabel process;


public Check(JTextArea msg, JLabel process)
{
this.msg = msg;
this.process = process;
}


public void run()
{
//更新文件版本標識URL
String versionUrl = "http://XXX.XXX.XXX/AutoUpdate/ver";


/**//*
這里是通過HTTP訪問一個頁面,以取得網絡上的版本號
比如這里就是在這個頁面直接打印出 6.19.1.1
然后把這個版本號比對本地的版本號,如果版本號不同的話,就從網絡上下載新的程序并覆蓋現有程序

*/

URL url = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader netVer = null;

//讀取網絡上的版本號

try
{
url = new URL(versionUrl);
is = url.openStream();
isr = new InputStreamReader(is);

netVer = new BufferedReader(isr);
String netVerStr = netVer.readLine();
String localVerStr = getNowVer();


if (netVerStr.equals(localVerStr))
{
msg.append("當前文件是最新版本\n");
isUpdated = false;

} else
{
msg.append("存在更新文件,現在開始更新
\n");
isUpdated = true;
netVersion = netVerStr;
}


} catch (MalformedURLException ex)
{

} catch (IOException ex)
{

} finally
{
//釋放資源

try
{
netVer.close();
isr.close();
is.close();

} catch (IOException ex1)
{
}
}

//如果版本不同,下載網絡上的文件,更新本地文件

if (isUpdated)
{
//本地需要被更新的文件
File oldFile = new File("client.exe");
//緩存網絡上下載的文件
File newFile = new File("temp.exe");
//網絡上的文件位置
String updateUrl =
"http://XXX.XXX.XXX/downloads/simpkle.exe";

HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;


try
{
//打開URL通道
url = new URL(updateUrl);
httpUrl = (HttpURLConnection) url.openConnection();

httpUrl.connect();

byte[] buffer = new byte[1024];

int size = 0;

is = httpUrl.getInputStream();
bis = new BufferedInputStream(is);
fos = new FileOutputStream(newFile);

msg.append("正在從網絡上下載新的更新文件\n");

//保存文件

try
{
int flag = 0;
int flag2 = 0;

while ((size = bis.read(buffer)) != -1)
{
//讀取并刷新臨時保存文件
fos.write(buffer, 0, size);
fos.flush();

//模擬一個簡單的進度條

if (flag2 == 99)
{
flag2 = 0;
process.setText(process.getText() + ".");
}
flag2++;
flag++;

if (flag > 99 * 50)
{
flag = 0;
process.setText("");
}
}

} catch (Exception ex4)
{
System.out.println(ex4.getMessage());
}

msg.append("\n文件下載完成\n");

//把下載的臨時文件替換原有文件
CopyFile(oldFile,newFile);
//把本地版本文件更新為網絡同步
UpdateLocalVerFile();


} catch (MalformedURLException ex2)
{

} catch (IOException ex)
{
msg.append("文件讀取錯誤\n");

} finally
{

try
{
fos.close();
bis.close();
is.close();
httpUrl.disconnect();

} catch (IOException ex3)
{
}
}
}

//啟動應用程序

try
{
msg.append("啟動應用程序");
Thread.sleep(500);
Process p = Runtime.getRuntime().exec("client.exe");

} catch (IOException ex5)
{

} catch (InterruptedException ex)
{
}
//退出更新程序
System.exit(0);
}
//復制文件

private void CopyFile(File oldFile, File newFile)
{
FileInputStream in = null;
FileOutputStream out = null;

try
{

if(oldFile.exists())
{
oldFile.delete();
}
in = new FileInputStream(newFile);
out = new FileOutputStream(oldFile);

byte[] buffer = new byte[1024 * 5];
int size;

while ((size = in.read(buffer)) != -1)
{
out.write(buffer, 0, size);
out.flush();
}

} catch (FileNotFoundException ex)
{

} catch (IOException ex)
{

} finally
{

try
{
out.close();
in.close();

} catch (IOException ex1)
{
}
}

}


private void UpdateLocalVerFile()
{
//把本地版本文件更新為網絡同步
FileWriter verOS = null;
BufferedWriter bw = null;

try
{
verOS = new FileWriter(LocalVerFileName);

bw = new BufferedWriter(verOS);
bw.write(netVersion);
bw.flush();


} catch (IOException ex)
{

} finally
{

try
{
bw.close();
verOS.close();

} catch (IOException ex1)
{
}
}
}


private String getNowVer()
{
//本地版本文件
File verFile = new File(LocalVerFileName);

FileReader is = null;
BufferedReader br = null;

//讀取本地版本

try
{
is = new FileReader(verFile);

br = new BufferedReader(is);
String ver = br.readLine();

return ver;

} catch (FileNotFoundException ex)
{
msg.append("本地版本文件未找到\n");

} catch (IOException ex)
{
msg.append("本地版本文件讀取錯誤\n");

} finally
{
//釋放資源

try
{
br.close();
is.close();

} catch (IOException ex1)
{
}
}
return "";
}
}



private void setAttb()
{
//窗體設置
this.setTitle("Auto Update");
this.setSize(200, 150);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

// 窗體居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();

if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}

if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}


public static void main(String[] args)
{
CheckUpdate checkupdate = new CheckUpdate();
checkupdate.setVisible(true);
}
}
轉載:http://blog.csdn.net/beiouwolf/archive/2006/09/23/1268291.aspx