ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-11-04 14:12:14  阅读:270  来源: 互联网

标签: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); } }

标签:Java,文件夹,服务器
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有