最近用swing写了一个客户端工具(以jar的方式),下面是写的jar自动更新程序。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JOptionPane;
public class AutoUpdateThread extends Thread {
private String filename = "test.jar";
private static String xiheportalURL = "http://localhost:8080/test/testJarAction";
public static void main(String[] arg) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
UpdateJFrame uj = null;
String message = null;
String title = null;
try {
uj = new UpdateJFrame();//提示用户正在更新,更新完成自动关闭
uj.setLocationRelativeTo(null);//居中显示
uj.setResizable(false);
uj.setVisible(true);
byte[] buffer = getXiheJarFormServer();//自动下载最新的jar包
if (buffer == null) {
message = "更新失败!原因:从XXXX上下载jar包失败!";
title = "Error";
} else {
autoUpdate(buffer);//替换本地jar包
message = "更新成功! 重新启动?";
title = "Message";
}
} catch (Exception e) {
message = "更新失败!原因:" + e.getMessage();
title = "Error";
e.printStackTrace();
} finally {
if (uj != null) {
uj.dispose();
}
int i = JOptionPane.showConfirmDialog(null, message, title,
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
if (i == 0) {
reStartXihetool();
}
}
}
public void reStartXihetool() {//重新运行新版jar包
try {
File dir = new File("");
String basepath = dir.getAbsolutePath();
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java -cp \"" + basepath + "/" + filename + "\" com.test skipUpdate");// com.test为jar主类
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] getXiheJarFormServer() throws Exception {
URL target = new URL(xiheportalURL);
HttpURLConnection conn = (HttpURLConnection) target.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Accept", "text/xml");
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.connect();
// check response code
int code = conn.getResponseCode();
boolean success = (code >= 200) && (code < 300);
byte[] buffer = null;
if (success) {
InputStream in = conn.getInputStream();
int size = conn.getContentLength();
System.out.println("size:" + size);
buffer = new byte[size];
int curr = 0, read = 0;
while (curr < size) {
read = in.read(buffer, curr, size - curr);
if (read <= 0) {
break;
}
curr += read;
}
// System.out.println(new String(buffer));
in.close();
}
return buffer;
}
public void autoUpdate(byte[] buffer) throws Exception {
File dir = new File("");
String basepath = dir.getAbsolutePath();
String newfilepath = basepath + "/" + filename;
System.out.println("newfilepath:" + newfilepath);
File newfile = new File(newfilepath);
OutputStream ops = new BufferedOutputStream(new FileOutputStream(newfile));
ops.write(buffer);
ops.close();
}
}
分享到:
相关推荐
Java 自动加载JAR文件并运行其中的类方法
脚本上有备注,将java项目打为jar包后,替换jar的包名为项目包名即可,log文件会自动生成,方便好用。
selenium-java.jar 自动化jar包,适用于 windows, mac系统
自动打包.java的文件成.jar 注意.java文件里面不能带package!
java-client-3.2.0-javadoc;java-client-3.2.0-sources;java-client-3.2.0;selenium-server-standalone-2.53.0;四个jar包;用于appium,自动化开发
java selenium自动化测试jar一套软件工具,用来支持不同的自动化测试方法 开源软件:可以根据需要来增加重构工具的某些功能 跨平台:linux , windows mac 核心功能就是可以在多个浏览器上进行自动化测试 支持多种...
NULL 博文链接:https://sky-xin.iteye.com/blog/2289726
基于springboot的自动更新 具体描述见 https://blog.csdn.net/qq_32969313/article/details/113940902
调用shell的ssh2工具包,websocket工具包
描述:java开发多个微服务系统,jar包方式运行,windows服务器部署,去除小黑框,改为log日志输出,设置开机自动启动。 相关技术文档:...
eclipse插件 可打包增量文件,可一键将增量文件更新至测试环境且自动重启 一、使用条件 1、eclipse (eclipse 4.0以上) 2、版本管理用的svn 二、使用方法 1、将下载的jar 放入eclipse\dropins下 2、重启eclipse ...
这个bat脚本资源可以在Windows环境下实现Java程序的守护进程和自动重启。它通过一个循环监控Java程序进程,如果检测到程序异常退出,会自动重启程序。同时具有可配置的重启次数和重启间隔时间。 这个小巧但实用的bat...
Appium 自动化测试所需的相关ja包,有需要的可以进行下载。
能实现简单的自动发送邮件功能,而且所带的jar文件都有,能够马上运行。方便快捷,你懂的!
POI Java自动转换word 所需Jar包 及其DEmo
javaFx自动升级布署工具,用ant构建,根据生成的app.xml比对更新
网上下载的一些java代码jar仅供学习参考!
三个Eclipse插件可以去掉多余的java项目中jar包,有操作图示。
将java项目的编辑码打通过cmd 窗口打包成可点击自动运行的JAR包,附件里的readme.txt文件说明了整个打包过程的步骤