/*先把下载的文件放在UpdateTemp文件夹中,待全部下载后,再把此文件夹中的文件移出替换旧文件*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Main.java
*
* Created on 2009-7-9, 22:29:27
*/
/**
*
* @author Administrator
*/
public class Main extends javax.swing.JFrame {
Connection cn=null;
MSConnection con=new MSConnection();
File file=null;
Vector vtFileName=new Vector();
int Process=0;
/** Creates new form Main */
public Main() {
initComponents();
this.setTitle("在线更新程序");
this.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //得到屏幕的尺寸
this.setLocation((screenSize.width)/2-150, (screenSize.height)/2-200);
// try {
// Thread.currentThread().sleep(10000);
// } catch (InterruptedException ex) {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
// }
}
public void RunProgram(){
// String path=System.getProperty("user.dir"); //获取路径
try {
String OS = System.getProperty("os.name"); //获取当前操作系统,Windows or Linux
// String path=System.getProperty("user.dir"); //获取路径
String path = new File("").getCanonicalPath();
if (OS.contains("Windows")) {
try {
Process p = Runtime.getRuntime().exec("cmd.exe /c /"" + path + "//aa.jar/""); //指定到目的路径
this.dispose();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void Update(){
try {
// String path=System.getProperty("user.dir");
String path = new File("").getCanonicalPath();
String sql = "select FILE_NAME,PROGRAM from VERSION_MAST where PROGRAM_NAME='RTC'";
PreparedStatement pst = cn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
while(rs.next()){
String FileName=rs.getString("FILE_NAME").trim();
/*创建UpdateTemp文件夹*/
File file1=new File(path + "/UpdateTemp");
JOptionPane.showMessageDialog(this, path + "/UpdateTemp");
if(!file1.exists()){
file1.mkdir();
}
/*创建目录*/
String[] temp=FileName.split("/");
String folder="";
for(int i=0;i<(temp.length-1);i++){
folder=folder+"/"+temp[i];
File file2=new File(path + "/UpdateTemp"+folder);
if(!file2.exists()){
file2.mkdir();
}
}
File file=new File(path + "/UpdateTemp/" + FileName);
if(!file.exists())
file.createNewFile();
InputStream fin= rs.getBinaryStream("PROGRAM");
FileOutputStream out;
out = new FileOutputStream(file);
int c;
byte buffer[]=new byte[1024]; //设置块大小
int m=1;
while((c=fin.read(buffer))!=-1){
for(int i=0;i
out.write(buffer[i]);
}
}
fin.close();
out.close();
vtFileName.add(FileName);
}
//JOptionPane.showMessageDialog(this, "下载文件成功!");
jLabel6.setText("√");
jLabel6.updateUI();
jLabel6.setVisible(true);
jLabel6.requestFocus();
Process++;
} catch (IOException ex) {
jLabel6.setText("×");
jLabel6.setForeground(Color.RED);
jLabel6.setVisible(true);
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
jLabel6.setText("×");
jLabel6.setForeground(Color.RED);
jLabel6.setVisible(true);
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void DeleteFile(String path){
File file = new File(path);
if(file.exists()){
File[] files = file.listFiles();
for(int i=0;i
if(files[i].isDirectory()){
DeleteFile(files[i].getPath());
files[i].delete();
}
if(files[i].isFile()){
files[i].delete();
}
}
}
}
public void MoveFile(){
try {
con.CloseConnection(cn);
try {
cn.close();
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
// String path=System.getProperty("user.dir");
String path = new File("").getCanonicalPath();
for (int i = 0; i < vtFileName.size(); i++) {
changeDirectory(vtFileName.get(i).toString(), path + "/UpdateTemp", path, true);
}
this.DeleteFile(path + "/UpdateTemp");
File file = new File(path + "/UpdateTemp");
file.delete();
// if(file.exists()){
// File [] files=file.listFiles();
// for(int i=0;i
//JOptionPane.showMessageDialog(this, files[i].getPath());
// files[i].delete();
// }
// file.delete();
// }
//JOptionPane.showMessageDialog(this, "更新成功!");
jLabel7.setText("√");
jLabel7.updateUI();
jLabel7.setVisible(true);
jLabel7.requestFocus();
Process++;
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
/** *//**转移文件目录
* @param filename 文件名
* @param oldpath 旧目录
* @param newpath 新目录
* @param cover 若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作
*/
public void changeDirectory(String filename,String oldpath,String newpath,boolean cover){
if(!oldpath.equals(newpath)){
String[] temp=filename.split("/");
String floder="";
for(int i=0;i<(temp.length-1);i++){
floder=floder+"/"+temp[i];
File file = new File(newpath+floder);
if(!file.exists()){
file.mkdir();
}
}
File oldfile=new File(oldpath+"/"+filename);
File newfile=new File(newpath+"/"+filename);
if(newfile.exists()){//若在待转移目录下,已经存在待转移文件
if(cover){//覆盖
newfile.delete();
oldfile.renameTo(newfile);
}
else
System.out.println("在新目录下已经存在:"+filename);
}
else{
oldfile.renameTo(newfile);
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("正在进行更新...");
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
jLabel2.setText("连接数据库");
jLabel3.setText("下载文件");
jLabel4.setText("复制文件");
jLabel5.setForeground(new java.awt.Color(51, 255, 51));
jLabel6.setForeground(new java.awt.Color(51, 255, 51));
jLabel7.setForeground(new java.awt.Color(51, 255, 51));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(38, 38, 38)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE))
);
jButton1.setText("更新");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(jButton1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
cn=con.getConnection("10.10.10.10", "aa", "bb", "cc");
jLabel5.setText("√");
Process++;
if(Process==1){
this.Update();
}
if(Process==2){
this.MoveFile();
}
if(Process==3){
if(JOptionPane.showConfirmDialog(this, "更新成功!是否立即运行程序?","提示",JOptionPane.YES_OPTION)==0){
this.RunProgram();
}
}else{
JOptionPane.showMessageDialog(this, "更新失败!请联系相关技术人员!");
}
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}