【JavaFx】客户端服务器C/S架构搭建

客户端获取服务器端软件更新版本方法:

package com.platform.ui.update;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.filechooser.FileSystemView;

import net.jimmc.jshortcut.JShellLink;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

public class DownloadFileController extends AnchorPane {
    @FXML
    private Button download;

    @FXML
    void downloadFile() {

        // 获取资源路径
        String tempResourcePath = this.getClass().getClassLoader()
                .getResource("").getPath();
        String resourcePath = tempResourcePath.substring(1,
                tempResourcePath.indexOf("classes"))
                + "resource";

        String targetPath = "C:\\f1";

        File targetFile = new File(targetPath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        File[] files = new File(resourcePath).listFiles();

        for (File file : files) {
            // File resourceFile = new File(resourcePath);

            // 以流的形式下载文件。
            InputStream fis;
            try {
                fis = new BufferedInputStream(new FileInputStream(
                        file.getAbsolutePath()));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                FileOutputStream out = new FileOutputStream(targetFile + "\\"
                        + file.getName());
                out.write(buffer);
                out.flush();
                out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 创建写入的目标文件
        String batPath = "C:\\f1\\run.bat";
        File file = new File(batPath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 写出流
        BufferedWriter output;
        try {
            output = new BufferedWriter(new FileWriter(file));
            output.write("cd C:\\f1");
            output.write("\r\n");
            output.write("javaws yk_platform_client.jnlp");
            output.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // 在桌面创建run.bat快捷方式
        FileSystemView fsv = FileSystemView.getFileSystemView();
        String writeFolderPath = fsv.getHomeDirectory().toString() + "\\"; // 这便是读取桌面路径的方法了
        String jarFileName = "C:\\f1\\run.bat";// 建立快捷方式后鼠标放到上面的时候现实的文件所存位置
        // create lnk file
        JShellLink link = new JShellLink();
        link.setFolder(writeFolderPath); // 创建的快捷方式所存在的位置,路径要真实路径,放到快速启动栏里面
        link.setName("Amazon采集器更新文件"); // 快捷方式的名称
        link.setIconLocation("C:\\f1\\erp.ico");// 图片位置
        link.setPath(jarFileName);
        link.setArguments("");// 设置执行参数
        link.save();

        System.out.println("执行完毕!");
    }

}

  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值