<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 沙漠中的魚 閱讀(4298) 評論(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

    主站蜘蛛池模板: ww亚洲ww在线观看国产| 亚洲人成国产精品无码| 亚洲精品国产福利片| 中文字幕免费在线看电影大全 | 亚洲第一综合天堂另类专| 2021久久精品免费观看| 亚洲黄色免费在线观看| 91久久精品国产免费一区| 自怕偷自怕亚洲精品| 16女性下面扒开无遮挡免费| 日韩精品一区二区亚洲AV观看 | 亚洲综合精品成人| 成人免费视频软件网站| 亚洲一级毛片免观看| 成年女性特黄午夜视频免费看| 亚洲人成77777在线播放网站不卡| 97视频热人人精品免费| 有色视频在线观看免费高清在线直播 | 亚洲自偷自拍另类图片二区| 成人精品一区二区三区不卡免费看| 亚洲国产电影av在线网址| 久久亚洲AV成人无码国产电影| 女人毛片a级大学毛片免费| 男女啪啪永久免费观看网站| 亚洲精品成人久久| 最近2019年免费中文字幕高清| 亚洲AV无码精品色午夜果冻不卡| 久久九九久精品国产免费直播 | 色婷婷7777免费视频在线观看| 91情国产l精品国产亚洲区 | 久久亚洲精品AB无码播放| 黄网站免费在线观看| 亚洲国产一二三精品无码| 中文字幕免费观看全部电影| 国产亚洲3p无码一区二区| 大地资源中文在线观看免费版| 亚洲国产精品成人精品无码区 | 免费毛片a线观看| 亚洲精品美女在线观看播放| 国产卡一卡二卡三免费入口| 香蕉大伊亚洲人在线观看|