//很早以前的工程项目,比较乱,凑合看吧,希望对你有一点点帮助
//updateDaemonThread
//看了源码发现我记错了,这个线程不是轮循,是加在main.bat里的第2句,第一句就是主app
//主app关闭了,才会执行第2句,比轮循更好一点
import java.io.*;
import java.util.Properties;
public class UpdateDaemonThread extends Thread {
String currentPath;
String version;
boolean quit = false;
public void run() {
try {
File path = new File(".");
currentPath = path.getAbsolutePath().substring(0,
path.getAbsolutePath().length() - 1);
SystemProperties.getProperties();
} catch (Exception ex1) {
}
try {
Thread.sleep(1000);
if (SystemProperties.props.getProperty("update").equals("true")) {
version=getProperties("./updates/update.ini");
SystemProperties.props.setProperty("update", "false");
SystemProperties.props.setProperty("version", version);
SystemProperties.setProperties();
System.out.println("false");
String command = "cmd.exe /c " + currentPath + "//main.bat";
Process process = Runtime.getRuntime().exec(command);
BufferedReader output = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while (output.readLine() != null) {
System.out.println(output.readLine());
}
quit = true;
}
System.exit(0);
} catch (Exception ex) {
}
}
public String getProperties(String fileName) {
try {
Properties props = new Properties();
FileInputStream is = new FileInputStream(fileName);
props.load(is);
return props.getProperty("version");
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
UpdateDaemonThread updatedaemonthread = new UpdateDaemonThread();
updatedaemonthread.start();
}
}
//update.java 太长了删掉一些没用的代码
import java.io.*;
import java.net.*;
import java.sql.*
import java.util.*;
import javax.swing.*;
import org.apache.commons.net.ftp.*;
public class Updater {
String ip;
int port = 21;
String username;
String psw;
String dir = "";
String newVersion, oldVersion;
MainFrame parent;
int mode;
public Updater(MainFrame mainFrame, int mode) {
this.parent = mainFrame;
this.mode = mode;
}
public void start() {
init();
update();
}
public void init() {
try {
SystemProperties.getProperties();
} catch (Exception e) {
JOptionPane.showMessageDialog(parent, "Çë¼ì²éÉý¼¶·þÎñÆ÷ÅäÖÃÐÅÏ¢£¡", "´íÎóÐÅÏ¢",
JOptionPane.ERROR_MESSAGE);
}
ip = SystemProperties.props.getProperty("serverAddress");
username = SystemProperties.props.getProperty("user");
psw = SystemProperties.props.getProperty("psw");
}
private void update() {
oldVersion = getProperties("./Config/Sysetm.cfg");
if (downLoad("update.ini")) {
newVersion = getProperties("./updates/update.ini");
if (newVersion != null) {
if (!newVersion.equals(oldVersion)) {
if (downLoad("main.jar")) {
JOptionPane.showMessageDialog(parent,
"¸üÐÂÍê³É£¬ÇëÍ˳ö³ÌÐòÖØÐÂÆô¶¯", "ÌáʾÐÅÏ¢",
JOptionPane.INFORMATION_MESSAGE);
// дÅäÖÃÎļþ£¬Í˳ö³ÌÐò
SystemProperties.props.setProperty("version", newVersion);
SystemProperties.props.setProperty("update", "true");
try {
SystemProperties.setProperties();
} catch (Exception e) {
}
parent.exit();
}
} else {
JOptionPane.showMessageDialog(parent, "ÒѾ­ÊÇ×îа汾£¬²»ÐèÒª¸üУ¡",
"¾¯¸æÐÅÏ¢", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(parent,
"ÏÂÔØÎļþ¹ý³ÌÖгö´í£¬ÇëÖØÊÔ»òÁªÏµ·þÎñÆ÷¹ÜÀíÔ±£¡", "¾¯¸æÐÅÏ¢",
JOptionPane.WARNING_MESSAGE);
}
}
}
public boolean downLoad(String fileName) {
System.out.println("Welcome to ftp session");
FTPClient ftp = new FTPClient();
try {
FTPClientConfig conf = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
ftp.configure(conf);
System.out.println("FTP.imag11e=" + FTP.IMAGE_FILE_TYPE);
ftp.connect(ip, 21);
ftp.login(username, psw);
ftp.setFileType(FTP.IMAGE_FILE_TYPE);
System.out.println(ftp.getReplyString());
//ftp.cwd(dir);
File file = new File("./updates/" + fileName);
FileOutputStream out = new FileOutputStream(file);
ftp.retrieveFile(fileName, out);
System.out.println(fileName + " is downloading.....");
out.flush();
out.close();
System.out.println("Download is Over");
return true;
} catch (SocketException e) {
JOptionPane.showMessageDialog(parent, "Á¬½Ó²»µ½Éý¼¶·þÎñÆ÷£¬Çë¼ì²éÍøÂç»òÁªÏµ·þÎñÆ÷¹ÜÀíÔ±£¡",
"¾¯¸æÐÅÏ¢", JOptionPane.WARNING_MESSAGE);
return false;
} catch (IOException e) {
JOptionPane.showMessageDialog(parent, "ÏÂÔØÎļþ¹ý³ÌÖгö´í£¬ÇëÖØÊÔ£¡", "¾¯¸æÐÅÏ¢",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
//This code copy from csdn voxer
//updateDaemonThread
//看了源码发现我记错了,这个线程不是轮循,是加在main.bat里的第2句,第一句就是主app
//主app关闭了,才会执行第2句,比轮循更好一点
import java.io.*;
import java.util.Properties;
public class UpdateDaemonThread extends Thread {
String currentPath;
String version;
boolean quit = false;
public void run() {
try {
File path = new File(".");
currentPath = path.getAbsolutePath().substring(0,
path.getAbsolutePath().length() - 1);
SystemProperties.getProperties();
} catch (Exception ex1) {
}
try {
Thread.sleep(1000);
if (SystemProperties.props.getProperty("update").equals("true")) {
version=getProperties("./updates/update.ini");
SystemProperties.props.setProperty("update", "false");
SystemProperties.props.setProperty("version", version);
SystemProperties.setProperties();
System.out.println("false");
String command = "cmd.exe /c " + currentPath + "//main.bat";
Process process = Runtime.getRuntime().exec(command);
BufferedReader output = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while (output.readLine() != null) {
System.out.println(output.readLine());
}
quit = true;
}
System.exit(0);
} catch (Exception ex) {
}
}
public String getProperties(String fileName) {
try {
Properties props = new Properties();
FileInputStream is = new FileInputStream(fileName);
props.load(is);
return props.getProperty("version");
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) {
UpdateDaemonThread updatedaemonthread = new UpdateDaemonThread();
updatedaemonthread.start();
}
}
//update.java 太长了删掉一些没用的代码
import java.io.*;
import java.net.*;
import java.sql.*
import java.util.*;
import javax.swing.*;
import org.apache.commons.net.ftp.*;
public class Updater {
String ip;
int port = 21;
String username;
String psw;
String dir = "";
String newVersion, oldVersion;
MainFrame parent;
int mode;
public Updater(MainFrame mainFrame, int mode) {
this.parent = mainFrame;
this.mode = mode;
}
public void start() {
init();
update();
}
public void init() {
try {
SystemProperties.getProperties();
} catch (Exception e) {
JOptionPane.showMessageDialog(parent, "Çë¼ì²éÉý¼¶·þÎñÆ÷ÅäÖÃÐÅÏ¢£¡", "´íÎóÐÅÏ¢",
JOptionPane.ERROR_MESSAGE);
}
ip = SystemProperties.props.getProperty("serverAddress");
username = SystemProperties.props.getProperty("user");
psw = SystemProperties.props.getProperty("psw");
}
private void update() {
oldVersion = getProperties("./Config/Sysetm.cfg");
if (downLoad("update.ini")) {
newVersion = getProperties("./updates/update.ini");
if (newVersion != null) {
if (!newVersion.equals(oldVersion)) {
if (downLoad("main.jar")) {
JOptionPane.showMessageDialog(parent,
"¸üÐÂÍê³É£¬ÇëÍ˳ö³ÌÐòÖØÐÂÆô¶¯", "ÌáʾÐÅÏ¢",
JOptionPane.INFORMATION_MESSAGE);
// дÅäÖÃÎļþ£¬Í˳ö³ÌÐò
SystemProperties.props.setProperty("version", newVersion);
SystemProperties.props.setProperty("update", "true");
try {
SystemProperties.setProperties();
} catch (Exception e) {
}
parent.exit();
}
} else {
JOptionPane.showMessageDialog(parent, "ÒѾ­ÊÇ×îа汾£¬²»ÐèÒª¸üУ¡",
"¾¯¸æÐÅÏ¢", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(parent,
"ÏÂÔØÎļþ¹ý³ÌÖгö´í£¬ÇëÖØÊÔ»òÁªÏµ·þÎñÆ÷¹ÜÀíÔ±£¡", "¾¯¸æÐÅÏ¢",
JOptionPane.WARNING_MESSAGE);
}
}
}
public boolean downLoad(String fileName) {
System.out.println("Welcome to ftp session");
FTPClient ftp = new FTPClient();
try {
FTPClientConfig conf = new FTPClientConfig(
FTPClientConfig.SYST_UNIX);
ftp.configure(conf);
System.out.println("FTP.imag11e=" + FTP.IMAGE_FILE_TYPE);
ftp.connect(ip, 21);
ftp.login(username, psw);
ftp.setFileType(FTP.IMAGE_FILE_TYPE);
System.out.println(ftp.getReplyString());
//ftp.cwd(dir);
File file = new File("./updates/" + fileName);
FileOutputStream out = new FileOutputStream(file);
ftp.retrieveFile(fileName, out);
System.out.println(fileName + " is downloading.....");
out.flush();
out.close();
System.out.println("Download is Over");
return true;
} catch (SocketException e) {
JOptionPane.showMessageDialog(parent, "Á¬½Ó²»µ½Éý¼¶·þÎñÆ÷£¬Çë¼ì²éÍøÂç»òÁªÏµ·þÎñÆ÷¹ÜÀíÔ±£¡",
"¾¯¸æÐÅÏ¢", JOptionPane.WARNING_MESSAGE);
return false;
} catch (IOException e) {
JOptionPane.showMessageDialog(parent, "ÏÂÔØÎļþ¹ý³ÌÖгö´í£¬ÇëÖØÊÔ£¡", "¾¯¸æÐÅÏ¢",
JOptionPane.WARNING_MESSAGE);
return false;
}
}
}
//This code copy from csdn voxer