最近用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();
}
}