/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package slippb_applet; import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; /** * * @author Administrator */ public class AutoUpdate { private String serverPBPath = ""; private String clientPBPath = ""; private String serverVersion = ""; private String clientVersion = ""; private String serverFiles = ""; public AutoUpdate(String serverPBPath, String clientPBPath, String serverFiles, String serverVersion) throws Exception { this.serverPBPath = serverPBPath; this.clientPBPath = clientPBPath; this.serverVersion = serverVersion; this.serverFiles = serverFiles; SetClientVersion(); // if (!serverVersion.equals(clientVersion)) { // CopyExePblFile(); // } } private void SetClientVersion() { File file = new File(clientPBPath + "version.txt"); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; String result = ""; try { fis = new FileInputStream(file); // Here BufferedInputStream is added for fast reading. bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // dis.available() returns 0 if the file does not have more lines. while (dis.available() != 0) { // this statement reads the line from the file and print it to // the console. result += dis.readLine(); } result = result.trim(); // dispose all the resources after using them. fis.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.clientVersion = result; } private void CopyExePblFile() throws Exception { String[] files = serverFiles.split("\\,"); for (int i = 0; i < files.length; i++) { saveUrl(serverPBPath + files[i].trim(), clientPBPath + files[i].trim()); } try { // Create file FileWriter fstream = new FileWriter(clientPBPath + "version.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write(serverVersion); //Close the output stream out.close(); } catch (Exception e) {//Catch exception if any System.err.println("Error: " + e.getMessage()); } } private void copyfile(String srFile, String dtFile) { try { File f1 = new File(srFile); File f2 = new File(dtFile); InputStream in = new FileInputStream(f1); //For Append the file. // OutputStream out = new FileOutputStream(f2,true); //For Overwrite the file. OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); System.out.println("File copied."); } catch (FileNotFoundException ex) { System.out.println(ex.getMessage() + " in the specified directory."); System.exit(0); } catch (IOException e) { System.out.println(e.getMessage()); } } public void saveUrl(String urlString, String filename) throws MalformedURLException, IOException { BufferedInputStream in = null; FileOutputStream fout = null; try { in = new BufferedInputStream(new URL(urlString).openStream()); fout = new FileOutputStream(filename); byte data[] = new byte[1024]; int count; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } } public String GetServerVersion() { return serverVersion; } public String GetClientVersion() { return clientVersion; } public void UpdateVersion() throws Exception { if (!serverVersion.equals(clientVersion)) { CopyExePblFile(); SetClientVersion(); } } }