加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Java程序自动更新本身应用程序的方法

(2014-12-11 20:11:51)
标签:

java

自动更新

本身

代码

例子

分类: IT技术
我们的目的是:有一个CheckUpdate.jar的可执行程序,我们希望它能实现自动更新。
具体的代码如下:

package autoupdate;

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.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

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

public class CheckUpdate extends JFrame{
private static final long serialVersionUID = 1L;
JFrame c = this;
public CheckUpdate(){
setAttb();
JLabel title = new JLabel("正在检查网络上的更新资源...");
    this.add(title, BorderLayout.NORTH);
    JTextArea msg = new JTextArea();
    this.add(msg, BorderLayout.CENTER);
    JLabel process = new JLabel();
    this.add(process, BorderLayout.SOUTH);
    
    new Check(msg, process).start();
}

private void setAttb() {
// TODO Auto-generated method stub
this.setTitle("Auto Update");
        this.setSize(400, 350);
        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  class Check extends Thread{
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(){
String versionUrl = "http://cooperator-ro.oss-cn-hangzhou.aliyuncs.com/CertTest/test/ver.txt";
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();
String localVerStr = getNowVer();
if(netVerStr.equals(localVerStr)){
msg.append("当前文件是最新版本\n");
isUpdated = false;
}else{
msg.append("存在更新文件,现在开始更新...\n");
isUpdated = true;
netVersion = netVerStr;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
netVer.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(isUpdated){
File oldFile = new File("CheckUpdate.jar");
File newFile = new File("temp.jar");
String updateUrl = "http://cooperator-ro.oss-cn-hangzhou.aliyuncs.com/CertTest/test/CheckUpdate.jar";
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
try {
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("正在从网络上下载新的更新文件\n");
int flag = 0;
int flag2 = 0;
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 (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
msg.append("\n文件下载完成\n");
String path = this.getClass().getResource("/").getPath(); 
System.out.println(path);
CopyFile(oldFile, newFile);
UpdateLocalVerFile();
executeCommand("java -jar temp.jar");
System.exit(0);
}else{
executeCommand("cmd.exe /c del temp.jar");
System.out.println("hello");
}
}

private void UpdateLocalVerFile() {
FileWriter verOS = null;
BufferedWriter bw = null;
try {
verOS = new FileWriter(LocalVerFileName);
bw = new BufferedWriter(verOS);
bw.write(netVersion);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bw.close();
verOS.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void executeCommand(String command){
   
       Runtime r = Runtime.getRuntime();
       try {
r.exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       

   }
private void CopyFile(File oldFile, File newFile) {
FileInputStream in = null;
FileOutputStream out = null;
if(oldFile.exists()){
oldFile.delete();
}
try {
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 e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private String getNowVer() {
// TODO Auto-generated method stub
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 e) {
// TODO Auto-generated catch block
e.printStackTrace();
msg.append("本地版本文件未找到\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
msg.append("本地版本文件读取错误\n");
}finally{
try {
br.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
CheckUpdate checkupdate = new CheckUpdate();
    checkupdate.setVisible(true);
}

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有