<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 :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

    JAVA自動(dòng)更新程序

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

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

    本地文件需要一個(gè)ver.txt  此文件內(nèi)容為本地軟件版本號(hào)
    網(wǎng)絡(luò)上我直接在一個(gè)頁面上打印出網(wǎng)絡(luò)存在的版本號(hào)
     例如,這個(gè)例子里,我在 http://XXX.XXX.XXX/AutoUpdate/ver  這里直接打印出版本號(hào)

    源文件: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);
            
            
    //啟動(dòng)更新線程
            new Check(msg, process).start();
        }


        
    private class Check extends Thread {
            
    //標(biāo)識(shí),是否存在新的更新文件
            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)識(shí)URL
                String versionUrl = "http://XXX.XXX.XXX/AutoUpdate/ver";

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

    */


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

                
    //讀取網(wǎng)絡(luò)上的版本號(hào)
                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{
                                
    //讀取并刷新臨時(shí)保存文件
                                fos.write(buffer, 0, size);
                                fos.flush();

                                
    //模擬一個(gè)簡單的進(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");

                        
    //把下載的臨時(shí)文件替換原有文件
                        CopyFile(oldFile,newFile);
                        
                        
    //把本地版本文件更新為網(wǎng)絡(luò)同步
                        UpdateLocalVerFile();

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

                    }

                }


                
    //啟動(dòng)應(yīng)用程序
                try {
                    msg.append(
    "啟動(dòng)應(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(
    "本地版本文件讀取錯(cuò)誤\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

    主站蜘蛛池模板: 亚洲情XO亚洲色XO无码| 日韩精品福利片午夜免费观着| 亚洲精品无码高潮喷水在线| 免费亚洲视频在线观看| 一进一出60分钟免费视频| 亚洲 国产 图片| 精品在线免费视频| 国产精品亚洲一区二区三区久久| 免费无码精品黄AV电影| 亚洲国产欧洲综合997久久| 在线观看免费污视频| 久久精品国产亚洲AV| 亚洲电影日韩精品 | 国产自国产自愉自愉免费24区 | 美美女高清毛片视频黄的一免费| 日本无卡码免费一区二区三区| 亚洲乱码中文字幕在线| 啊v在线免费观看| fc2免费人成为视频| 亚洲va无码手机在线电影| 最近中文字幕无免费| 亚洲精品99久久久久中文字幕| 51午夜精品免费视频| 亚洲高清视频在线观看| 无码人妻久久一区二区三区免费丨 | 国内精品免费视频精选在线观看 | 久久午夜羞羞影院免费观看| 67194在线午夜亚洲| 四虎永久成人免费| 一个人免费视频观看在线www| 亚洲久本草在线中文字幕| 处破痛哭A√18成年片免费| 人人鲁免费播放视频人人香蕉| 久久精品国产亚洲av四虎| 免费一本色道久久一区| 一级片在线免费看| 亚洲国产精品日韩在线观看| 又爽又高潮的BB视频免费看| 99re免费在线视频| 理论片在线观看免费| 97久久精品亚洲中文字幕无码|