1. 程式人生 > >關於Java實現連線伺服器獲取更新

關於Java實現連線伺服器獲取更新

第一步:

             在tomcat伺服器的webapps\ROOT資料夾下放入兩個檔案,一個是Update.txt。(更新資訊)另一個是info.java(新版本檔案)在第一個檔案裡面寫入   02,最近版本,http://localhost:8080/Info.java,new version   這個格式的資訊(版本號,更新描述,在伺服器上的地址,存放在本地時的檔名)注意,在編輯update.txt儲存時,

要以utf-8無BOM格式編碼儲存,不然在後面讀取時會出現一些特殊字元,如"?"。

第二步:

           編碼實現:

           1、建立JavaBean Info檔案,定義變數,生成get/set方法。寫定一個版本號

    2、建立介面

               public interface CheckUpdateI {
               //檢查更新
               void CheckUpdate();
              //下載更新
               void DownLoad(URL theURL, String filePath) throws IOException;
               }

   3、實現介面

       public class CheckUpdateImpl implements CheckUpdateI {
               String lastestversionid;
               String softwareurl;
               String description;
               String file;
               //檢查更新
public void CheckUpdate() {
// TODO Auto-generated method stub
Info info = new Info();
String read;
String readStr ="";
String  FileName="Http://localhost:8080/update.txt";
try{
URL url =new URL(FileName);
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
urlCon.setConnectTimeout(5000);
urlCon.setReadTimeout(5000);
BufferedReader br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"utf-8"));
List<Info>list = new ArrayList<>();
while ((read = br.readLine()) !=null) {
    readStr = readStr + read;
}
br.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
readStr ="f";
}
String[] information=readStr.split(","); 
this.setLastestversionid(information[0]);
this.setDescription(information[1]);
this.setSoftwareurl(information[2]);
this.setFile(information[3]);
}
public String getLastestversionid() {
return lastestversionid;
}
public void setLastestversionid(String lastestversionid) {
this.lastestversionid = lastestversionid;
}
//下載
@Override
public  void DownLoad(URL theURL, String filePath) throws IOException{
// TODO Auto-generated method stub   
    File dirFile = new File(filePath);
       if(!dirFile.exists()){//檔案路徑不存在時,自動建立目錄
         dirFile.mkdir();
       }
  //從伺服器上獲取檔案並儲存
     URLConnection  connection = theURL.openConnection();
     InputStream in = connection.getInputStream();  
     FileOutputStream os = new FileOutputStream(filePath+"//"+this.getFile()); 
     byte[] buffer = new byte[4 * 1024];  
     int read;  
     while ((read = in.read(buffer)) > 0) {  
         os.write(buffer, 0, read);  
          }  
       os.close();  
       in.close();
  }
public String getSoftwareurl() {
return softwareurl;
}
public void setSoftwareurl(String softwareurl) {
this.softwareurl = softwareurl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}

4、在service中呼叫介面

public class CheckUpdateService {
public Integer now = 0;
public Integer lastest = 0;
public void CheckUpdate(){
CheckUpdateImpl cui = new CheckUpdateImpl();
cui.CheckUpdate();
//cui.getLastestversionid();
Info info = new Info();
System.out.println(info.getVersion());
System.out.println(cui.getLastestversionid());
//Info info = new Info();
now = Integer.parseInt(Info.getVersion());
lastest = Integer.parseInt(cui.getLastestversionid());
if(lastest>now){
info.setVersion(cui.getLastestversionid());
         try {  
         String urlPath = cui.getSoftwareurl();   
     String filePath = "E://ProjectDowload";   //本地存放檔案的地址
     URL url = new URL(urlPath);   
             cui.DownLoad(url, filePath);   
          } catch (IOException e) {   
           e.printStackTrace();   
        }   
JOptionPane.showMessageDialog(null, "更新成功"+"新版本介紹:"+cui.getDescription());
}else{
JOptionPane.showMessageDialog(null, "已是最新版本");
}

System.out.println(lastest);
}


}