解决api/version乱码问题
This commit is contained in:
@@ -18,6 +18,7 @@ import cn.wildfirechat.pojos.OutputLoginData;
|
|||||||
import io.netty.handler.codec.http.FullHttpRequest;
|
import io.netty.handler.codec.http.FullHttpRequest;
|
||||||
import io.netty.handler.codec.http.HttpResponseStatus;
|
import io.netty.handler.codec.http.HttpResponseStatus;
|
||||||
import win.liyufan.im.GitRepositoryState;
|
import win.liyufan.im.GitRepositoryState;
|
||||||
|
import win.liyufan.im.Utility;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@@ -32,7 +33,8 @@ public class VersionAction extends Action {
|
|||||||
response.setStatus(HttpResponseStatus.OK);
|
response.setStatus(HttpResponseStatus.OK);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response.setContent(new Gson().toJson(GitRepositoryState.getGitRepositoryState()));
|
|
||||||
|
response.setContent(Utility.formatJson(new Gson().toJson(GitRepositoryState.getGitRepositoryState())));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
response.setContent("{\"version\":\"unknown\"}");
|
response.setContent("{\"version\":\"unknown\"}");
|
||||||
|
|||||||
@@ -8,8 +8,7 @@
|
|||||||
|
|
||||||
package win.liyufan.im;
|
package win.liyufan.im;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class GitRepositoryState {
|
public class GitRepositoryState {
|
||||||
@@ -64,7 +63,9 @@ public class GitRepositoryState {
|
|||||||
public static GitRepositoryState getGitRepositoryState() throws IOException {
|
public static GitRepositoryState getGitRepositoryState() throws IOException {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
try {
|
try {
|
||||||
properties.load(new FileInputStream("config/git.properties"));
|
InputStream inputStream = new FileInputStream("config/git.properties");
|
||||||
|
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
properties.load(bf);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,4 +59,78 @@ public class Utility {
|
|||||||
|
|
||||||
return macAddressBuilder.toString();
|
return macAddressBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 格式化
|
||||||
|
*
|
||||||
|
* @param jsonStr
|
||||||
|
* @return
|
||||||
|
* @author lizhgb
|
||||||
|
* @Date 2015-10-14 下午1:17:35
|
||||||
|
* @Modified 2017-04-28 下午8:55:35
|
||||||
|
*/
|
||||||
|
public static String formatJson(String jsonStr) {
|
||||||
|
if (null == jsonStr || "".equals(jsonStr))
|
||||||
|
return "";
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
char last = '\0';
|
||||||
|
char current = '\0';
|
||||||
|
int indent = 0;
|
||||||
|
boolean isInQuotationMarks = false;
|
||||||
|
for (int i = 0; i < jsonStr.length(); i++) {
|
||||||
|
last = current;
|
||||||
|
current = jsonStr.charAt(i);
|
||||||
|
switch (current) {
|
||||||
|
case '"':
|
||||||
|
if (last != '\\'){
|
||||||
|
isInQuotationMarks = !isInQuotationMarks;
|
||||||
|
}
|
||||||
|
sb.append(current);
|
||||||
|
break;
|
||||||
|
case '{':
|
||||||
|
case '[':
|
||||||
|
sb.append(current);
|
||||||
|
if (!isInQuotationMarks) {
|
||||||
|
sb.append('\n');
|
||||||
|
indent++;
|
||||||
|
addIndentBlank(sb, indent);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ']':
|
||||||
|
if (!isInQuotationMarks) {
|
||||||
|
sb.append('\n');
|
||||||
|
indent--;
|
||||||
|
addIndentBlank(sb, indent);
|
||||||
|
}
|
||||||
|
sb.append(current);
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
sb.append(current);
|
||||||
|
if (last != '\\' && !isInQuotationMarks) {
|
||||||
|
sb.append('\n');
|
||||||
|
addIndentBlank(sb, indent);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
sb.append(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加space
|
||||||
|
*
|
||||||
|
* @param sb
|
||||||
|
* @param indent
|
||||||
|
* @author lizhgb
|
||||||
|
* @Date 2015-10-14 上午10:38:04
|
||||||
|
*/
|
||||||
|
private static void addIndentBlank(StringBuilder sb, int indent) {
|
||||||
|
for (int i = 0; i < indent; i++) {
|
||||||
|
sb.append('\t');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user