关于Java实现连接服务器获取更新


第一步:

在tomcat服务器的webappsROOT文件夹下放入两个文件,一个是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); } }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/java/292579.html

(0)
上一篇 2022年11月4日 10:43
下一篇 2022年11月4日 14:46

相关推荐

发表回复

登录后才能评论