前段時(shí)間做了一個(gè)工具發(fā)布給公司的各部門使用后反饋了不少BUG,每次修改后均需要發(fā)郵件通知各用戶替換最新版本,很不方便,因此后來就寫了一個(gè)自動(dòng)升級(jí)的功能,這樣每次發(fā)布新的版本時(shí)只需要將其部署到自動(dòng)升級(jí)服務(wù)器上,工具使用用戶運(yùn)行工具時(shí)就會(huì)連接到自動(dòng)升級(jí)服務(wù)器,檢查是否有版本更新,如果有則完成更新后再運(yùn)行最新版本,否則就運(yùn)行當(dāng)前工具版本。
為了使這個(gè)自動(dòng)升級(jí)模塊具有通用性,我將其做成可以單獨(dú)運(yùn)行的程序,而并非集成到工具中,這樣則可以為各類軟件提供自動(dòng)升級(jí)的功能。自動(dòng)升級(jí)模塊采用SOCKET方式實(shí)現(xiàn)升級(jí)客戶端與服務(wù)端的交互,通過版本控制文件autoupdate.xml來控制版本的更新,完成自動(dòng)更新后會(huì)將歷史清單history.htm也發(fā)送給客戶端,并自動(dòng)打開該文件,使用戶可以對本次升級(jí)的具體內(nèi)容一目了然。
詳細(xì)實(shí)現(xiàn):
autoupdate.xml具體內(nèi)容:
1
<?xml version="1.0" encoding="GBK"?>
2
<Info>
3
<Version>1.0.3</Version>
4
<UpdateServer>
5
<Ip>122.2.14.212</Ip>
6
<Port>2110</Port>
7
</UpdateServer>
8
<Files>
9
<File>
10
<Name>mftest.jar</Name>
11
<Path>.\bin</Path>
12
<SubVer>1.0.0.3</SubVer>
13
</File>
14
<File>
15
<Name>run.bat</Name>
16
<Path>.</Path>
17
<SubVer>1.0.0.3</SubVer>
18
</File>
19
<File>
20
<Name>eglreco.fix</Name>
21
<Path>.\config\resource</Path>
22
<SubVer>1.0.0.0</SubVer>
23
</File>
24
<File>
25
<Name>eglrec.fix</Name>
26
<Path>.\config\resource</Path>
27
<SubVer>1.0.0.0</SubVer>
28
</File>
29
<File>
30
<Name>dom4j-1.6.1.jar</Name>
31
<Path>.\lib</Path>
32
<SubVer>1.0.0.0</SubVer>
33
</File>
34
<File>
35
<Name>jaxen-1.1-beta-4.jar</Name>
36
<Path>.\lib</Path>
37
<SubVer>1.0.0.0</SubVer>
38
</File>
39
<File>
40
<Name>jxl.jar</Name>
41
<Path>.\lib</Path>
42
<SubVer>1.0.0.0</SubVer>
43
</File>
44
</Files>
45
</Info>
其中UpdateServer用戶描述服務(wù)端的IP和端口;Version為軟件的大版本號(hào),F(xiàn)iles為軟件涉及到的文件,其中的SubVer為具體文件的版本號(hào),其他就顧名思義了。
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

history.htm具體內(nèi)容:
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html>
3
<head>
4
<title>XXX軟件更新歷史</title>
5
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
6
<style type="text/css">
7
BODY,TABLE {
8
FONT-SIZE: 12px;
9
COLOR: #666666;
10
FONT-FAMILY: 宋體;
11
background-color: #ffffff;
12
line-height: 160%;}
13
</style>
14
</head>
15
16
<body>
17
<table width="600" border="1" align="center" cellpadding="2" cellspacing="0">
18
<tr bgcolor="#ECECEC">
19
<td align="center" width=50>版本</td>
20
<td align="center" width=470>更新內(nèi)容</td>
21
<td align="center" width=80>日期</td>
22
</tr>
23
<tr>
24
<td align="center" >1.0.3</td>
25
<td ><p>1.實(shí)現(xiàn)自動(dòng)升級(jí)功能<br>
26
2.增加版本號(hào)顯示<br>
27
3.修正
.<br>
28
</p>
29
</td>
30
<td align="center" >2008-12-25</td>
31
</tr>
32
<tr>
33
<td align="center" >1.0.2</td>
34
<td ><p>1.修正了
等字符而無法正確生成的錯(cuò)誤<br>
35
2.修正了
無法生成的錯(cuò)誤<br>
36
</p>
37
</td>
38
<td align="center" >2008-12-20</td>
39
</tr>
40
<tr>
41
<td align="center" >1.0.1</td>
42
<td ><p>1.增加通過
<br>
43
2.修正因?yàn)?img alt="" src="http://m.tkk7.com/Images/dot.gif" />的錯(cuò)誤<br>
44
</p>
45
</td>
46
<td align="center" >2008-12-15</td>
47
</tr>
48
<tr>
49
<td align="center" >1.0.0</td>
50
<td ><p>1.實(shí)現(xiàn)通過
.功能<br>
51
2.實(shí)現(xiàn)通過
.功能<br>
52
</p>
53
</td>
54
<td align="center" >2008-12-11</td>
55
</tr>
56
</table>
57
</body>
58
</html>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27


28

29

30

31

32

33

34


35


36

37

38

39

40

41

42


43

44

45

46

47

48

49

50


51


52

53

54

55

56

57

58

工作原理:
自動(dòng)升級(jí)客戶端首先會(huì)將本地的autoupdate.xml內(nèi)容發(fā)送給服務(wù)端,服務(wù)端收到客戶端的版本信息后與服務(wù)端本地的版本信息(autoupdate.xml)進(jìn)行比較,首先比較Version,如果一致則通知客戶端無需更新,如果不一致則檢查Files中各文件的SubVer,服務(wù)端將SubVer不一致的文件發(fā)送給客戶端,對于服務(wù)端有而客戶端沒有的文件也需要發(fā)送給客戶端,處理完所有File后,服務(wù)端將本地的history.htm與傳送給客戶端,并通知客戶端更新完畢;客戶端收到更新完畢后的應(yīng)答后自動(dòng)打開history.htm,將更新歷史信息顯示給用戶查看。