Posted on 2008-09-01 16:17
沙漠中的鱼 阅读(4289)
评论(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.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class CheckUpdate extends JFrame
{
JFrame c = this;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public CheckUpdate()
{
//设置窗体属性
setAttb();
![](/Images/OutliningIndicators/InBlock.gif)
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();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private class Check extends Thread
{
//标识,是否存在新的更新文件
private boolean isUpdated = false;
//保存最新的版本
String netVersion;
//本地版本文件名
String LocalVerFileName = "ver.txt";
![](/Images/OutliningIndicators/InBlock.gif)
//显示信息
private JTextArea msg;
private JLabel process;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Check(JTextArea msg, JLabel process)
{
this.msg = msg;
this.process = process;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void run()
{
//更新文件版本标识URL
String versionUrl = "http://XXX.XXX.XXX/AutoUpdate/ver";
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
这里是通过HTTP访问一个页面,以取得网络上的版本号
比如这里就是在这个页面直接打印出 6.19.1.1
然后把这个版本号比对本地的版本号,如果版本号不同的话,就从网络上下载新的程序并覆盖现有程序
![](/Images/OutliningIndicators/InBlock.gif)
*/
![](/Images/OutliningIndicators/InBlock.gif)
URL url = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader netVer = null;
![](/Images/OutliningIndicators/InBlock.gif)
//读取网络上的版本号
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
url = new URL(versionUrl);
is = url.openStream();
isr = new InputStreamReader(is);
![](/Images/OutliningIndicators/InBlock.gif)
netVer = new BufferedReader(isr);
String netVerStr = netVer.readLine();
String localVerStr = getNowVer();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (netVerStr.equals(localVerStr))
{
msg.append("当前文件是最新版本\n");
isUpdated = false;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
msg.append("存在更新文件,现在开始更新
\n");
isUpdated = true;
netVersion = netVerStr;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (MalformedURLException ex)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
//释放资源
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
netVer.close();
isr.close();
is.close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex1)
{
}
}
![](/Images/OutliningIndicators/InBlock.gif)
//如果版本不同,下载网络上的文件,更新本地文件
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (isUpdated)
{
//本地需要被更新的文件
File oldFile = new File("client.exe");
//缓存网络上下载的文件
File newFile = new File("temp.exe");
//网络上的文件位置
String updateUrl =
"http://XXX.XXX.XXX/downloads/simpkle.exe";
![](/Images/OutliningIndicators/InBlock.gif)
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
//打开URL通道
url = new URL(updateUrl);
httpUrl = (HttpURLConnection) url.openConnection();
![](/Images/OutliningIndicators/InBlock.gif)
httpUrl.connect();
![](/Images/OutliningIndicators/InBlock.gif)
byte[] buffer = new byte[1024];
![](/Images/OutliningIndicators/InBlock.gif)
int size = 0;
![](/Images/OutliningIndicators/InBlock.gif)
is = httpUrl.getInputStream();
bis = new BufferedInputStream(is);
fos = new FileOutputStream(newFile);
![](/Images/OutliningIndicators/InBlock.gif)
msg.append("正在从网络上下载新的更新文件\n");
![](/Images/OutliningIndicators/InBlock.gif)
//保存文件
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
int flag = 0;
int flag2 = 0;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while ((size = bis.read(buffer)) != -1)
{
//读取并刷新临时保存文件
fos.write(buffer, 0, size);
fos.flush();
![](/Images/OutliningIndicators/InBlock.gif)
//模拟一个简单的进度条
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (flag2 == 99)
{
flag2 = 0;
process.setText(process.getText() + ".");
}
flag2++;
flag++;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (flag > 99 * 50)
{
flag = 0;
process.setText("");
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception ex4)
{
System.out.println(ex4.getMessage());
}
![](/Images/OutliningIndicators/InBlock.gif)
msg.append("\n文件下载完成\n");
![](/Images/OutliningIndicators/InBlock.gif)
//把下载的临时文件替换原有文件
CopyFile(oldFile,newFile);
//把本地版本文件更新为网络同步
UpdateLocalVerFile();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (MalformedURLException ex2)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex)
{
msg.append("文件读取错误\n");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
fos.close();
bis.close();
is.close();
httpUrl.disconnect();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex3)
{
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
//启动应用程序
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
msg.append("启动应用程序");
Thread.sleep(500);
Process p = Runtime.getRuntime().exec("client.exe");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex5)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (InterruptedException ex)
{
}
//退出更新程序
System.exit(0);
}
//复制文件
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void CopyFile(File oldFile, File newFile)
{
FileInputStream in = null;
FileOutputStream out = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(oldFile.exists())
{
oldFile.delete();
}
in = new FileInputStream(newFile);
out = new FileOutputStream(oldFile);
![](/Images/OutliningIndicators/InBlock.gif)
byte[] buffer = new byte[1024 * 5];
int size;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while ((size = in.read(buffer)) != -1)
{
out.write(buffer, 0, size);
out.flush();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (FileNotFoundException ex)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
out.close();
in.close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex1)
{
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void UpdateLocalVerFile()
{
//把本地版本文件更新为网络同步
FileWriter verOS = null;
BufferedWriter bw = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
verOS = new FileWriter(LocalVerFileName);
![](/Images/OutliningIndicators/InBlock.gif)
bw = new BufferedWriter(verOS);
bw.write(netVersion);
bw.flush();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
bw.close();
verOS.close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex1)
{
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private String getNowVer()
{
//本地版本文件
File verFile = new File(LocalVerFileName);
![](/Images/OutliningIndicators/InBlock.gif)
FileReader is = null;
BufferedReader br = null;
![](/Images/OutliningIndicators/InBlock.gif)
//读取本地版本
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
is = new FileReader(verFile);
![](/Images/OutliningIndicators/InBlock.gif)
br = new BufferedReader(is);
String ver = br.readLine();
![](/Images/OutliningIndicators/InBlock.gif)
return ver;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (FileNotFoundException ex)
{
msg.append("本地版本文件未找到\n");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex)
{
msg.append("本地版本文件读取错误\n");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
//释放资源
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
br.close();
is.close();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException ex1)
{
}
}
return "";
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void setAttb()
{
//窗体设置
this.setTitle("Auto Update");
this.setSize(200, 150);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
![](/Images/OutliningIndicators/InBlock.gif)
// 窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
CheckUpdate checkupdate = new CheckUpdate();
checkupdate.setVisible(true);
}
}
转载:http://blog.csdn.net/beiouwolf/archive/2006/09/23/1268291.aspx