IndexController.java
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.bsth.controller;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class IndexController {
Logger logger = LoggerFactory.getLogger(this.getClass());
String indexSource;
/**
* 构造函数
*/
public IndexController() {
load();
}
/**
*
* @Title: index
* @Description: TODO(输出首页 index.html)
*/
@RequestMapping
public void index(String initFragment, HttpServletResponse resp) {
// 初始打开的片段地址
String outStr = StringUtils.replace(indexSource, "^_^initFragment^_^",
initFragment == null ? "" : initFragment);
resp.setContentType("text/html;charset=UTF-8");
try {
resp.getOutputStream().write(outStr.getBytes());
} catch (IOException e) {
logger.error("", e);
}
}
@RequestMapping(value = "/index/load", method = RequestMethod.GET)
public void load() {
BufferedInputStream bis = null;
try {
InputStream is = IndexController.class.getClassLoader().getResourceAsStream("static/index.html");
bis = new BufferedInputStream(is);
StringBuilder source = new StringBuilder();
byte[] buffer = new byte[bis.available()];
while (bis.read(buffer) != -1) {
source.append(new String(buffer));
}
indexSource = source.toString();
} catch (Exception e) {
logger.error("", e);
} finally {
try {
bis.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
}