单机版桌面应用程序局域网内利用共享盘实现自动更新

基于javaswing的软件,利用局域网共享盘实现的简单的自动更新程序,适合局域网内大量单机版软件更新。

简易程序,不适合C/S、B/S架构。

实现思路:共享盘内保存版本信息和最新jar包,软件读取共享版版本信息文件和自身版本比较,若有最新版本则进行jar包替换重启。

 

 

 

运行:

UpdateProgram update= new UpdateProgram(loginUnit);//版本校验

update.versionVerify();

 

更新程序:

package cn.njmeter.watertest.update;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import cn.njmeter.watertest.ui.LoginUnit;
import cn.njmeter.watertest.ui.UpdateUI;
import cn.njmeter.watertest.utils.ShowUtils;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class UpdateProgram {
 private static LoginUnit loginUnit;
 private static UpdateUI ui=new UpdateUI();
 private static String url;
 private static String javaBin = "jre1.8.0_181_64/bin/java";
 
 public UpdateProgram(LoginUnit login) {
  loginUnit = login;
  url=readIp();
  readJrePath();
 }
 
//从共享盘读取版本信息校验版本,如果版本>软件版本更新
 public void versionVerify() {   
  String versionName="";
  int version=0;
  int currentInt=0;
  InputStream in = null ;
  SAXReader reader = new SAXReader();
  String remotePhotoUrl="smb://"+url+"//My Documents//车间生产软件//shuichejian//测试程序//测试软件更新//version.xml";//存放版本文件,版本信息自定义
  try {
  SmbFile remoteFile = new SmbFile(remotePhotoUrl);
  remoteFile.connect();
  in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
   Document doc =
     reader.read(in);
   Element root =
     doc.getRootElement();
   List<Element> elements =
     root.elements();
   for(Element ele : elements){
    versionName = ele.attributeValue("version");
   }
   versionName=""+versionName.charAt(1)+versionName.charAt(3)+versionName.charAt(5);
   String current = ""+LoginUnit.currentVersion.charAt(1)+LoginUnit.currentVersion.charAt(3)+LoginUnit.currentVersion.charAt(5);
   version=Integer.parseInt(versionName);
   currentInt = Integer.parseInt(current);
  }catch (Exception e) {
   ShowUtils.message("校验版本失败"+e.getMessage());
   return;
  }finally { 
    try { 
           if(in != null) in.close(); 
       }
       catch (Exception e) {}
   }
   if(version>currentInt) {
    ui.setVisible(true); 
   }
  }
 
// 主程序WaterTest.jar  从共享盘读取进行本地替换
 public static String versionUpdate() {
  if(loginUnit!=null) {
   loginUnit.dispose();
   if(LoginUnit.waterMeter!=null) {
    LoginUnit.waterMeter.setVisible(false);
   }
  }
  String result="";
  InputStream in = null ;
  FileOutputStream fos= null;
  BufferedOutputStream bos= null;
  String remotePhotoUrl="smb://"+url+"//My Documents//车间生产软件//shuichejian//测试程序//测试软件更新//WaterTest.jar";
  try {
  SmbFile remoteFile = new SmbFile(remotePhotoUrl);
  if(!remoteFile.exists()) {
   ShowUtils.message("更新文件不存在");
   return "fail";
  }
  remoteFile.connect();
  in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
  fos=new FileOutputStream("WaterTest.jar");
  bos= new BufferedOutputStream(fos);
  int b ;
  while ( (b = in.read()) != -1) {
   bos.write(b);
  }
  bos.flush();
  result="success";
  }catch (Exception e) {
   // TODO: handle exception
  }finally { 
    try { 
           if(in != null) in.close(); 
       }
       catch (Exception e) {}
   }
  return result;
  }
 
//更新完毕利用cmd重启
 public static void restart() {
    final File currentJar = new File("WaterTest.jar");
    /* is it a jar file? */
    if(!currentJar.getName().endsWith(".jar"))
      return;
    if(javaBin==null||"".equals(javaBin)) {
      ShowUtils.message("配置文件jre未设置,请手动重启软件.");
      System.exit(0);
    }
    /* Build command: java -jar application.jar */
    final ArrayList<String> command = new ArrayList<String>();
    command.add(javaBin);
    command.add("-jar");
    command.add(currentJar.getPath());
    final ProcessBuilder builder = new ProcessBuilder(command);
    try {
   builder.start();
  } catch (Exception e2) {
   ShowUtils.message("重启失败,请手动重启-"+e2.getMessage());
   ui.message.setText("重启失败,请关闭本界面手动重启软件.");
   ui.updateButton.setEnabled(false);
   ui.cancelButton.setEnabled(false);
   return;
  }
     System.exit(0);
 }
 
//依赖jar包校验
 public static void libVerify() {
  String libUrl="smb://"+url+"//My Documents//车间生产软件//shuichejian//测试程序//测试软件更新//WaterTest_lib/";
  File localLib = new File("WaterTest_lib");
  Collection<String> localList = new ArrayList<String>();
  if(localLib.exists()) {
   File[] subs = localLib.listFiles();
   for(int i=0;i<subs.length;i++) {
    localList.add(subs[i].getName());
   }
  }
  
  SmbFile remoteLib =null;
  try {
   remoteLib = new SmbFile(libUrl);
  } catch (MalformedURLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
  try {
   if(remoteLib.exists()) {
    SmbFile[] subs = remoteLib.listFiles();
    for(int i=0;i<subs.length;i++) {
     String name=subs[i].getName();
     if(!localList.contains(name)) {
      libUpdate(name);
     }
    }
   }
   
  } catch (SmbException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
//依赖jar包更新
 public static String libUpdate(String name) {
  String libUrl="smb://"+url+"//My Documents//车间生产软件//shuichejian//测试程序//测试软件更新//WaterTest_lib//"+name;
  String result="";
  InputStream in = null ;
  FileOutputStream fos= null;
  BufferedOutputStream bos= null;
  try {
  SmbFile remoteFile = new SmbFile(libUrl);
  if(!remoteFile.exists()) {
   return "fail";
  }
  remoteFile.connect();
  in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
  fos=new FileOutputStream("WaterTest_lib//"+name);
  bos= new BufferedOutputStream(fos);
  int b ;
  while ( (b = in.read()) != -1) {
   bos.write(b);
  }
  bos.flush();
  result="success";
  }catch (Exception e) {
   // TODO: handle exception
  }finally { 
    try { 
           if(in != null) in.close(); 
       }
       catch (Exception e) {}
   }
  return result;
 }
 
//访问共享盘有两种方式,外网和内网访问,依据当前电脑网络情况选择,此测试软件ip从数据库配置文件获取,读者自行实现。
 public static String readIp() {
  String url="192.168.2.253";
  Properties p=new Properties();
  File db = new File("C:"+File.separator+"Config"+File.separator+"WaterTestConfig"+File.separator+"db.properties");
  if(db.exists()) {
   try {
    p.load(new FileInputStream(db));
    url= p.getProperty("jdbc.url");
   
    int start=url.indexOf("//");
    
    url=url.substring(start+2);
   
    int end=url.indexOf(':');
    
    url = url.substring(0,end);
   
    
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  return url;
 }
//获取jre路径,有些软件jre不在安装目录下,jre不能写死,会导致重启失败,jre信息保存在配置文件中
 public static void readJrePath() {
  Properties p=new Properties();
  File db = new File("systemPros.properties");
  if(db.exists()) {
   try {
    p.load(new FileInputStream(db));
    javaBin= p.getProperty("javaBin");
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}
 
//提示界面
public class UpdateUI extends JFrame {
 private JPanel contentPane;
 private UpdateUI ui;
 public JButton updateButton;
 public JButton cancelButton;
 public JLabel message;
 /**
  * Create the frame.
  */
 public UpdateUI() {
  getContentPane().setFont(new Font("微软雅黑", Font.PLAIN, 12));
  ui=this;
  initialize();
 }

 public static void main(String[] args) {
  JFrame val=new UpdateUI();
  val.setVisible(true);
 }

  private void initialize() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(300, 300, 350, 200);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  contentPane.setLayout(new BorderLayout(0, 0));
  this.getContentPane().setLayout(null); 
  message = new JLabel("检测到最新版本,是否更新软件?");
  message.setFont(new Font("微软雅黑", Font.PLAIN, 16));
  message.setBounds(36, 32, 265, 43);
  getContentPane().add(message);
  
  updateButton = new JButton("更新");
  updateButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
  updateButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    message.setText("正在更新,请稍等...");
    UpdateProgram.libVerify();
    String result = UpdateProgram.versionUpdate();
  
    if("success".equals(result)) {
     message.setText("更新成功,一秒后重启...");
     try {
      Thread.sleep(1000);
     } catch (InterruptedException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
     }
     UpdateProgram.restart();
    }
   }
  });
  updateButton.setBounds(57, 100, 86, 36);
  getContentPane().add(updateButton);
  
  cancelButton = new JButton("取消");
  cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
  cancelButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    ui.setVisible(false);
   }
  });
  cancelButton.setBounds(178, 100, 84, 36);
  getContentPane().add(cancelButton);
 }
}
posted @ 2020-06-12 16:46  月下独酌一杯酒  阅读(611)  评论(0)    收藏  举报