修复文件读写的乱码问题
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
"projectName" : "HTTP SERVER 模拟器",
|
||||
"width" : 1200,
|
||||
"height" : 800,
|
||||
"stringLen" : 8,
|
||||
"intLen" : 8,
|
||||
"serverConfigs" : [ {
|
||||
"serverName" : "WMS",
|
||||
"port" : 8082,
|
||||
@@ -12,21 +10,7 @@
|
||||
"url" : "/login",
|
||||
"serverName" : "WMS",
|
||||
"requestType" : "POST",
|
||||
"responseBody" : "{\n\t\"success\": \"$int$\"\n}",
|
||||
"headerMap" : null
|
||||
}, {
|
||||
"urlName" : "退出",
|
||||
"url" : "/logout",
|
||||
"serverName" : "WMS",
|
||||
"requestType" : "POST",
|
||||
"responseBody" : "{\n\t\"id\": 1\n}",
|
||||
"headerMap" : null
|
||||
}, {
|
||||
"urlName" : "登录2",
|
||||
"url" : "/login22",
|
||||
"serverName" : "WMS",
|
||||
"requestType" : "POST",
|
||||
"responseBody" : "",
|
||||
"responseBody" : "{\n\t\"success\": \"ok\"\n}",
|
||||
"headerMap" : null
|
||||
} ]
|
||||
}, {
|
||||
|
||||
@@ -4,16 +4,18 @@ import com.dayrain.views.HomePage;
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class ApplicationStarter extends Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
launch(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
new HomePage(primaryStage).start();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.dayrain.component;
|
||||
|
||||
import com.dayrain.utils.FileUtils;
|
||||
import com.fasterxml.jackson.databind.util.BeanUtil;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class ConfigHolder {
|
||||
private static Configuration configuration;
|
||||
@@ -19,4 +22,8 @@ public class ConfigHolder {
|
||||
public static Configuration get() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public static void replace(Configuration config) {
|
||||
configuration = config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,4 +73,16 @@ public class Configuration {
|
||||
public void setIntLen(int intLen) {
|
||||
this.intLen = intLen;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Configuration{" +
|
||||
"projectName='" + projectName + '\'' +
|
||||
", width=" + width +
|
||||
", height=" + height +
|
||||
", stringLen=" + stringLen +
|
||||
", intLen=" + intLen +
|
||||
", serverConfigs=" + serverConfigs +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.dayrain.handle;
|
||||
|
||||
import com.dayrain.component.ConfigHolder;
|
||||
import com.dayrain.component.Configuration;
|
||||
import com.dayrain.utils.FileUtils;
|
||||
import com.dayrain.views.HomePage;
|
||||
@@ -38,6 +39,7 @@ public class ImportConfigHandler implements EventHandler<ActionEvent> {
|
||||
|
||||
if(file != null) {
|
||||
Configuration loadConfig = FileUtils.load(file);
|
||||
ConfigHolder.replace(loadConfig);
|
||||
FileUtils.saveConfig(loadConfig);
|
||||
homePage.replaceConfig(loadConfig);
|
||||
homePage.restart();
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
package com.dayrain.utils;
|
||||
|
||||
import com.dayrain.ApplicationStarter;
|
||||
import com.dayrain.component.Configuration;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class FileUtils{
|
||||
|
||||
private static String configPath = getResourcePath("config.json");
|
||||
|
||||
public static void saveConfig(Configuration configuration){
|
||||
saveConfig(configuration, new File(configPath));
|
||||
|
||||
saveConfig(configuration, getConfigFile());
|
||||
}
|
||||
|
||||
public static void saveConfig(Configuration configuration, File file){
|
||||
FileWriter fileWriter = null;
|
||||
|
||||
BufferedWriter bufferedWriter = null;
|
||||
try {
|
||||
fileWriter = new FileWriter(file);
|
||||
String config = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(configuration);
|
||||
fileWriter.write(config);
|
||||
fileWriter.flush();
|
||||
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
|
||||
bufferedWriter.write(config);
|
||||
bufferedWriter.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
if(fileWriter != null) {
|
||||
fileWriter.close();
|
||||
if(bufferedWriter != null) {
|
||||
bufferedWriter.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -39,15 +47,16 @@ public class FileUtils{
|
||||
}
|
||||
|
||||
public static Configuration load() {
|
||||
return load(new File(configPath));
|
||||
|
||||
return load(getConfigFile());
|
||||
}
|
||||
|
||||
public static Configuration load(File file) {
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
StringBuilder configStr = new StringBuilder();
|
||||
bufferedReader = new BufferedReader(new FileReader(file));
|
||||
String buf = null;
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
|
||||
String buf;
|
||||
while ((buf = bufferedReader.readLine()) != null) {
|
||||
configStr.append(buf);
|
||||
}
|
||||
@@ -66,6 +75,7 @@ public class FileUtils{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public static String getFromInputStream(InputStream inputStream) {
|
||||
try {
|
||||
byte[]buf = new byte[4096];
|
||||
@@ -82,14 +92,27 @@ public class FileUtils{
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getResourcePath(String fileName) {
|
||||
String file = Thread.currentThread().getContextClassLoader().getResource("resources/" + fileName).getFile();
|
||||
return new File(file).toString();
|
||||
private static File getConfigFile() {
|
||||
String property = System.getProperty("user.dir");
|
||||
File file = new File(property + File.separator + "config" + File.separator + "config.json");
|
||||
if(!file.exists()) {
|
||||
|
||||
File dir = new File(property + File.separator + "config");
|
||||
if(!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Configuration configuration = new Configuration(1200, 800, 8, 8);
|
||||
saveConfig(configuration, file);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static String getResourcePathWithProtocol(String fileName) {
|
||||
String file = Thread.currentThread().getContextClassLoader().getResource("resources/" + fileName).getFile();
|
||||
return "file:" + File.separator + new File(file);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,10 +47,10 @@ import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Circle;
|
||||
import javafx.scene.text.Font;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import javafx.util.Callback;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -121,7 +121,7 @@ public class HomePage {
|
||||
primaryStage.setHeight(configuration.getHeight());
|
||||
primaryStage.getIcons().add(getIcon());
|
||||
primaryStage.show();
|
||||
primaryStage.setOnCloseRequest(event -> FileUtils.saveConfig(configuration));
|
||||
primaryStage.setOnCloseRequest(event -> ConfigHolder.save());
|
||||
}
|
||||
|
||||
public void drawServerPanel(VBox serverContainer, ServerConfig serverConfig, Stage primaryStage) {
|
||||
@@ -253,12 +253,7 @@ public class HomePage {
|
||||
}
|
||||
|
||||
public Image getIcon() {
|
||||
try {
|
||||
return new Image(new FileInputStream(FileUtils.getResourcePath("panda.png")));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
return new Image("resources/panda.png");
|
||||
}
|
||||
|
||||
public Background getBackGround() {
|
||||
|
||||
Reference in New Issue
Block a user