Beispiel #1
0
  private void dorestart() {
    // TODO Auto-generated method stub
    File thisfile = new File(fileName);
    System.out.println(thisfile.getAbsolutePath());
    // thisfile.renameTo(new File("old" + fileName));
    copy(filePath + "/" + fileName, fileName);
    DialogUtil.openInfo(shellww, "更新成功,立马重启");
    shellww.getDisplay().dispose();

    MainApp.main(new String[] {});
  }
Beispiel #2
0
 private boolean canUpdate() {
   // TODO Auto-generated method stub
   DialogUtil.openInfo(shellww, HttpUtils.getHtml(chekupdate) + "--" + version);
   return (HttpUtils.getHtml(chekupdate) > Double.valueOf(version));
 }
Beispiel #3
0
  protected void down() {
    // TODO Auto-generated method stub
    cacel.setEnabled(false);

    if (!canUpdate()) {
      DialogUtil.openInfo(shellww, "没有发下新版本!");
      return;
    }
    HttpURLConnection httpURLConnection = null;
    URL url = null;
    BufferedInputStream bis = null;
    byte[] buf = new byte[10240];
    int size = 0;
    File filea = new File(filePath);
    if (!filea.exists()) {
      filea.mkdir();
    }
    String remoteUrl = downurl;

    // 检查本地文件
    RandomAccessFile rndFile = null;
    File file = new File(filePath + "//" + fileName);
    long remoteFileSize = getRemoteFileSize(remoteUrl);
    long nPos = 0;

    // System.out.println("remote size" + remoteFileSize + "--" + (int)
    // remoteFileSize);
    long every = remoteFileSize / 100;
    // System.out.println("every size" + every);

    if (file.exists()) {
      long localFileSzie = file.length();
      if (localFileSzie < remoteFileSize) {
        System.out.println("文件续传...");
        nPos = localFileSzie;
      } else {
        System.out.println("文件存在,重新下载...");
        file.delete();
        try {
          file.createNewFile();
        } catch (Exception e) {
          // TODO: handle exception
          e.printStackTrace();
        }
      }

    } else {
      // 建立文件
      try {
        file.createNewFile();
      } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
      }
    }

    // 下载文件
    try {
      url = new URL(remoteUrl);
      httpURLConnection = (HttpURLConnection) url.openConnection();
      // 设置User-Agent
      httpURLConnection.setRequestProperty("User-Agent", "Net");
      // 设置续传开始
      httpURLConnection.setRequestProperty("Range", "bytes=" + nPos + "-");
      // 获取输入流
      bis = new BufferedInputStream(httpURLConnection.getInputStream());
      rndFile = new RandomAccessFile(filePath + "\\" + fileName, "rw");
      rndFile.seek(nPos);
      long upsize = 0L; // 递增计数
      int num = 0;
      while ((size = bis.read(buf)) != -1) {
        // if (i > 500) break;
        rndFile.write(buf, 0, size);
        upsize += size;
        num = (int) (upsize / every);
        System.out.println(num + "///////////");
        updatepro.setSelection(num);
      }
      updatepro.setSelection(100);
      httpURLConnection.disconnect();
      MessageDialog.openInformation(shellww, "下载成功!", "重新启动!");
    } catch (Exception e) {
      // TODO: handle exception
      MessageDialog.openInformation(shellww, "exception", e.getMessage());
      MessageDialog.openError(shellww, "网络异常", "网络连接失败,请检查网络环境!");
      return;
    }
    dorestart();
  }