沙漠中的魚

          欲上天堂,先下地獄
          posts - 0, comments - 56, trackbacks - 0, articles - 119
            BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

          JAVA自動更新程序

          Posted on 2008-09-01 16:17 沙漠中的魚 閱讀(4302) 評論(0)  編輯  收藏 所屬分類: Java

          最近由于一個工程需要做應(yīng)用程序啟動時,自動更新的項目
          在GOOGLE上找了半天也沒見到什么比較好的辦法
          自己動手寫了一個通過版本號檢查網(wǎng)絡(luò)上是不是存在新的更新文件,并自動通過HTTP下載文件的程序
          希望對正在找此類程序的朋友有幫助

          本地文件需要一個ver.txt  此文件內(nèi)容為本地軟件版本號
          網(wǎng)絡(luò)上我直接在一個頁面上打印出網(wǎng)絡(luò)存在的版本號
           例如,這個例子里,我在 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() {
                  
          //設(shè)置窗體屬性
                  setAttb();

                  JLabel title 
          = new JLabel("正在檢查網(wǎng)絡(luò)上的更新資源");
                  
          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 {
                  
          //標(biāo)識,是否存在新的更新文件
                  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() {
                      
          //更新文件版本標(biāo)識URL
                      String versionUrl = "http://XXX.XXX.XXX/AutoUpdate/ver";

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

          */


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

                      
          //讀取網(wǎng)絡(luò)上的版本號
                      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(
          "當(dāng)前文件是最新版本\n");
                              isUpdated 
          = false;
                          }
           else {
                              msg.append(
          "存在更新文件,現(xiàn)在開始更新\n");
                              isUpdated 
          = true;
                              netVersion 
          = netVerStr;
                          }


                      }
           catch (MalformedURLException ex) {
                      }
           catch (IOException ex) {
                      }
           finally {
                          
          //釋放資源
                          try {
                              netVer.close();
                              isr.close();
                              is.close();
                          }
           catch (IOException ex1) {
                          }

                      }


                      
          //如果版本不同,下載網(wǎng)絡(luò)上的文件,更新本地文件
                      if (isUpdated) {
                          
          //本地需要被更新的文件
                          File oldFile = new File("client.exe");
                          
          //緩存網(wǎng)絡(luò)上下載的文件
                          File newFile = new File("temp.exe");
                          
                          
          //網(wǎng)絡(luò)上的文件位置
                          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(
          "正在從網(wǎng)絡(luò)上下載新的更新文件\n");

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

                                      
          //模擬一個簡單的進(jìn)度條
                                      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);
                              
                              
          //把本地版本文件更新為網(wǎng)絡(luò)同步
                              UpdateLocalVerFile();

                          }
           catch (MalformedURLException ex2) {
                          }
           catch (IOException ex) {
                              msg.append(
          "文件讀取錯誤\n");
                          }
           finally {
                              
          try {
                                  fos.close();
                                  bis.close();
                                  is.close();
                                  httpUrl.disconnect();
                              }
           catch (IOException ex3) {
                              }

                          }

                      }


                      
          //啟動應(yīng)用程序
                      try {
                          msg.append(
          "啟動應(yīng)用程序");
                          Thread.sleep(
          500);
                          Process p 
          = Runtime.getRuntime().exec("client.exe");
                      }
           catch (IOException ex5) {
                      }
           catch (InterruptedException ex) {
                      }

                      
                      
          //退出更新程序
                      System.exit(0);
                  }

          //復(fù)制文件
                  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() {
                      
          //把本地版本文件更新為網(wǎng)絡(luò)同步
                      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() {
                  
          //窗體設(shè)置
                  this.setTitle("Auto Update");
                  
          this.setSize(200150);
                  
          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);
              }

          }


           

          轉(zhuǎn)載:http://blog.csdn.net/beiouwolf/archive/2006/09/23/1268291.aspx

          主站蜘蛛池模板: 黄陵县| 溧阳市| 准格尔旗| 香格里拉县| 灌南县| 丹江口市| 定兴县| 纳雍县| 洛宁县| 凭祥市| 健康| 肇源县| 卫辉市| 曲阜市| 拉萨市| 大竹县| 儋州市| 绥阳县| 高州市| 永兴县| 黄山市| 高唐县| 浙江省| 资讯 | 苏尼特右旗| 鄯善县| 民勤县| 临汾市| 买车| 新龙县| 临沧市| 将乐县| 新乐市| 正阳县| 无棣县| 康马县| 兴隆县| 无锡市| 新兴县| 南木林县| 屯门区|