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

时间:2023-03-09 20:22:09
【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("执行完毕!");
} }