mirror of
https://gitee.com/willfree/minoaClientWindows_gradle.git
synced 2026-10-05 01:15:44 +08:00
286 lines
11 KiB
Java
286 lines
11 KiB
Java
package cn.minoa.view.login;
|
||
|
||
import cn.minoa.MainApp;
|
||
import javafx.fxml.FXML;
|
||
import javafx.scene.control.Alert;
|
||
import javafx.scene.control.TextField;
|
||
import javafx.scene.image.Image;
|
||
import javafx.scene.image.ImageView;
|
||
import javafx.stage.Stage;
|
||
|
||
import java.io.*;
|
||
import java.net.InetSocketAddress;
|
||
import java.net.Socket;
|
||
//import java.net.URL;
|
||
import java.util.Properties;
|
||
import java.util.regex.Pattern;
|
||
|
||
public class SettingOverviewController {
|
||
@SuppressWarnings("unused")
|
||
private MainApp mainApp;
|
||
@SuppressWarnings("unused")
|
||
private Stage dialogStage;
|
||
|
||
@FXML
|
||
private TextField ipAddress;
|
||
@FXML
|
||
private TextField portAddress;
|
||
@FXML
|
||
private TextField packetPrefix;
|
||
@FXML
|
||
private ImageView miniLogo;
|
||
|
||
public SettingOverviewController(){
|
||
}
|
||
|
||
@FXML
|
||
private void initialize() {
|
||
// 在fxml被载入时候被自动调用,在setMainApp那里将修改为显示配置文件信息
|
||
miniLogo.setImage(new Image("file:resources/images/logo_mini.png"));
|
||
ipAddress.setPromptText("IP格式示例:121.15.171.89");
|
||
portAddress.setPromptText("端口号格式示例:6363");
|
||
packetPrefix.setPromptText("格式示例:/ndn/edu/pkusz/OA)");
|
||
}
|
||
|
||
// 根据表单修改配置文件
|
||
@FXML
|
||
private void handleSetting() {
|
||
if (isRightContent()) {
|
||
//修改本地配置文件
|
||
changeSettingProperties();
|
||
mainApp.loadSettingProperties();
|
||
//弹窗提示
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.INFORMATION);
|
||
alert.setTitle("设置成功");
|
||
alert.setHeaderText("服务器地址/包前缀修改成功");
|
||
alert.setContentText("您已成功修改服务器IP/包前缀。\n请重新启动应用以生效新设置!");
|
||
alert.showAndWait();
|
||
System.exit(0);
|
||
}
|
||
}
|
||
|
||
private void changeSettingProperties(){
|
||
InputStream inputStream = null;
|
||
OutputStream outputStream=null;
|
||
try {
|
||
//如果 classPath 不存在,则获取的输入流会为 null
|
||
inputStream=new BufferedInputStream(new FileInputStream(mainApp.getClientPath()+ File.separator+"ndn"+File.separator+"settings.properties"));
|
||
// inputStream = MainApp.class.getClassLoader().getResourceAsStream("ndn/settings.properties");
|
||
if (inputStream == null) {
|
||
System.out.println("没有找到配置文件...");
|
||
}
|
||
Properties settingProperties = new Properties();
|
||
settingProperties.load(inputStream);
|
||
settingProperties.setProperty("ServerIPAddress",ipAddress.getText().trim());
|
||
settingProperties.setProperty("ServerPortAddress",portAddress.getText().trim());
|
||
settingProperties.setProperty("DataPackagePrefix",packetPrefix.getText().trim());
|
||
|
||
outputStream=new BufferedOutputStream(new FileOutputStream(mainApp.getClientPath()+ File.separator+"ndn"+File.separator+"settings.properties"));
|
||
// URL url = MainApp.class.getClassLoader().getResource("ndn/settings.properties");
|
||
// outputStream = new FileOutputStream(url.getPath());
|
||
settingProperties.store(outputStream,"修改配置文件");
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}finally{
|
||
//关闭输入流
|
||
if (inputStream != null) {
|
||
try {
|
||
inputStream.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
//关闭输出流
|
||
if (outputStream != null) {
|
||
try {
|
||
outputStream.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
* @param mainApp
|
||
*/
|
||
public void setMainApp(MainApp mainApp) {
|
||
this.mainApp = mainApp;
|
||
ipAddress.setText(mainApp.minoaServerIpString);
|
||
portAddress.setText(mainApp.minoaServerPortInteger.toString());
|
||
packetPrefix.setText(mainApp.globalPrefixString);
|
||
packetPrefix.setDisable(true);
|
||
}
|
||
|
||
/*
|
||
* @param dialogStage
|
||
*/
|
||
public void setDialogStage(Stage dialogStage) {
|
||
this.dialogStage = dialogStage;
|
||
}
|
||
|
||
// 验证输入内容格式
|
||
private boolean isRightContent() {
|
||
//验证IP是否为空
|
||
if (ipAddress.getText().trim().equals("")) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("IP错误");
|
||
alert.setHeaderText("你的IP错误");
|
||
alert.setContentText("请确保你输入的IP不为空。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//验证IP格式是否正确
|
||
if (!ipCheck(ipAddress.getText().trim())) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("IP错误");
|
||
alert.setHeaderText("你的IP错误");
|
||
alert.setContentText("请确保你输入的IP格式正确。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//验证端口号是否为空
|
||
if (portAddress.getText().trim().equals("")) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("端口号错误");
|
||
alert.setHeaderText("你的端口号错误");
|
||
alert.setContentText("请确保你输入的端口号不为空。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//判断端口号是否正确
|
||
boolean flag_port=true;
|
||
try{
|
||
Integer port=Integer.parseInt(portAddress.getText().trim());
|
||
if((port<1)||(port>65535)){
|
||
flag_port=false;
|
||
}
|
||
}catch(Exception e){
|
||
flag_port=false;
|
||
}finally {
|
||
if(flag_port==false){
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("端口号错误");
|
||
alert.setHeaderText("你的端口号错误");
|
||
alert.setContentText("请确保你输入的端口号格式正确。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
}
|
||
//验证前缀是否为空
|
||
if (packetPrefix.getText().trim().equals("")) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("包前缀错误");
|
||
alert.setHeaderText("你的包前缀错误");
|
||
alert.setContentText("请确保你输入的包前缀不为空。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//判断前缀格式是否正确
|
||
if ((!packetPrefix.getText().trim().equals(""))&&(!prefixCheck(packetPrefix.getText().trim()))) {
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("包前缀错误");
|
||
alert.setHeaderText("你的包前缀错误");
|
||
alert.setContentText("请确保你输入的包前缀格式正确。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//使用socket判断这里的ip和端口号是否可以连接上
|
||
String ipHost=ipAddress.getText().trim();
|
||
Integer portHost=Integer.parseInt(portAddress.getText().trim());
|
||
if(!isHostConnectable(ipHost,portHost)){
|
||
// 弹窗参考:https://code.makery.ch/blog/javafx-dialogs-official/
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("连接错误");
|
||
alert.setHeaderText("地址连接错误");
|
||
alert.setContentText("请确保你输入的服务器IP及端口号是可达的。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
//判断是否与原内容一致
|
||
if(isNotChange()){
|
||
Alert alert = new Alert(Alert.AlertType.WARNING);
|
||
alert.setTitle("设置未变");
|
||
alert.setHeaderText("设置未变");
|
||
alert.setContentText("请确保新配置信息与旧配置信息是不同的。");
|
||
alert.showAndWait();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 判断新设置的参数是否与原参数一致
|
||
public boolean isNotChange(){
|
||
if(ipAddress.getText().trim().equals(mainApp.minoaServerIpString)&&
|
||
portAddress.getText().trim().equals(mainApp.minoaServerPortInteger.toString())&&
|
||
packetPrefix.getText().trim().equals(mainApp.globalPrefixString)){
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 判断ip和端口号是否可以连接
|
||
public static boolean isHostConnectable(String host, int port) {
|
||
Socket socket = new Socket();
|
||
try {
|
||
socket.connect(new InetSocketAddress(host, port));
|
||
} catch (IOException e) {
|
||
// e.printStackTrace();
|
||
return false;
|
||
} finally {
|
||
try {
|
||
socket.close();
|
||
} catch (IOException e) {
|
||
//do nothing
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//判断IP合法性【参考:https://blog.csdn.net/bokerr/article/details/83508415】
|
||
public static boolean ipCheck(String ipAddressString) {
|
||
//标准IPv4地址的正则表达式:
|
||
Pattern IPV4_REGEX =
|
||
Pattern.compile(
|
||
"^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
|
||
//无全0块,标准IPv6地址的正则表达式
|
||
Pattern IPV6_STD_REGEX =
|
||
Pattern.compile(
|
||
"^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
|
||
//压缩正则表达式
|
||
Pattern IPV6_COMPRESS_REGEX =
|
||
Pattern.compile(
|
||
"^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4})*)?)::((([0-9A-Fa-f]{1,4}:)*[0-9A-Fa-f]{1,4})?)$");
|
||
|
||
//判断是否为合法IPv4地址
|
||
if(IPV4_REGEX.matcher(ipAddressString).matches()){
|
||
return true;
|
||
}else if(IPV6_COMPRESS_REGEX.matcher(ipAddressString).matches()){
|
||
return true;
|
||
}else if(IPV6_STD_REGEX.matcher(ipAddressString).matches()){
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//判断前缀格式是否正确
|
||
public static boolean prefixCheck(String prefixString){
|
||
if(prefixString.length()<2){
|
||
return false;
|
||
}
|
||
if((prefixString.charAt(0)!='/')||(prefixString.charAt(prefixString.length()-1)=='/')){
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
}
|