java c/s版本更新实现

最近正在修改一个c/s的项目,为了方便要做一个版本更新的功能,毕竟每次修改都要重新安装浪费时间

开始也没什么好的思路,毕竟也没怎么做过c/s的项目,在网上看到有版本更新的博客,修改代码,自己慢慢尝试也总算是做了出来

我以为c/s更新需要把之前的卸载重新安装,之后发现我错了,只要把安装之后的文件夹里的.exe文件替换了就算是更新了,如果有配置文件那么需要把改动后的配置文件也替换了

之后我以为直接把.exe文件下载到文件夹里替换就行了,后来有点问题,首先把文件下载到download文件夹里,之后在执行.bat脚本进行文件的替换

代码

package kh.other;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import kh.util.FileUtils;
/*
 * 版本更新功能实现
 */
public class CheckUpdate
  extends JFrame
{
  private static final long serialVersionUID = 1L;
  JFrame c = this;
  
  public CheckUpdate()
  {
    setAttb();
    this.setVisible(true);
    JLabel title = new JLabel("正在检查网络上的更新资源...");
    add(title, "North");
    JTextArea msg = new JTextArea();
    add(msg, "Center");
    JLabel process = new JLabel();
    add(process, "South");
 
    new Check(msg, process).run();
  }
  
  private class Check
  {
    private boolean isUpdated = false;
    String netVersion;
    String localVerFileName = "xxx.txt";
    String localSystem;
    String localType;
    private JTextArea msg;
    private JLabel process;
    
    public Check(JTextArea msg, JLabel process)
    {
      this.msg = msg;
      this.process = process;
    }
    
    private void run() {
         //更新文件版本标识URL  
        String versionUrl = "http://197.100.100.110:8080/xxx/xxx/xxx";  
        /**//* 
        这里是通过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();
            System.out.println("读取baseapp版本文件内容:"+netVerStr);
            String localVerStr  = getNowVer();
            System.out.println("读取本地版本内容:"+localVerStr);
            if(netVerStr.equals(localVerStr)){
                System.out.println("当前最新版本");
                msg.append("当前时最新版本");
                isUpdated = false;
            }else{
                System.out.println("存在更新文件是否更新");
                msg.append("存在更新文件,现在开始更新\n");
                isUpdated = true;
                netVersion = netVerStr;
                
            }
            
            
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            try {
                netVer.close();
                isr.close();
                is.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
        //如果版本号不同,下载网络上的文件更新本地文件
        if(isUpdated){
            //本地需要被更新的文件  
            File oldFile = new File(System.getProperty("user.dir")+File.separator+"SingleLogin.exe");       
             File oldPropertyFile = new File(System.getProperty("user.dir")+File.separator+"download"+File.separator+"config.properties"); 
              //缓存网络上下载的文件的位置 
            File newFile = new File(System.getProperty("user.dir")+File.separator+"SingleLogin.exe"); 
            File newPropertyFile = new File(System.getProperty("user.dir")+File.separator+"download"+File.separator+"config.properties"); 
                   
             String filePath = System.getProperty("user.dir")+File.separator+"download";
             System.out.println("文件夹路径:"+filePath);
             //文件夹不存在创建文件夹
             isChartPathExist(filePath);
             
             String updateUrl = null;
             String updateUrl2 = null;
         //网络上的文件位置  
             updateUrl =  "http://197.100.100.110:8080/xxx/xxx/xxx/xxx.exe"; 
             updateUrl2 = "http://197.100.100.110:8080/xxx/xxx/xxx/xxx.properties";
                 
            //下载文件
             openURLload(updateUrl, newFile, process, msg);
             openURLload(updateUrl2, newPropertyFile, process, msg);

             UpdateLocalVerFile();  
             //启动应用程序
             try {
                    msg.append("启动应用程序");  
                    Thread.sleep(500);  
                    Runtime.getRuntime().exec("cmd.exe   /C   start   /b   %cd%/xxx.bat");
                    System.exit(0);
                   } catch (Exception e) {
                   // TODO: handle exception
               }
            
        }
              
       
    }
   
    
    private void UpdateLocalVerFile() {  
         //把本地版本文件更新为网络同步  
        FileWriter verOS = null;  
        BufferedWriter bw = null;  
        try {
            verOS = new FileWriter(localVerFileName); 
            
             bw = new BufferedWriter(verOS);  
             bw.write(netVersion);  
             bw.flush();  
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            try {
                bw.close();  
                verOS.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
        
    }
    
    private String getNowVer() { 
        //本地版本文件  
        File verFile = new File(localVerFileName);
        
        FileReader is = null;  
        BufferedReader br = null;  
        String ver = null;
      //读取本地版本  
        try {
            is = new FileReader(verFile);  
              
            br = new BufferedReader(is);  
            ver = br.readLine(); 
            System.out.println("读取本地文件。。。。。。。。。。内容:"+ver);
            return ver;
        } catch (FileNotFoundException ex) {
             msg.append("本地版本文件未找到\n");  
        }catch (IOException ex ) {
             msg.append("本地版本文件读取错误\n");
        }finally{
            //释放资源  
            try {
                br.close();  
                is.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return ver;  
    }
     
  }
  
  private void setAttb() {
      System.out.println("加载窗体。。。。。。");
      //窗体设置  
      this.setTitle("Auto Update");  
      this.setSize(200, 150);  
      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);  
  }
  
  private static void isChartPathExist(String dirPath) {        
      File file = new File(dirPath);        
      if (!file.exists()) {            
          file.mkdirs();        
      }    
  } 
  
  //下载文件
  public static void openURLload(String url, File loadFile ,JLabel process,JTextArea msg){
        URL url2 = null;
        HttpURLConnection httpurl = null;
        InputStream is = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        try {
            url2 = new URL(url);
            httpurl = (HttpURLConnection) url2.openConnection();
            httpurl.connect();
            
            byte[] buffer = new byte[1024];  
          
          int size = 0; 
          int flag = 0;  
          int flag2 = 0; 
          is = httpurl.getInputStream();  
          bis = new BufferedInputStream(is); 
          fos = new FileOutputStream(loadFile);
          
          msg.append("正在从网络上下载新的更新文件\n"); 
          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 e) {
            msg.append("文件读取错误\n");
            e.printStackTrace();
        }finally{
               
           try {
               fos.close();
               bis.close();
                 is.close();  
                 httpurl.disconnect();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
           
        }
     
    }


  public static void main(String[] args) {

       new CheckUpdate();  
  }
  
}

 

.bat脚本

@echo off 
copy /y "%CD%\download\xxx.exe" "%CD%\xxx.exe" 
copy /y "%CD%\download\xxx.properties" "%CD%\xxx.properties" 
start "" "%CD%\SingleLogin.exe" 

之后每次启动检测更新,有更新自动更新为新版本

CheckUpdate checkUpdate = new CheckUpdate();
checkUpdate.dispose();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LouD_dm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值