提交 b5859d70 编写于 作者: O o2null

修正windows下中文路径无法启动的问题

上级 e25fe8ee
......@@ -116,7 +116,7 @@ public class CreateConfigSample {
String name = StringUtils.lowerCase(cls.getSimpleName().substring(0, 1)) + cls.getSimpleName().substring(1)
+ ".json";
File file = new File(dir.toFile(), name);
logger.print("create file:{}.", file.getAbsoluteFile());
// logger.print("create file:{}.", file.getAbsoluteFile());
FileUtils.write(file, XGsonBuilder.toJson(map), DefaultCharset.charset);
}
......
package com.x.base.core.project.tools;
import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.util.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.ConfigObject;
import com.x.base.core.project.config.Nodes;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.Nodes;
import com.x.base.core.project.gson.XGsonBuilder;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
public class BaseTools {
private static Logger logger = LoggerFactory.getLogger(BaseTools.class);
public static String getBasePath() throws Exception {
return getBaseDirectory().getAbsolutePath();
public static String getBasePath() throws IOException, URISyntaxException {
return getBaseDirectory().toAbsolutePath().toString();
}
public static File getBaseDirectory() throws Exception {
String path = BaseTools.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File file = new File(path);
if (!file.isDirectory()) {
file = file.getParentFile();
}
while (null != file) {
File versionFile = new File(file, "version.o2");
if (versionFile.exists()) {
return file;
/**
* 从Main.class所在的目录开始递归向上,找到version.o2所在目录,就是程序根目录.
*
* @return
* @throws IOException
* @throws URISyntaxException
*/
private static Path getBaseDirectory() throws IOException, URISyntaxException {
Path path = Paths.get(
new URI("file://" + BaseTools.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
while (Files.exists(path)) {
Path versionFile = path.resolve("version.o2");
if (Files.exists(versionFile) && Files.isRegularFile(versionFile)) {
return path.toAbsolutePath();
}
file = file.getParentFile();
path = path.getParent();
}
throw new Exception("can not define o2server base directory.");
throw new IOException("can not define o2server base directory.");
}
// public static File getBaseDirectory() throws Exception {
// String path = BaseTools.class.getProtectionDomain().getCodeSource().getLocation().getPath();
// File file = new File(path);
// if (!file.isDirectory()) {
// file = file.getParentFile();
// }
// while (null != file) {
// File versionFile = new File(file, "version.o2");
// if (versionFile.exists()) {
// return file;
// }
// file = file.getParentFile();
// }
// throw new Exception("can not define o2server base directory.");
// }
public static <T> T readConfigObject(String path, Class<T> cls) throws Exception {
String base = BaseTools.getBasePath();
File file = new File(base, path);
......@@ -136,28 +172,28 @@ public class BaseTools {
String base = BaseTools.getBasePath();
File file = new File(base, path);
if ((!file.exists()) || file.isDirectory()) {
//throw new Exception("can not get file with path:" + file.getAbsolutePath());
// throw new Exception("can not get file with path:" + file.getAbsolutePath());
return null;
}
return FileUtils.readFileToString(file, DefaultCharset.charset);
}
public static boolean executeSyncFile(String syncFilePath) throws Exception {
public static boolean executeSyncFile(String syncFilePath) throws Exception {
boolean Syncflag = false;
Nodes nodes = Config.nodes();
//同步config文件
if(Config.nodes().centerServers().first().getValue().getConfigApiEnable()) {
// 同步config文件
if (Config.nodes().centerServers().first().getValue().getConfigApiEnable()) {
for (String node : nodes.keySet()) {
if (nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()) {
Syncflag = executeSyncFile(syncFilePath, node, nodes.get(node).nodeAgentPort());
}
}
}
return Syncflag;
return Syncflag;
}
private static boolean executeSyncFile(String syncFilePath , String nodeName ,int nodePort){
private static boolean executeSyncFile(String syncFilePath, String nodeName, int nodePort) {
boolean syncFileFlag = false;
File syncFile;
InputStream fileInputStream = null;
......@@ -165,18 +201,18 @@ public class BaseTools {
try (Socket socket = new Socket(nodeName, nodePort)) {
syncFile = new File(Config.base(), syncFilePath);
fileInputStream= new FileInputStream(syncFile);
fileInputStream = new FileInputStream(syncFile);
socket.setKeepAlive(true);
socket.setSoTimeout(2000);
DataOutputStream dos = null;
DataInputStream dis = null;
DataInputStream dis = null;
try {
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
Map<String, Object> commandObject = new HashMap<>();
commandObject.put("command", "syncFile:"+ syncFilePath);
commandObject.put("command", "syncFile:" + syncFilePath);
commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
dos.writeUTF(XGsonBuilder.toJson(commandObject));
dos.flush();
......@@ -184,17 +220,16 @@ public class BaseTools {
dos.writeUTF(syncFilePath);
dos.flush();
logger.info("同步文件:"+syncFilePath+" starting...");
logger.info("同步文件:" + syncFilePath + " starting...");
byte[] bytes = new byte[1024];
int length =0;
while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
int length = 0;
while ((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
dos.write(bytes, 0, length);
dos.flush();
}
logger.info("同步文件:" + syncFilePath +"end.");
logger.info("同步文件:" + syncFilePath + "end.");
}finally {
} finally {
dos.close();
dis.close();
socket.close();
......
......@@ -12,6 +12,8 @@ import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Method;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.MappedByteBuffer;
......@@ -111,7 +113,7 @@ public class Main {
}, "consoleCommandThread");
private static void init() throws Exception {
String base = getBasePath();
String base = getBasePath().toString();
pid(base);
scanWar(base);
cleanTempDir(base);
......@@ -675,26 +677,45 @@ public class Main {
method.invoke(urlClassLoader, new Object[] { local_temp_classes_dir.toURI().toURL() });
}
private static String getBasePath() throws Exception {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File file = new File(path);
if (!file.isDirectory()) {
file = file.getParentFile();
}
while (null != file) {
File versionFile = new File(file, "version.o2");
if (versionFile.exists()) {
return file.getAbsolutePath();
/**
* 从Main.class所在的目录开始递归向上,找到version.o2所在目录,就是程序根目录.
*
* @return
* @throws IOException
* @throws URISyntaxException
*/
private static Path getBasePath() throws IOException, URISyntaxException {
Path path = Paths
.get(new URI("file://" + Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()));
while (Files.exists(path)) {
Path versionFile = path.resolve("version.o2");
if (Files.exists(versionFile) && Files.isRegularFile(versionFile)) {
return path.toAbsolutePath();
}
file = file.getParentFile();
path = path.getParent();
}
throw new Exception("can not define o2server base directory.");
throw new IOException("can not define o2server base directory.");
}
// private static String getBasePath() throws Exception {
// String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
// File file = new File(path);
// if (!file.isDirectory()) {
// file = file.getParentFile();
// }
// while (null != file) {
// File versionFile = new File(file, "version.o2");
// if (versionFile.exists()) {
// return file.getAbsolutePath();
// }
// file = file.getParentFile();
// }
// throw new Exception("can not define o2server base directory.");
// }
private static void cleanTempDir(String base) throws Exception {
File local_temp_dir = new File(base, "local/temp");
FileUtils.forceMkdir(local_temp_dir);
FileUtils.cleanDirectory(local_temp_dir);
File temp = new File(base, "local/temp");
FileUtils.forceMkdir(temp);
FileUtils.cleanDirectory(temp);
}
private static List<String> readManifest(File file) throws Exception {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册