<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    沙漠中的魚

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

    JAVA自動更新程序

    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(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);
        }

    }


     

    轉載:http://blog.csdn.net/beiouwolf/archive/2006/09/23/1268291.aspx

    主站蜘蛛池模板: 亚洲精品无码av中文字幕| 永久免费观看的毛片的网站| 婷婷亚洲综合五月天小说在线| 久久久无码精品亚洲日韩蜜桃 | 亚洲人成无码网站| 午夜小视频免费观看| 9420免费高清在线视频| j8又粗又长又硬又爽免费视频| 亚洲精品久久久久无码AV片软件| 久久综合亚洲色HEZYO社区| 亚洲日韩精品一区二区三区| 吃奶摸下高潮60分钟免费视频| 性色av无码免费一区二区三区| 亚洲精品国产免费| 久草视频在线免费看| 中文在线观看永久免费| 一级毛片免费播放视频| 精品亚洲视频在线| 国产亚洲一卡2卡3卡4卡新区| 亚洲最大无码中文字幕| 亚洲码一区二区三区| 99久久精品国产亚洲| 国产精品久久久亚洲| 国产l精品国产亚洲区在线观看| 亚洲伊人久久综合中文成人网| 一本色道久久88综合亚洲精品高清| 免费观看一级毛片| 免费观看理论片毛片| 日本a级片免费看| 老司机永久免费网站在线观看| 啦啦啦在线免费视频| 在线A级毛片无码免费真人| 成人免费看黄20分钟| 成年女人免费v片| 在线免费不卡视频| 麻豆国产人免费人成免费视频 | 亚洲伊人久久大香线焦| 亚洲av午夜精品无码专区| 亚洲性一级理论片在线观看| 国产成人精品日本亚洲专一区| 一本色道久久88—综合亚洲精品 |